问题
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.
回答1:
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.
回答2:
The closest thing to IPC (inter process communication) in standard C++ is file io.
There is no in-memory IPC in standard C++.
来源:https://stackoverflow.com/questions/40333264/can-c11-condition-variables-be-used-to-synchronize-processes