C++11 std::线程联接崩溃,Xcode 6上出现system_error异常和SIGABRT
C++11 std::thread join crashes with system_error exception and SIGABRT on Xcode 6?
这里有一个简单的线程跟踪程序。线程只需打印前十个整数,然后打印"thread is done"消息。
#include <iostream>
#include <vector>
#include <numeric>
#include <thread>
void f();
int main(int argc, const char * argv[]) {
std::thread t(f);
std::cout << "Thread start" << std::endl;
t.detach();
t.join();
std::cout << "Thread end" << std::endl;
return 0;
}
void f()
{
std::vector<int> a(10);
std::iota(a.begin(), a.end(), 0);
for(const int& i : a)
{
std::cout << i << std:: endl;
}
std::cout << "Thread is done." << std::endl;
}
然而,当运行时,t.join在libc ABI中的某个位置抛出std::__1::system_error异常,导致程序以SIGABRT:终止
Thread start
0
1
2
3
4
5
6
7
8
9
Thread is done.
libc++abi.dylib: terminating with uncaught exception of type std::__1::system_error: thread::join failed: No such process
有时,当它运行时,主线程中的异常在线程t运行之前发生(在同一位置)(但它仍然发生):
Thread start
libc++abi.dylib: terminating with uncaught exception of type std::__1::system_error: thread::join failed: No such process
0
1
2
3
4
5
6
7
8
9
Thread is done.
问题是分离和连接都有一个线程可连接的先决条件,并且两者都有可连接为false的后置条件。这意味着,一旦在线程上调用一个,尝试调用另一个是无效的。
其次,您看到的不同行为是由于线程和主函数的执行时间。有时分离和连接直到线程运行之后才执行,有时它们在之前运行,以及介于两者之间的任何操作。
可能是尝试连接未启动的线程的结果。
当我加入这样一个线程的数组时,我得到了这个错误:
for (auto& th : threads) th.join();
然后我重新编写了一份循环手册,没有给我任何错误:
for (i = 0; i< numthreads; i++)
threads[i] = thread(start,i+1);
我想这是因为我这样声明数组:
std::thread threads[MAXTHREADS];
所以它试图连接我还没有开始的线程。
完整参考代码:
#include <sched.h>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <thread> // std::thread
#include <mutex> // std::mutex
using namespace std;
mutex mtx; // mutex for critical section
#define MAXTHREADS 10
#define MAXTIMES 1
int data[MAXTHREADS];
int start(int id) {
int stride = 64, dummy;
mtx.lock();
for(int times = 0; times < MAXTIMES; times++) {
for (int i = 0; i < MAXTHREADS; i = i + 1) {
//dummy = data[i]; //sim a read from every slot in the array
cout << data[i] << ", ";
}
cout << endl;
}
mtx.unlock();
return 0;
}
int main()
{
std::thread threads[MAXTHREADS];
int i;
int numthreads = 6;
for(int i = 0; i < MAXTHREADS; i++)
data[i] = i;
printf("Creating %d threadsn", numthreads);
for (i = 0; i< numthreads; i++)
threads[i] = thread(start,i+1);
for (i = 0; i< numthreads; i++)
threads[i].join();
//for (auto& th : threads) th.join();
printf("All threads joinedn");
return 0;
}
std::线程在构造后开始执行。因此,分离是不必要的。
相关文章:
- 处理多个异常集合的C++方法
- 我在c++代码中生成了一个运行时#3异常
- 孤立代码块在结构中引发异常
- C++中的赋值发生,尽管右侧出现异常
- 从构造函数抛出异常时如何克服内存泄漏
- C++/CLI System.AccessViolation在托管类中调用非托管函数时出现异常
- 来自 Android 应用程序内部的 boost 类型的 boost::wrapexcept<boost::system::system_error> 的未捕获异常
- std::system 实例化单一实例对象时的异常
- 在 C# 中加载 COM 对象会引发异常"无法将类型'System.__ComObject'的 COM 对象强制转换为接口类型...",但C++或 VB 不会
- 尝试使用 fread 读取文本文件时发生 'System.AccessViolationException' 类型的未处理异常
- WP8.1 发布内部版本异常的原因:"System.IO.FileNotFoundException: The specified module could not be found" ?
- 从c#中的线程调用c++函数时发生system.access.violation异常
- System.IO.FileNotFoundException未经处理消息=“找不到指定的模块.(HRESULT中的异常
- 推力异常:"thrust::system::system_error at memory location 0x00000000"
- XXX 中发生了类型 'System.ExecutionEngineException' 的未处理异常.exe
- 什么是类型为"System.Runtime.InteropServices.SEHException"的未处理异常
- OpenCV 代码中'System.Runtime.InteropServices.SEHException'未处理的异常
- System类型的未处理异常.my.dll中出现StackOverflow异常
- 类型为"System.Reflection.TargetParameterCountException"的未处理异常
- System.Runtime.Serialization.Serialization异常出现问题