嗨,我正在尝试使用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)
我试过 …
我有最简单的代码:
#include <iostream>
#include <thread>
void worker()
{
std::cout << "another thread";
}
int main()
{
std::thread t(worker);
std::cout << "main thread" << std::endl;
t.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
虽然我仍然无法编译它g++来运行.
更多细节:
$ g++ --version
g++ (Ubuntu/Linaro 4.8.1-10ubuntu8) 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 or FITNESS FOR A PARTICULAR PURPOSE.
Run Code Online (Sandbox Code Playgroud)
编译命令:
$ g++ main.cpp -o main.out -pthread -std=c++11
Run Code Online (Sandbox Code Playgroud)
运行:
$ …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) 我有一个类似于C++ Threads之前的错误 ,std :: system_error - 不允许操作?
我正在使用完全相同的源代码并进行编译
g++ ../src/main.cpp -pthread -std=c++11
Run Code Online (Sandbox Code Playgroud)
工作没有任何问题.
因为我想在更大的项目中使用线程,所以我必须使用CMake的线程.在搜索解决方案后,我发现了几个代码,例如:
cmake_minimum_required (VERSION 2.6)
project (Test)
add_definitions("-std=c++11")
find_package (Threads)
add_executable (main src/main.cpp)
target_link_libraries (main ${CMAKE_THREAD_LIBS_INIT})
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)
我的错是什么?
CMake输出看起来很有希望:
-- The C compiler identification is GNU 4.8.2
-- The CXX compiler identification is GNU 4.8.2
-- Check for working C compiler: /usr/bin/cc
-- Check for working …Run Code Online (Sandbox Code Playgroud) 您好,我在C++ 11中遇到线程问题.我有使用g ++ 4.8.1的ubuntu 64bit 13.10(测试).我试着编译代码:
#include <thread>
void func()
{
// do some work
}
int main()
{
std::thread t(func);
t.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
带选项:-std = c ++ 11 -pthread -lpthread.编译成功,但当我尝试运行它时,我收到一个错误:
在抛出'std :: system_error'的实例后调用terminate
():启用多线程以使用std :: thread:不允许操作
我写了一个简单的代码来测试threading在C++11。但是我不能在 中运行它Clion IDE,它在红线上给出以下错误。
terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread: Operation not permitted
Run Code Online (Sandbox Code Playgroud)
进程以退出代码结束134。
关于我的系统的一些细节:
我想代码对于解决这个问题不是那么重要,但我把它贴在这里:
#include <iostream>
#include <thread>
using namespace std;
void task1 () {
cout << "Task 1::"<<endl;
}
int main() {
thread t1(task1);
t1.join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)