【问题标题】:C++ - thread execution failsC++ - 线程执行失败
【发布时间】:2015-03-24 14:07:56
【问题描述】:

我正在尝试运行一个函数,该函数将让调用线程在给定时间内休眠。但是当我运行它时,我得到了错误libc++abi.dylib: terminating. Abort trap: 6

睡眠功能:

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

我将线程定义如下:

thread p1(process_for, 2000);

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

【问题讨论】:

  • 你忘记加入你的话题了。

标签: c++ multithreading pthreads


【解决方案1】:

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

添加

p1.join();

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

【讨论】:

    【解决方案2】:

    本程序编译运行成功

    #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();
    }
    

    你好像忘记加入话题了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-25
      • 1970-01-01
      • 2010-12-21
      • 1970-01-01
      • 1970-01-01
      • 2023-03-13
      相关资源
      最近更新 更多