C++线程间通信
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20595760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ communication between threads
提问by 2c2c
I have a couple classes that each open a different program in different threads and do/hold information about it using CreateProcess
(if there's a more C++ oriented way to do this let me know-- I looked).
我有几个类,每个类都在不同的线程中打开一个不同的程序,并使用它来执行/保存有关它的信息CreateProcess
(如果有更多面向 C++ 的方法来执行此操作,请告诉我 - 我看过了)。
Some of the classes are dependent on one of the other programs running. ie B must stop if A stopped. I made this code a while ago and my solution then, was having a class with static functions that run the various programs and static member variables that hold their "state". I was also using CreateThread
.
一些类依赖于正在运行的其他程序之一。即如果 A 停止,B 必须停止。不久前我编写了这段代码,然后我的解决方案是有一个带有静态函数的类,这些函数运行各种程序和保存其“状态”的静态成员变量。我也在使用CreateThread
.
Looking back, this method seemed... fragile and awkward looking.
I have no idea if using such a "static class" is good practice or not (especially recalling how awkward initializing the state member variables). I'd like to perhaps make each class contain its own run function. However, the issue I am considering is how to let class B know if A has awkwardly stopped. They'd still need to know a way to be aware of each other's state. Note that I'd like to use std::thread
in this rework and that I have little to no experience with multithreading. Thanks for any help.
回想起来,这种方法似乎……脆弱而笨拙的样子。我不知道使用这样的“静态类”是否是好的做法(尤其是回忆初始化状态成员变量是多么尴尬)。我想让每个类都包含自己的运行函数。但是,我正在考虑的问题是如何让 B 班知道 A 是否笨拙地停下了。他们仍然需要知道一种方法来了解彼此的状态。请注意,我想std::thread
在这次返工中使用,而且我几乎没有多线程经验。谢谢你的帮助。
回答by Matthieu M.
Well, in a multi-process application you would be using pipes/files to transmit information from one process to another (or even maybe the return value of a child process). So could also try shared memory, though it can be somewhat challenging (look into Boost.Interprocess if you wish to).
好吧,在多进程应用程序中,您将使用管道/文件将信息从一个进程传输到另一个进程(甚至可能是子进程的返回值)。所以也可以尝试共享内存,尽管它可能有点挑战性(如果你愿意,可以看看 Boost.Interprocess)。
In a multi-threaded application you basically have the same options available:
在多线程应用程序中,您基本上可以使用相同的选项:
- you can use memory that is shared (provided you synchronize access)
- you can use queues to pass information from one thread to another (such as with pipes)
- 您可以使用共享的内存(前提是您同步访问)
- 您可以使用队列将信息从一个线程传递到另一个线程(例如使用管道)
so really the two are quite similar.
所以这两者真的很相似。
Following Tony Hoare's precept, you should generally share by communicating, and not communicate by sharing, which means privileging queues/pipes to shared memory; however for just a boolean flag, shared memory can prove easier to put in place:
遵循 Tony Hoare 的戒律,你通常应该通过通信来共享,而不是通过共享来通信,这意味着将队列/管道授予共享内存的特权;然而,对于一个布尔标志,共享内存可以证明更容易实现:
void do_a(std::atomic_bool& done) {
// do things
done = true;
}
int main() {
std::atomic_bool done = false;
auto a = std::async(do_a, done);
auto b = std::async([](std::atomic_bool& done) {
while (not done) {
std::cout << "still not done" << std::endl;
sleep(1);
}
});
// other stuff in parallel.
}
And of course, you can even put this flag in a std::shared_ptr
to avoid the risk of dangling references.
当然,您甚至可以将此标志放在 a 中std::shared_ptr
以避免悬空引用的风险。