gpt4 book ai didi

c++ - 在 Linux 中构建 C++11 线程化翻译单元

转载 作者:太空狗 更新时间:2023-10-29 12:18:09 26 4
gpt4 key购买 nike

<分区>

给定一个使用 C++11 线程特性的简单程序:

#include <iostream>
#include <thread>

using namespace std;

void dont_thread_on_me() {
cout << "action from another thread" << endl;
}

int main() {
thread t { dont_thread_on_me };
t.detach();

cin.get();
}

如果我使用以下方法构建应用程序:

c++ -std=c++11 Program.cpp -o Program.out

程序 构建 很好,但是当我运行它时,我得到:

./Program.out
terminate called after throwing an instance of 'std::system_error'
what(): Operation not permitted
Aborted

如果我使用 -pthread 构建它,如下所示:

c++ -std=c++11 Program.cpp -o Program.out -pthread

程序运行良好。我还没有看到任何其他需要特殊构建标志的 C++11 功能,为什么是这个?

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com