Can C++11 condition_variables be used to synchronize processes?
Asked Answered
S

2

3

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.

Stickler answered 30/10, 2016 at 20:23 Comment(0)
E
4

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.

Estray answered 30/10, 2016 at 20:35 Comment(2)
I should have asked this along with my original question, but: it looks like the std::condition_variable also cannot be used like a counting semaphore, right? I see it makes use of std::uniqe_lock<std::mutex>, so it would seem usable as a binary semaphore only, right?Stickler
@StoneThrow: I would say yes, but you should ask a separate question to have a clear answer to that.Estray
C
1

The closest thing to IPC (inter process communication) in standard C++ is file io.

There is no in-memory IPC in standard C++.

Chlorinate answered 30/10, 2016 at 20:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.