本文介紹了在 linux 下的 GCC 中使用 std::thread 的正確鏈接選項是什么?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在嘗試將 std::thread
與 G++ 一起使用.這是我的測試代碼
Hi I am trying to use std::thread
with G++. Here is my test code
#include <thread>
#include <iostream>
int main(int, char **){
std::thread tt([](){ std::cout<<"Thread!"<<std::endl; });
tt.join();
}
它可以編譯,但是當我嘗試運行它時,結果是:
It compiles, but when I try to run it the result is:
terminate called after throwing an instance of 'std::system_error'
what(): Operation not permitted
Aborted
我的編譯器版本:
$ g++ --version
g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
我的測試代碼有什么問題?
What is wrong with my test code?
更新:我使用以下命令行來編譯和運行我的代碼.
UPDATE: I use the following command line to compile and run my code.
$ g++ -std=c++0x test.cpp
$ ./a.out
我試過了
$ g++ -std=c++0x -lpthread test.cpp
$ ./a.out
還是一樣.
推薦答案
我覺得在Linux上pthread是用來實現std::thread
的,所以需要指定-pthread
代碼>編譯器選項.
I think on Linux pthread is used to implement std::thread
so you need to specify the -pthread
compiler option.
因為這是一個鏈接選項,所以這個編譯器選項需要在源文件之后:
As this is a linking option, this compiler option need to be AFTER the source files:
$ g++ -std=c++0x test.cpp -pthread
這篇關于在 linux 下的 GCC 中使用 std::thread 的正確鏈接選項是什么?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!