简体   繁体   中英

C++ - thread execution fails

I am trying run a function which will let the calling thread go to sleep for a given time. But when I run it i get the error libc++abi.dylib: terminating. Abort trap: 6 libc++abi.dylib: terminating. Abort trap: 6

Sleep function:

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

And I defined the thread as below:

thread p1(process_for, 2000);

I also tried this_thread::sleep_for(chrono::milliseconds(1000)) but that gave me the same error. What am I possibly doing wrong?

as the comment said. you have to join the thread in the main

add

p1.join();

eventually see: http://www.cplusplus.com/reference/thread/thread/join/

This program compiles and runs successfuly

#include <thread>
#include <chrono>

void process_for(int cycles) 
{
    std::this_thread::sleep_for( std::chrono::milliseconds( cycles ) );
}

int main()
{
    std::thread p1( process_for, 2000 );

    p1.join();
}

It seems you forgot to join the thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM