如何匿名声明std :: thread? [英] How can I declare an std::thread anonymously?

查看:140
本文介绍了如何匿名声明std :: thread?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下简短程序:

#include <thread>

int Foo() {
while (1);
}

int main(){
    std::thread t(Foo);
    std::thread s(Foo);

    // (std::thread(Foo));

    t.join();
}

它将使用

g++ -Wl,--no-as-needed DoubleBufferTest.cc -o DoubleBufferTest -std=c++0x -pthread

在注释行中,我试图使用此处以匿名方式声明新线程.但是,当将该行注释回时,我可以编译,但运行时会出现以下错误:

In the commented out line, I am trying to use the technique described here to declare a new thread anonymously. However, when that line is commented back in, I can compile but running gives the following error:

terminate called without an active exception            
Aborted (core dumped)                                   

如何正确地匿名声明线程?

How can I correctly declare a thread anonymously?

注意,我在g++ 4.4.7上.

推荐答案

您可以这样做:

std::thread(Foo).detach();

这篇关于如何匿名声明std :: thread?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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