Search code examples
c++multithreadingc++11semaphorecondition-variable

Can C++11 condition_variables be used to synchronize processes?


I'm trying to learn about C++11's std::condition_variable. I've read the articles at cppreference.com and cplusplus.com as well as C++0x has no semaphores? How to synchronize threads?.

My question, which I think has not been answered by the three noted articles, is: can "semaphores" that are created with a combination of of std::mutex and std::condition_variable (see the answers to C++0x has no semaphores? How to synchronize threads?) be used to synchronize between processes the way posix named semaphores can be? It's not clear to me that this functionality can be achieved, because I don't see "sharable" information, such as a name, used in the creation of these objects.


Solution

  • No, those are meant to synchronize threads within one process, not processes.

    Interprocess communication is implemented by using shared files. An interprocess mutex or shared memory is just a file (created in your temp folder for instance) to exchange information (data, locks) between two processes. boost::interprocess offers an nice implementation of that (it does not require any link, code is compiled on the fly, you just need to include it).

    To synchronize processes, you should have a look at boost::interprocess. It offers synchronization mechanisms. It offers an interprocess semaphore.