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

Here is a simple thread trace program. The thread simply prints the first ten integers and then the message "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;
}

      

However, when it is executed, t.join throws a std :: __ 1 :: system_error exception, somewhere in the libc ABI, causing the program to terminate with 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

      

Sometimes when it fires an exception on the main thread (ibid.), Before thread t starts (but it still does):

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.

      

+3


source to share


3 answers


The problem is that both detach and join have a precondition that the stream is joinable, and both have post-order that joinable is false. This means that once you call one on the thread, the attempt to call the other is invalid.



Second, the different behavior you see has to do with the timing of the thread and the main function. Sometimes detach and connect don't get done until the thread is started, sometimes they get started before and everything in between.

+4


source


May be the result of trying to join those that have not been launched.

I was getting this error when I was entering an array for streams like this:

for (auto& th : threads) th.join();

      

I then rewrote it with a manual for a loop that didn't give me any errors:

for (i = 0; i< numthreads; i++)   
        threads[i] = thread(start,i+1);

      



I think because I declared the array like this:

    std::thread threads[MAXTHREADS];

      

And so it tried to join the ones that I didn't run.

Full code for reference:

#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 threads\n", 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 joined\n");

    return 0;
}

      

+1


source


The std :: thread line starts after construction. Thus, there is no need to detach.

-2


source







All Articles