C ++线程异步同时运行 [英] c++ thread asynchronous running simultaneously

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

问题描述

我是C ++ 11中线程的新手.我有两个线程,我想使它们在同一时间启动.我可以想到两种方法(如下所示).但是,似乎它们都不如我预期的那样工作.它们是在启动另一个线程之前启动一个线程.任何提示将不胜感激!另一个问题是我正在处理线程队列.因此,我将有两个消费者和四个生产者.以下针对消费者的代码是正确的方法吗?有没有人可以提供的参考?

I'm new to thread in C++ 11. I have two threads and I want to make them start at the exact same time. I could think of two ways of doing it (as below). However, it seems that none of them work as I expected. They are start one thread before launching another. Any hint would be appreciated! Another question is I'm working on a threaded queue. So I would have two consumers and four producers. Is the following code for consumer the right way to go? Is there any reference that anyone can provide?

for(int i = 1; i <= 2; i++)
    auto c = async(launch::async, consumer, i);


auto c1 = async(launch::async, consumer, 1);
auto c2 = async(launch::async, consumer, 2);

推荐答案

其他答案关于无法保证两个线程同时启动的说法是正确的.不过,如果您想接近一点,则有不同的方法可以做到这一点.

What the other answers said about it not being possible to guarantee that two threads start at the same time is true. Still, if you want to come close there are different ways to do that.

一种方法是使用一组std :: promises指示何时一切就绪.每个线程都设置一个承诺,表示已准备就绪,然后等待从第三个std :: promise获得的std :: shared_future(副本).主线程等待所有线程的所有promise都被设置,然后触发线程执行.这样可以确保每个线程均已启动,并且在应同时运行的代码块之前.

One way is to use a set of std::promises to indicate when everything is ready. Each thread sets a promise to indicate that it's ready and then waits on a (copy of a) std::shared_future obtained from a third std::promise; the main thread waits for all the promises from all the threads to be set and then triggers the threads to go. This ensures that each thread has started and is just before the chunk of code that should be run concurrently.

std::promise<void> go, ready1, ready2; // Promises for ready and go signals
std::shared_future<void> ready(go.get_future()); // Get future for the go signal
std::future<void> done1, done2; // Get futures to indicate that threads have finished
try
{
    done1 = std::async(std::launch::async, 
        [ready, &ready1]
    {
        ready1.set_value(); // Set this thread's ready signal
        ready.wait(); // Wait for ready signal from main thread
        consumer(1);
    });
    done2 = std::async(std::launch::async,
        [ready, &ready2]
    {
        ready2.set_value(); // Set this thread's ready signal
        ready.wait(); // Wait for ready signal from main thread
        consumer(2);
    });
    // Wait for threads to ready up
    ready1.get_future().wait();
    ready2.get_future().wait();
    // Signal threads to begin the real work
    go.set_value();
    // Wait for threads to finish
    done1.get();
    done2.get();
}
catch (...)
{
    go.set_value(); // Avoid chance of dangling thread
    throw;
}

注意:大部分答案是从Anthony Williams的"C ++ Concurrency in Action"(第311-312页)中复制的,但是我修改了代码以使其适合问题中的示例.

Note: most of this answer was copied from "C++ Concurrency in Action" by Anthony Williams (pages 311-312), but I adapted the code to fit the example in the question.

这篇关于C ++线程异步同时运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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