这是我的代码:
#include <thread>
#include <chrono>
using namespace std::literals::chrono_literals;
#include <iostream>
void f(int n)
{
for (int cnt = 0; cnt < n; ++cnt) {
std::this_thread::sleep_for(1s);
std::cout << "." << std::flush;
}
std::cout << std::endl;
}
int main()
{
std::thread t1 = std::thread(f, 5);
//t1.join();
t1 = std::thread(f, 5); // <- should abort if t1.join() is not done
t1.join();
}对于该网站,我使用gcc9.2执行器来查看未连接线程被销毁时会发生什么,但这是编译器输出选项卡的内容:
Could not execute the program
Compiler returned: 1
Compiler stderr
/tmp/ccjlEO57.o: In function `std::thread::thread<void (&)(int), int&, void>(void (&)(int), int&)':
/opt/compiler-explorer/gcc-9.2.0/include/c++/9.2.0/thread:126: undefined reference to `pthread_create'
collect2: error: ld returned 1 exit status同时-当我在编译器选项中添加"-lpthread“时.编辑框中,我得到一个不同的错误:
Program returned: 255
Program stderr
terminate called after throwing an instance of 'std::system_error'
what(): Resource temporarily unavailable请注意,对于第二次运行,第一个t1.join()没有被注释掉(因此它应该运行良好)。
这是否意味着你不能在其他令人难以置信的令人敬畏的godbolt.org网站上测试任何与godbolt.org相关的内容?
发布于 2019-10-30 16:44:04
在所有类似的情况下,线程创建都是故意在godbolt.org上禁用的(以防止拒绝服务攻击或其他滥用),因此目前无法在该服务上使用std::线程。
发布于 2021-05-07 16:00:05
站点现在支持线程。将-pthread添加到编译器参数中。
https://stackoverflow.com/questions/58629747
复制相似问题