线程之间的C ++通信 [英] C++ communication between threads

查看:401
本文介绍了线程之间的C ++通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一对夫妇的类,每个打开不同的程序在不同的线程,并使用 CreateProcess (如果有一个更多的C ++定向的方式做这个让我知道 - 我看了)



有些类依赖于运行的其他程序之一。即如果A停止,B必须停止。我以前做了这段代码,然后我的解决方案是有一个类,静态函数运行各种程序和静态成员变量保持他们的状态。我也使用 CreateThread



回想起来,这种方法似乎...脆弱和尴尬的看。我不知道如果使用这样的静态类是好的做法或不(特别是回忆如何尴尬地初始化状态成员变量)。我想让每个类包含自己的运行函数。我考虑的问题是如何让B类知道A是否尴尬停止。他们仍然需要知道一种方式来了解彼此的状态。注意,我想在这个返工中使用 std :: thread ,并且我没有多线程的经验。感谢您的帮助。

解决方案

在多进程应用程序中,您将使用管道/文件从一个过程到另一个(或者甚至可能是子进程的返回值)。所以也可以尝试共享内存,虽然它可能有点具有挑战性(看看Boost.Interprocess如果你愿意)。



在多线程应用程序中,相同的选项:




  • 您可以使用共享的内存(如果您同步访问)

  • 您可以使用队列将信息从一个线程传递到另一个线程(例如使用管道)



,因此两者非常相似。 / p>

按照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<仍未完成<< std :: endl;
sleep(1);
}
});

//其他东西并行。
}

当然,你甚至可以把这个标志放在 std :: shared_ptr ,以避免引用暂停的风险。


I have a 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)

Some of the classes are dependent on one of the other programs running. ie B must stop if A stopped. I made this code awhile 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.

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

解决方案

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

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:

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.

这篇关于线程之间的C ++通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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