Conditions
- What's A Condition Variable?
- Boost.Interprocess Condition Types And Headers
- Anonymous condition example
What's A Condition Variable?
- 在前面的例子中,一個mutex被用來鎖定,但我們不能用它來有效地等待,直到滿足繼續的條件。一個條件變量可以做兩件事。
- wait: 線程被阻塞,直到其他線程通知它可以繼續,因為導致等待的條件已經消失。
- 通知:線程通知其他線程可以繼續。線程向一個被阻塞的線程或所有被阻塞的線程發送一個信號,告訴它們引起它們等待的條件已經消失。
- 條件變量中的等待總是與一個mutex相關聯。在等待條件變量之前,必須先鎖定該mutex。當在條件變量上等待時,線程解鎖mutex并原子性地等待。
- 當線程從等待函數中返回時(例如因為信號或超時),mutex對象再次被鎖定。
Boost.Interprocess Condition Types And Headers
- Boost.Interprocess offers the following condition types:
- #include <boost/interprocess/sync/interprocess_condition.hpp>
- interprocess_condition。一個匿名的條件變量,可以放在共享內存或內存映射文件中,與 boost::interprocess::interprocess_mutex 一起使用。
- #include <boost/interprocess/sync/interprocess_condition_any.hpp>
- interprocess_condition_any.一個匿名的條件變量,可以放在共享內存或內存映射文件中,用于任何鎖類型。一個匿名的條件變量,可以放在共享內存或內存映射文件中,用于任何鎖類型。
- #include <boost/interprocess/sync/named_condition.hpp>
- named_condition.一個命名的條件變量,與 named_mutex 一起使用。與named_mutex一起使用的命名條件變量。
- #include <boost/interprocess/sync/named_condition_any.hpp>
- named_condition: 一個命名的條件變量,可用于任何類型的鎖。
-
命名條件與匿名條件類似,但它們與命名的mutexes結合使用。好幾次,我們不想用同步數據來存儲同步對象。我們希望使用相同的數據改變同步方法(從進程間到進程內,或者沒有任何同步)。將進程共享的匿名同步與同步數據一起存儲將禁止這樣做。我們希望通過網絡或其他通信方式發送同步數據。發送過程共享的同步對象就沒有任何意義了。
Anonymous condition example
- 想象一下,一個進程,將一個跟蹤寫到一個簡單的共享內存緩沖區,另一個進程逐一打印。第一個進程寫入跟蹤并等待另一個進程打印數據。為了達到這個目的,我們可以使用兩個條件變量:第一個條件變量用來阻止發送者,直到第二個進程打印信息,第二個條件變量用來阻止接收者,直到緩沖區有痕跡要打印。
- 共享內存跟蹤緩沖區(doc_anonymous_condition_shared_data.hpp)
#include <boost/interprocess/sync/interprocess_mutex.hpp>
#include <boost/interprocess/sync/interprocess_condition.hpp>struct trace_queue
{enum { LineSize = 100 };trace_queue(): message_in(false){}//Mutex to protect access to the queueboost::interprocess::interprocess_mutex mutex;//Condition to wait when the queue is emptyboost::interprocess::interprocess_condition cond_empty;//Condition to wait when the queue is fullboost::interprocess::interprocess_condition cond_full;//Items to fillchar items[LineSize];//Is there any messagebool message_in;
};
- 這是該進程的主進程。創建共享內存,將緩沖區放置在那里,并開始逐一寫入消息,直到寫下 "最后一條消息",表示沒有更多的消息要打印。
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <iostream>
#include <cstdio>
#include "doc_anonymous_condition_shared_data.hpp"using namespace boost::interprocess;int main ()
{//Erase previous shared memory and schedule erasure on exitstruct shm_remove{shm_remove() { shared_memory_object::remove("MySharedMemory"); }~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }} remover;//Create a shared memory object.shared_memory_object shm(create_only //only create,"MySharedMemory" //name,read_write //read-write mode);try{//Set sizeshm.truncate(sizeof(trace_queue));//Map the whole shared memory in this processmapped_region region(shm //What to map,read_write //Map it as read-write);//Get the address of the mapped regionvoid * addr = region.get_address();//Construct the shared structure in memorytrace_queue * data = new (addr) trace_queue;const int NumMsg = 100;for(int i = 0; i < NumMsg; ++i){scoped_lock<interprocess_mutex> lock(data->mutex);if(data->message_in){data->cond_full.wait(lock);}if(i == (NumMsg-1))std::sprintf(data->items, "%s", "last message");elsestd::sprintf(data->items, "%s_%d", "my_trace", i);//Notify to the other process that there is a messagedata->cond_empty.notify_one();//Mark message buffer as fulldata->message_in = true;}}catch(interprocess_exception &ex){std::cout << ex.what() << std::endl;return 1;}return 0;
}
- 第二個過程打開共享內存,打印每一條消息,直到收到 "最后一條消息 "消息。
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <iostream>
#include <cstdio>
#include "doc_anonymous_condition_shared_data.hpp"using namespace boost::interprocess;int main ()
{//Erase previous shared memory and schedule erasure on exitstruct shm_remove{shm_remove() { shared_memory_object::remove("MySharedMemory"); }~shm_remove(){ shared_memory_object::remove("MySharedMemory"); }} remover;//Create a shared memory object.shared_memory_object shm(create_only //only create,"MySharedMemory" //name,read_write //read-write mode);try{//Set sizeshm.truncate(sizeof(trace_queue));//Map the whole shared memory in this processmapped_region region(shm //What to map,read_write //Map it as read-write);//Get the address of the mapped regionvoid * addr = region.get_address();//Construct the shared structure in memorytrace_queue * data = new (addr) trace_queue;const int NumMsg = 100;for(int i = 0; i < NumMsg; ++i){scoped_lock<interprocess_mutex> lock(data->mutex);if(data->message_in){data->cond_full.wait(lock);}if(i == (NumMsg-1))std::sprintf(data->items, "%s", "last message");elsestd::sprintf(data->items, "%s_%d", "my_trace", i);//Notify to the other process that there is a messagedata->cond_empty.notify_one();//Mark message buffer as fulldata->message_in = true;}}catch(interprocess_exception &ex){std::cout << ex.what() << std::endl;return 1;}return 0;
}
- 通過條件變量,一個進程如果不能繼續工作就可以阻塞,當滿足繼續工作的條件時,另一個進程可以喚醒它。
#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
#include <iostream>
#include <cstring>
#include "doc_anonymous_condition_shared_data.hpp"using namespace boost::interprocess;int main ()
{//Create a shared memory object.shared_memory_object shm(open_only //only create,"MySharedMemory" //name,read_write //read-write mode);try{//Map the whole shared memory in this processmapped_region region(shm //What to map,read_write //Map it as read-write);//Get the address of the mapped regionvoid * addr = region.get_address();//Obtain a pointer to the shared structuretrace_queue * data = static_cast<trace_queue*>(addr);//Print messages until the other process marks the endbool end_loop = false;do{scoped_lock<interprocess_mutex> lock(data->mutex);if(!data->message_in){data->cond_empty.wait(lock);}if(std::strcmp(data->items, "last message") == 0){end_loop = true;}else{//Print the messagestd::cout << data->items << std::endl;//Notify the other process that the buffer is emptydata->message_in = false;data->cond_full.notify_one();}}while(!end_loop);}catch(interprocess_exception &ex){std::cout << ex.what() << std::endl;return 1;}return 0;
}
?