【发布时间】:2014-12-05 11:48:41
【问题描述】:
我正在尝试一些简单的boost::thread代码,如下:
#include <iostream>
#include <boost/thread.hpp>
void InputLoop()
{
std::cout << "Loop start" << std::endl;
int y = 0;
while (1)
{
std::cout << "y = " << y << std::endl;
y++;
}
std::cout << "Loop end" << std::endl;
}
int main()
{
std::cout << "Main start" << std::endl;
boost::thread t(InputLoop);
t.start_thread();
while (1)
{
int x = 0;
}
std::cout << "Main end" << std::endl;
return 0;
}
这给出了输出:
Main start
Loop start
y = 0
y = 1
y = 2
.
.
.
The program has unexpectedly finished
所以,它在InputLoop() 期间崩溃了。发生崩溃时y的值在不同的运行中有所不同,范围从大约0到大约10000。
发生了什么事?
【问题讨论】:
-
它在coliru.stacked-crooked.com 上运行。虽然,没有 t.start_thread();并带有“警告:未使用的变量'x' [-Wunused-variable] int x = 0;”
-
我需要升级或降级我的 boost-pick。因为
start_thread在我的平台上声明private访问权限(OSX 10.7.1) -
@WhozCraig - 抱歉,已编辑,忘记提及 - "/usr/local/include/boost/thread/detail/thread.hpp:177:14: error: 'void boost::thread: :start_thread()' 是私有的 void start_thread()"。只是想指出它不会“意外崩溃”。
标签: c++ boost boost-thread