8

I have a similar error like another had before C++ Threads, std::system_error - operation not permitted?

I am using exactly the same source code and compiling with

g++ ../src/main.cpp -pthread -std=c++11

works without any problem.

Since I want to use threads in a larger project I have to use threads with CMake. After searching for solutions I found several codes for example:

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})

But for me it's not working I get always:

terminate called after throwing an instance of 'std::system_error'
  what():  Enable multithreading to use std::thread: Operation not permitted
Aborted (core dumped)

What is my mistake?

The CMake output looks promising:

-- 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 C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Looking for include file pthread.h
-- Looking for include file pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE  
-- Configuring done
-- Generating done
Community
  • 1
  • 1
Julian
  • 416
  • 1
  • 4
  • 8

2 Answers2

22

EDIT: I am now using gcc 5.4, the CMake snippet in question just work fine.

I just faced the same problem.My final CMakeLists.txt, which works, is as follows (note - it will work for Windows as well, i.e.- using Visual Studio):

cmake_minimum_required (VERSION 2.6)
project (Test)
SET(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-std=c++11 -pthread")

find_package (Threads)
add_executable (main src/main.cpp)
target_link_libraries (main ${CMAKE_THREAD_LIBS_INIT})

EDIT 2: As of CMake 3.2, there is an IMPORTED target you link to instead:

cmake_minimum_required (VERSION 3.2)
project (Test)
SET(CMAKE_CXX_STANDARD 11)

find_package (Threads)
add_executable (main src/main.cpp)
target_link_libraries (main Threads::Threads)

(it might be 3.1.3, but I cannot find the change in the release notes)

Botje
  • 26,269
  • 3
  • 31
  • 41
prehistoricpenguin
  • 6,130
  • 3
  • 25
  • 42
  • +1 had the same issue, all the "standard" ways of enabling multithreading in gnu c++ did not work when cross-compiling for my raspberry pi. This snippet, however, solved the issues. Thanks – birgersp Feb 09 '16 at 22:32
  • @BirgerSkogengPedersen Glad to hear that – prehistoricpenguin Feb 16 '16 at 07:14
  • Note that this snippet is linking the same lib in two different ways. The ```-pthread``` works by itself, or the ```find_package``` + ```target_link_libraries``` calls. Only one of those two methods is needed (I would say the latter is preferred). – Chuck Claunch Feb 21 '17 at 19:14
  • 1
    @ChuckClaunch Maybe for lower gcc version(such like 4.8), we should use cmake in some way as my snippet showing. I tried on gcc 5.4 a few seconds ago, both `-pthread` or `find_package + target_link_libraries` runs smoothly. – prehistoricpenguin Feb 22 '17 at 06:42
  • @prehistoricpenguin Right, both work, however it's redundant to use both at the same time. And you must be careful because ```-pthread``` could potentially link against a different library than ```find_package``` depending on your system and CMake environment. For what it's worth, the cmake way of using ```find_package``` + ```target_link_libraries``` works perfectly in a cross-platform application I'm working on right now. I've shown it to work in Ubuntu 15.04, CentOS 7, and Windows 10. All without the ```-pthread``` flag set or any extra ```if```s related to Windows. – Chuck Claunch Feb 22 '17 at 16:26
  • @ChuckClaunch Please read the question carefully, when working with `gcc 4.8.2`, there are problems without `pthread` flag. – prehistoricpenguin Feb 23 '17 at 09:13
  • @prehistoricpenguin I read the question just fine. Nowhere on this page (nor in the original question) is gcc mentioned except for you saying in a comment that "maybe" gcc 4.8 could operate differently. What I'm telling you is the two methods of linking the library are redundant. If you need to use the ```-pthread``` option for it to work, fine, then remove the CMake way of doing it. You absolutely do not need both. – Chuck Claunch Feb 23 '17 at 20:08
  • @ChuckClaunch We can see `The C compiler identification is GNU 4.8.2` in the question, so Julian was using GCC 4.8.2 at the time. He meet problem without the optinon `-pthread`. I am now using GCC 5.4 so I can not reproduce it. – prehistoricpenguin Feb 24 '17 at 03:08
  • @prehistoricpenguin ok I see that in his output. Either way it still found the threading library and successfully linked. He had a runtime problem. It's more important to someone troubleshooting to learn what happened w/the correct way (finding out what the ```find_package``` call did, rather than just assuming the system pthread was ok. It's a band-aid to just link it with a gcc def. Sounds like OP had a permission issue or might have had multiple threading libraries installed. – Chuck Claunch Feb 24 '17 at 14:51
  • @ChuckClaunch I think there maybe bugs within gcc 4.8, for the deference with `-pthread` and `-lpthread`, please refer http://stackoverflow.com/questions/23250863/difference-between-pthread-and-lpthread-while-compiling, there are indeed some difference. – prehistoricpenguin Feb 25 '17 at 08:27
  • @prehistoricpenguin There may indeed be a bug (though I don't see where you conclude that based on the reference), I simply don't want someone looking at this answer as the correct way to use the threading library in a CMake project. I will leave it at that. – Chuck Claunch Feb 26 '17 at 14:19
  • 1
    @ChuckClaunch I will edit the answer to add some supplement, thank you! – prehistoricpenguin Feb 27 '17 at 01:30
1

In my case (Linux desktop), setting the flag is enough:

cmake_minimum_required (VERSION 2.6)
PROJECT (threads_tests)
SET(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-std=c++11 -pthread")
ADD_EXECUTABLE(threads_1 threads_1.cpp)

but explicitly finding the library and linking with it also works and may be necessarily for some crosscompile situations with embedded platforms.