可以使用C ++ 11 condition_variables来同步进程吗? [英] Can C++11 condition_variables be used to synchronize processes?

查看:91
本文介绍了可以使用C ++ 11 condition_variables来同步进程吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解C ++ 11的std::condition_variable.我已经阅读了 cppreference.com C ++ 0x没有信号灯?如何同步线程?.

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?.

我的问题(我认为尚未为三篇提到的文章回答)是:是否可以将std::mutexstd::condition_variable组合创建的信号量"(请参阅​​C++0x没有信号灯?如何同步线程?)用于进程之间的同步方式可以使用posix命名信号量来实现?对我来说不清楚是否可以实现此功能,因为在创建这些对象时看不到可共享"信息,例如名称.

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.

推荐答案

不,它们是为了在一个进程(而不是进程)中同步线程.

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

进程间通信是通过使用共享文件来实现的.进程间互斥或共享内存只是一个文件(例如,在您的temp文件夹中创建),用于在两个进程之间交换信息(数据,锁). boost::interprocess提供了一个很好的实现(它不需要任何链接,代码是即时编译的,您只需要包含它即可).

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).

要同步进程,您应该查看 boost: :interprocess .它提供同步机制.它提供了进程间信号量.

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

这篇关于可以使用C ++ 11 condition_variables来同步进程吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆