Visual Studio 2013标准::线程
Visual Studio 2013 std::thread
当使用Visual Studio 2013进行编译时,以下程序会给出一些奇怪的编译/运行时行为:
#include "stdafx.h"
#include <thread>
#include <chrono>
#include <iostream>
int main()
{
{//works
std::thread([](){
std::cout << " thread running1n";
});
}
{//compile but synstax error exist
auto func = [](){
std::cout << " thread running2n";
};
std::thread(fun); //fun is not defined
}
{//compile, see next block for mystery compile error
auto func = [](){
std::cout << " thread running2n";
};
std::thread tmp(func);
}
{//does not compile and don't know why
auto func = [](){
std::cout << " thread running2n";
};
std::thread(func); //error C2371: 'func' : redefinition; different basic types
}
return 0;
}
当这个程序工作时,由于线程之间存在竞争条件,可能会崩溃。主线程可以先于其他线程结束。
有人知道为什么第二块和最后一块不起作用吗?
{//compile but synstax error exist
auto func = [](){
std::cout << " thread running2n";
};
std::thread(fun); //fun is not defined
}
这里没有语法错误,std::thread(fun)
默认构造一个名为fun
的std::thread
对象。
最后一个块中的错误是由于相同的原因
std::thread(func); //error C2371: 'func' : redefinition; different basic types
您试图在上面默认构造一个名为func
的std::thread
对象,这是一个错误,因为同一范围中已经存在同名的lambda。要将lambda传递给thread
构造函数,请使用大括号而不是
std::thread{func};
现在,在您进行了这些更改之后,您的代码将进行编译,但在运行时会失败,因为块1、3和4中的线程对象;4都将调用std::terminate
(当然,当第一个线程对象调用std::terminate
时,程序就会终止,所以其他两个这样做是没有意义的)。
发生这种情况的原因是,在所有3个块中都有可连接的线程,如果运行这样一个线程对象的析构函数,就会调用std::terminate
。为了避免这种情况,您必须拨打thread::join
(您也可以拨打thread::detach
,但不要这样做)。
例如
{//works
std::thread([](){
std::cout << " thread running1n";
}).join();
}
实时演示
相关文章:
- 如果不包含 pthread,为什么 GCC 的线程标准库实现会抛出异常?
- 标准 cpp 数据结构和线程互斥锁?
- 标准::p set_value和线程安全
- C++ 线程时出错,标准::调用:
- C++ 杀死一个标准::线程
- 什么时候标准::线程::连接会因no_such_process而失败
- 将线程之间的标准输入/输出重定向
- 组合来自多个线程的数据的标准方法?
- 可以在构造函数初始值设定项列表中使用标准::线程
- 包装标准::线程调用函数
- 标准::atomic_应该如何...<std::shared_ptr>用于线程安全类的复制和移动操作?
- 是否使用符合标准的线程池进行 std::async 的 Visual C++ 实现
- 为什么Win API线程执行函数,而标准线程则没有
- 是否可以使用标准的C 线程而不是FLTK超时来更新窗口
- 标准C++11是否保证std::async(std::launch::asyncfunc)在单独的线程中启动func
- 代码::块无法识别标准::线程
- win32 标准::线程泄漏内存
- C++标准库容器相对于所包含对象的线程安全性
- 标准::线程::id的要求.可以雾化吗?
- C++ 标准::线程"Attempt to use a deleted function"