首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在网站上: godbolt.org,只有在那里:我如何使用std::线程?

在网站上: godbolt.org,只有在那里:我如何使用std::线程?
EN

Stack Overflow用户
提问于 2019-10-30 16:19:39
回答 2查看 1.9K关注 0票数 7

这是我的代码:

代码语言:javascript
复制
#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执行器来查看未连接线程被销毁时会发生什么,但这是编译器输出选项卡的内容:

代码语言:javascript
复制
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“时.编辑框中,我得到一个不同的错误:

代码语言:javascript
复制
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相关的内容?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-10-30 16:44:04

在所有类似的情况下,线程创建都是故意在godbolt.org上禁用的(以防止拒绝服务攻击或其他滥用),因此目前无法在该服务上使用std::线程。

票数 4
EN

Stack Overflow用户

发布于 2021-05-07 16:00:05

站点现在支持线程。将-pthread添加到编译器参数中。

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58629747

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档