本文介绍了向量的std :: threads的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 11

我想让一个向量 std :: thread s。以下三点的结合说明我可以。

I am trying to make a vector of std::threads. The combination of the following three points says I can.

1。)根据,
线程' s默认构造函数创建一个不代表线程的

1.) According to http://en.cppreference.com/w/cpp/thread/thread/thread, thread’s default constructor creates a

2。)根据,线程 operator = / p>

2.) According to http://en.cppreference.com/w/cpp/thread/thread/operator%3D, thread’s operator=

3。)根据
,将
只传递一个大小类型变量到一个向量构造函数将构造

3.) According to http://en.cppreference.com/w/cpp/container/vector/vector, passing only a size type variable to a vector constructor will construct

所以,我这样做:

#include <iostream>
#include <thread>
#include <vector>

void foo()
{
    std::cout << "Hello\n";
    return;
}

int main()
{
    std::vector<std::thread> vecThread(1);
    vecThread.at(0) = std::thread(foo);
    vecThread.at(0).join();
    return 0;
}

这在VC11和,如下所示:

This runs as expected in VC11 and g++ 4.8.0 (online compiler here) as seen in the following:

控制台输出:

Hello

然后我在clang 3.2中通过在同一个网页上切换编译器菜单进行了尝试:

stderr:
pure virtual method called
terminate called without an active exception

当代表线程的线程对象在 join()之前超出范围 ed或 detach() ed,程序将被强制终止。我有 join() ed vecThread.at(0),所以唯一留下的问题是临时线程

When a thread object—that represents a thread—goes out of scope before being join()ed or detach()ed, the program will be forced to terminate. I have join()ed vecThread.at(0), so the only thing left in question is the temporary thread

std :: thread(foo);

vecThread.at(0)= std :: thread(foo);

分配。

但是,根据Web引用,线程只能通过移动线程右值引用来分配。我不能想到任何方式到 join() detach()一个临时线程对象。

However, according to the web reference, threads can only be assigned by moving a thread rvalue reference. I cannot think of any way to join() or detach() a temporary thread object.

所以如果clang的输出是正确的,那么线程的用法是 operator =

So if clang’s output is correct, then what is the use of thread’s operator=? Or is this a clang compiler bug?

在g ++ 4.8.0中,更改行

In g++ 4.8.0, changing the line

vecThread.at(0)= std :: thread(foo)

vecThread.at(0)= std :: thread {foo}

大括号)仍然给出预期的 Hello 输出。

(replacing parentheses with braces) still gives the expected Hello output.

但是,将行更改为 vecThread.at(0)= {foo} 让它抱怨:

However, changing the line to vecThread.at(0) = {foo} makes it complain:

g ++ 4.8.0对大括号的投诉

不知道它是什么意思。

在clang中进行同样的更改会更加高级:

Making the same change in clang gives the even more advanced:

clang 3.2对大括号的投诉

clang 3.2's complaint on braces:

error: no viable overloaded '='
vecThread.at(0) = {foo};
...
note: candidate function not viable: cannot convert initializer list
argument to 'const std::thread'
thread& operator=(const thread&) = delete;
...
note: candidate function not viable: cannot convert initializer list
argument to 'std::thread'
thread& operator=(thread&& __t) noexcept

我不知道这是什么意思。

and I don’t know what that means either.

我无法使用VC11来证实上述

I cannot use VC11 to corroborate the above

vecThread.at(0) = {foo}

问题,因为VC11在2012年11月的CTP编译器不支持标准库上的统一初始化语法。 / p>

problems because VC11, as of the November 2012 CTP compiler, does not support uniform initialization syntax on the Standard Library.

推荐答案

您的第一个例子是正确的。抛出异常是一个已知的,当您使用clang与libstdc ++。要解决它,你必须安装libc ++(llvm版本的c ++库)。请参阅下面的使用libc ++编译的示例

Your first example is correct. Throwing an exception is a known bug, when you using clang with libstdc++. To solve it, you have to install libc++(llvm version of c++ library). See an example of compiling with libc++ below

#include <thread>

int main()
{
    std::thread t([] () {});
    t.join();
    return 0;
}

$ clang ++ -std = c ++ 11 - stdlib = libc ++ main.cpp -o main -lc ++ -lsupc ++ -lpthread

PS请参见,为什么是 -lsupc ++

P.S. See here, why is the flag -lsupc++ required too.

这篇关于向量的std :: threads的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 03:35
查看更多