嗨,我正在尝试使用std::threadG ++.这是我的测试代码
#include <thread>
#include <iostream>
int main(int, char **){
std::thread tt([](){ std::cout<<"Thread!"<<std::endl; });
tt.join();
}
Run Code Online (Sandbox Code Playgroud)
它编译,但当我尝试运行它时,结果是:
terminate called after throwing an instance of 'std::system_error'
what(): Operation not permitted
Aborted
Run Code Online (Sandbox Code Playgroud)
我的编译器版本:
$ 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.
Run Code Online (Sandbox Code Playgroud)
我的测试代码出了什么问题?
更新:我使用以下命令行来编译和运行我的代码.
$ g++ -std=c++0x test.cpp
$ ./a.out
Run Code Online (Sandbox Code Playgroud)
我试过 …
所以我写了一个程序来测试64位kubuntu linux上的线程,版本13.04.实际上我从正在编写测试程序的其他人那里抢了代码.
#include <cstdlib>
#include <iostream>
#include <thread>
void task1(const std::string msg)
{
std::cout << "task1 says: " << msg << std::endl;
}
int main(int argc, char **argv)
{
std::thread t1(task1, "Hello");
t1.join();
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
我编译使用:
g++ -pthread -std=c++11 -c main.cpp
g++ main.o -o main.out
Run Code Online (Sandbox Code Playgroud)
然后跑了:
./main.out
Run Code Online (Sandbox Code Playgroud)
顺便说一下,当我使用-l'时,main.out会像所有可执行文件一样以绿色文本显示,但在其名称的末尾也有一个星号.为什么是这样?
回到手头的问题:当我运行main.out时,出现了一个错误,其中说:
terminate called after throwing an instance of 'std::system_error'
what(): Operation not permitted
Aborted (core dumped)
Run Code Online (Sandbox Code Playgroud)
任何有关如何解决此问题的想法?
我在我的系统上创建并执行了一个简单的线程.当我执行这个程序时,我有错误消息:启用多线程使用std :: thread:不允许操作
关于我的系统的一些细节:
我编译包括库的源代码 pthread
源代码:
#include <iostream>
#include <thread>
using namespace std;
void func(void) {
cout << "test thread" << endl;
}
int main() {
cout << "start" << endl;
thread t1 (func);
t1.join();
cout << "end" << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我目前正在尝试在一个共享库中使用C++ 11多线程,该库被加载到Linux上的主程序(用C语言编写)中.这是大型模拟程序的一部分,我无法改变有关库加载或更改主程序的任何内容.
主程序是用gcc 4.1.2编译的,我没有它的源代码(我不能用gcc 4.8.2重新编译它).
共享库使用gcc 4.8.2编译,以便使用C++ 11多线程.我正在传递编译器命令
-pthread -lpthread -std=c++11
Run Code Online (Sandbox Code Playgroud)
正如在Linux下的GCC中使用std :: thread的正确链接选项中所解释的那样?
使用此配置(" -pthread -std=c++11"和gcc 4.8)编译独立测试程序在我的系统上正常工作.但是当我启动加载共享库的程序时,我得到一个异常:
Caught std::exception!
Exception Message: Enable multithreading to use std::thread: Operation not permitted
Terminating...
Run Code Online (Sandbox Code Playgroud)
使用-pthread和-lpthread(编辑:也只是-pthread没有-lpthread)编译参数不起作用.编译器参数是(我正在使用cook构建系统):
-pthread -std=c++11 -fmessage-length=0 -fPIC -Wchar-subscripts ...(lots of -W* here)
... -Wunused-variable -m64 -D__64BIT__ -pthread -lpthread
Run Code Online (Sandbox Code Playgroud)
和链接器参数(由于构建系统而产生的重复参数):
-pthread -lpthread -std=c++11 -pthread -lpthread -std=c++11 -shared -fPIC -Wl,-Bsymbolic -Wl,--allow-shlib-undefined -pthread -lpthread
Run Code Online (Sandbox Code Playgroud)
在我的库上调用ldd会给出以下输出
$ ldd calc3/build/amd64_linux26_RH5/library.so
linux-vdso.so.1 => …Run Code Online (Sandbox Code Playgroud) 这不是一个重复的问题,因为提供的解决方案不适用于我的编译器.我试图从这个问题编译并运行以下示例.
#include <thread>
#include <iostream>
int main(int, char **){
std::thread tt([](){ std::cout<<"Thread!"<<std::endl; });
tt.join();
}
Run Code Online (Sandbox Code Playgroud)
我试图使用原始问题中提供的解决方案以及此复制品的已接受答案.但是,尽管我尝试了列出的所有组合,特别是尝试过
g++ main.cpp -o main.out -pthread -std=c++11
Run Code Online (Sandbox Code Playgroud)
当我运行生成的可执行文件时,我仍然得到
terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread: Operation not permitted
Aborted (core dumped)
Run Code Online (Sandbox Code Playgroud)
这是输出g++ --version.
g++ (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY …Run Code Online (Sandbox Code Playgroud) 我试图使用std :: thread,但是当我尝试运行它时我有这个错误.
抛出'std :: system_error'的实例后终止调用
what():启用多线程以使用std :: thread:不允许操作
中止(核心倾倒)
关于它有很多问题,每个答案都说类似的东西:我必须用"-pthread"或"-lpthread"构建.有些人还说要添加"-Wl, - no-as-needed".
我尝试了很多东西,但都没有用.
我正在使用Lubuntu上的Code :: Blocks 12.11,GNU GCC Compiler进行编译.在编译器设置菜单中,我检查了编译器标志
"让g ++遵循C++ 11 ISO C++语言标准[-std = c ++ 11]"
在其他选项中,我写了答案所说的,这是一个例子
-pthread
-Wl,--no-as-needed
Run Code Online (Sandbox Code Playgroud)
这是我的构建日志(我不确定它是否重要)
g++ -Wall -fexceptions -std=c++11 -g -pthread -Wl,--no-as-needed -std=c++11 -I../DeskManagerDll -I/usr/include/X11/extensions -I/usr/include/X11 -c /home/julien/Documents/test/main.cpp -o obj/Debug/main.o
g++ -L/home/julien/Documents/DeskManagerDll -L-L/usr/lib/i386-linux-gnu -o bin/Debug/test obj/Debug/main.o -L/usr/X11R6/lib -lX11 -lXext -lpthread -Wl,--no-as-needed /home/julien/Documents/DeskManagerDll/bin/Debug/libDeskManagerDll.so
Output size …Run Code Online (Sandbox Code Playgroud)