C++11 std::线程联接崩溃,Xcode 6上出现system_error异常和SIGABRT

C++11 std::thread join crashes with system_error exception and SIGABRT on Xcode 6?

本文关键字:system 异常 SIGABRT error 线程 std 崩溃 Xcode C++11      更新时间:2023-10-16

这里有一个简单的线程跟踪程序。线程只需打印前十个整数,然后打印"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::线程在构造后开始执行。因此,分离是不必要的。

相关文章: