C++ - 线程执行失败

标签 c++ multithreading pthreads

我正在尝试运行一个函数,让调用线程在给定时间内进入休眠状态。但是当我运行它时,出现错误 libc++abi.dylib: terminating。中止陷阱:6

休眠功能:

void process_for(int cycles) {
    sleep(cycles);
}

我定义的线程如下:

thread p1(process_for, 2000);

我也试过 this_thread::sleep_for(chrono::milliseconds(1000)) 但这给了我同样的错误。我可能做错了什么?

最佳答案

正如评论所说。 你必须加入主线程

添加

p1.join();

最终看到: http://www.cplusplus.com/reference/thread/thread/join/

关于C++ - 线程执行失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29234784/

相关文章:

c# - 异步/等待中的重入?

c++ - 新版本的 g++ 的多线程性能下降?

c++ - 同步屏幕 I/O & pthread & C++

linux - CPU 亲和性掩码(将线程放在不同的 CPU 上)

c++ - Windows Linux C++编译问题: "no matching function for call"

c++ - Visual Studio 2010 Arduino cpp 错误 : argument of type "char *" is incompatible with parameter of type "LPCWSTR"

c++ - 静态数组类变量 "multiple definition"C++

c++ - #include<iostream> 存在,但我收到错误 : identifier "cout" is undefined. 为什么?

ios - iOS使用多线程保存数据

Java - 使用具有线程间通信的两个线程打印数字序列