0

I write a code in C++ (KDevelop, Ubuntu):

#include <iostream>
#include <thread>

static const int num_threads = 10;

//This function will be called from a thread

void call_from_thread(int tid) {
  std::cout << "Launched by thread " << tid << std::endl;
}

int main() {
  std::thread t[num_threads];

  //Launch a group of threads
  for (int i = 0; i < num_threads; ++i) {
    t[i] = std::thread(call_from_thread, i);
  }

  std::cout << "Launched from the main\n";

  //Join the threads with the main thread
  for (int i = 0; i < num_threads; ++i) {
    t[i].join();
  }

  return 0;
}

I have added the pthread in the CMake file:

cmake_minimum_required(VERSION 2.8)

project(just_testing)

set(CMAKE_CXX_FLAGS "-g -Wall -pthread -std=gnu++11")

add_executable(just_testing main.cpp)

target_link_libraries(just_testing "-lpthread")

The problem is that I still get the error:

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

I do not get it... What have I done wrong?


EDIT:

If I compile it with g++ -std=c++11 -pthread ../main.cpp It generates a.out, that is running well. So it is something about cmake file: What?


EDIT 2: make with VERBOSE=1:

$ make VERBOSE=1
/usr/bin/cmake -H/home/me/projects/just_testing -B/home/me/projects/just_testing/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/me/projects/just_testing/build/CMakeFiles /home/me/projects/just_testing/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory `/home/me/projects/just_testing/build'
make -f CMakeFiles/just_testing.dir/build.make CMakeFiles/just_testing.dir/depend
make[2]: Entering directory `/home/me/projects/just_testing/build'
cd /home/me/projects/just_testing/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/me/projects/just_testing /home/me/projects/just_testing /home/me/projects/just_testing/build /home/me/projects/just_testing/build /home/me/projects/just_testing/build/CMakeFiles/just_testing.dir/DependInfo.cmake --color=
make[2]: Leaving directory `/home/me/projects/just_testing/build'
make -f CMakeFiles/just_testing.dir/build.make CMakeFiles/just_testing.dir/build
make[2]: Entering directory `/home/me/projects/just_testing/build'
/usr/bin/cmake -E cmake_progress_report /home/me/projects/just_testing/build/CMakeFiles 1
[100%] Building CXX object CMakeFiles/just_testing.dir/main.cpp.o
/usr/bin/c++    -g -Wall -pthread -std=gnu++11 -g -I/usr/local/include/opencv -I/usr/local/include    -o CMakeFiles/just_testing.dir/main.cpp.o -c /home/me/projects/just_testing/main.cpp
Linking CXX executable just_testing
/usr/bin/cmake -E cmake_link_script CMakeFiles/just_testing.dir/link.txt --verbose=1
/usr/bin/c++   -g -Wall -pthread -std=gnu++11 -g    CMakeFiles/just_testing.dir/main.cpp.o  -o just_testing -rdynamic /usr/local/lib/libopencv_core.a /usr/local/lib/libopencv_flann.a /usr/local/lib/libopencv_imgproc.a /usr/local/lib/libopencv_highgui.a /usr/local/lib/libopencv_features2d.a /usr/local/lib/libopencv_calib3d.a /usr/local/lib/libopencv_ml.a /usr/local/lib/libopencv_video.a /usr/local/lib/libopencv_legacy.a /usr/local/lib/libopencv_objdetect.a /usr/local/lib/libopencv_photo.a /usr/local/lib/libopencv_gpu.a /usr/local/lib/libopencv_videostab.a /usr/local/lib/libopencv_ts.a /usr/local/lib/libopencv_ocl.a /usr/local/lib/libopencv_superres.a /usr/local/lib/libopencv_nonfree.a /usr/local/lib/libopencv_stitching.a /usr/local/lib/libopencv_contrib.a -lpthread /usr/local/lib/libopencv_nonfree.a /usr/local/lib/libopencv_gpu.a /usr/local/lib/libopencv_legacy.a /usr/local/lib/libopencv_photo.a /usr/local/lib/libopencv_ocl.a /usr/local/lib/libopencv_calib3d.a /usr/local/lib/libopencv_features2d.a /usr/local/lib/libopencv_flann.a /usr/local/lib/libopencv_ml.a /usr/local/lib/libopencv_video.a /usr/local/lib/libopencv_objdetect.a /usr/local/lib/libopencv_highgui.a /usr/local/lib/libopencv_imgproc.a /usr/local/lib/libopencv_core.a /usr/local/share/OpenCV/3rdparty/lib/liblibjpeg.a -lpng /usr/local/share/OpenCV/3rdparty/lib/liblibtiff.a /usr/local/share/OpenCV/3rdparty/lib/liblibjasper.a /usr/local/share/OpenCV/3rdparty/lib/libIlmImf.a -lz -lgtk-x11-2.0 -lgdk-x11-2.0 -latk-1.0 -lgio-2.0 -lpangoft2-1.0 -lpangocairo-1.0 -lgdk_pixbuf-2.0 -lcairo -lpango-1.0 -lfontconfig -lgobject-2.0 -lglib-2.0 -lfreetype -lgthread-2.0 -lglib-2.0 -lfreetype -lgthread-2.0 -lavcodec -lavformat -lavutil -lswscale -lstdc++ -ldl -lm -lpthread -lrt 
make[2]: Leaving directory `/home/me/projects/just_testing/build'
/usr/bin/cmake -E cmake_progress_report /home/me/projects/just_testing/build/CMakeFiles  1
[100%] Built target just_testing
make[1]: Leaving directory `/home/me/projects/just_testing/build'
/usr/bin/cmake -E cmake_progress_start /home/me/projects/just_testing/build/CMakeFiles 0

EDIT 3: c++ and g++ versions:

me@me-VirtualBox:~$ c++ --version
c++ (Ubuntu 4.9-20140406-1ubuntu1) 4.9.0 20140405 (experimental) [trunk revision 209157]
Copyright (C) 2014 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.

me@me-VirtualBox:~$ g++ --version
g++ (Ubuntu 4.9-20140406-1ubuntu1) 4.9.0 20140405 (experimental) [trunk revision 209157]
Copyright (C) 2014 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.

Could it be because I use VirtualBox? (I do not think so)

thedarkside ofthemoon
  • 2,251
  • 6
  • 31
  • 48
  • It's been a loooong time since I last wrote something in C/C++, but if I understand the code correctly, you are not using pthreads, but C++11's native std::threads. You shouldn't need to have pthread in your cmake. Don't know whether that'll solve your problem, though, sorry. EDIT: Now that I've read the link above by OrangeDog, I might not even be right about that... – Filip Vondrášek Jul 04 '14 at 13:04
  • 1
    There's also an SO question the answer to which suggests this might be due to a gcc bug: http://stackoverflow.com/questions/19463602/compiling-multithread-code-with-g – shakurov Jul 04 '14 at 13:06
  • 2
    @FilipB.Vondrášek The C++ standard library threads are not [green threads](http://en.wikipedia.org/wiki/Green_threads), they are built on top of native threads. On POSIX systems that is typically POSIX threads (the `pthread` functions). – Some programmer dude Jul 04 '14 at 13:07
  • @FilipB.Vondrášek If you're using g++ and `libstd++`, then you *do* need the `-pthread` flag. The implementation uses `pthreads` under the hood. – juanchopanza Jul 04 '14 at 13:07
  • I cannot reproduce this on my system (Linux-3.14.9-200.fc20.x86_64-x86_64-with-fedora-20-Heisenbug, gcc version 4.8.3 20140624 (Red Hat 4.8.3-1)). – Jonas Schäfer Jul 04 '14 at 13:13
  • If the two solutions offered do not solve the problem, please amend your question to show your attempts and the results thereof. – MSalters Jul 04 '14 at 13:14
  • 1
    `-pthread` is needed when linking. It's possible that `CMAKE_CXX_FLAGS` is only used when compiling (though I know nothing about cmake, so can't confirm that or offer a solution). – Mike Seymour Jul 04 '14 at 13:14
  • 1
    @MikeSeymour It is in fact needed in both places (the preprocessor needs to know about this, too) -- while linking, it would need to be ``-lpthread``, which the OP also added. – Jonas Schäfer Jul 04 '14 at 13:14
  • @OrangeDog I have written the `#include`s between `<>` So there is not an answer – thedarkside ofthemoon Jul 04 '14 at 13:15
  • 1
    @thedarksideofthemoon Try compiling and running with your own command line, by-passing cmake, to see if the problem is with gcc or with cmake. – juanchopanza Jul 04 '14 at 13:16
  • @JonasWielicki: that doesn't seem to be the case, though I can't find the documentation to confirm what you're supposed to do. I pass `-pthread` (no `l`) to the linker, and nothing special to the compiler, and threading is supported. – Mike Seymour Jul 04 '14 at 13:20
  • @MikeSeymour I usually pass `-pthread` as a *compiler* flag only, and it seems to work. I found this: http://stackoverflow.com/questions/2127797/gcc-significance-of-pthread-flag-when-compiling – juanchopanza Jul 04 '14 at 13:22
  • Edited question after compiled in command line – thedarkside ofthemoon Jul 04 '14 at 13:23
  • 2
    Run the makefile manually with `VERBOSE=1` (i.e. `make VERBOSE=1`) to make sure that all flags are set as they should. – Some programmer dude Jul 04 '14 at 13:24
  • @MikeSeymour gcc manpage documents ``-pthread`` and its effects. I don’t know about the neccessity of the preprocessor knowing about this, but appearantly its part of some specification. – Jonas Schäfer Jul 04 '14 at 13:25
  • The solution could be that you're passing `gnu+11` in CMake and `c++11` manually. I'm putting 50$ there. – Nemanja Boric Jul 04 '14 at 13:29
  • @JonasWielicki: So `-pthread` (not `-lpthread`) is indeed needed when linking, as I said (and also when compiling, apparently). – Mike Seymour Jul 04 '14 at 13:30
  • I have added the VERBOSE=1 result: It seems that the flags are there... No? – thedarkside ofthemoon Jul 04 '14 at 13:31
  • gnu+11 and c++11 are not solving my problem ... – thedarkside ofthemoon Jul 04 '14 at 13:32
  • Can you confirm that `/usr/bin/c++` is pointing to the same executable where `g++` is? – Nemanja Boric Jul 04 '14 at 13:33
  • 1
    The flags look alright, so it's probably the problem answered in the question @shakurov links to, which is a bug in GCC. – Some programmer dude Jul 04 '14 at 13:38
  • @thedarksideofthemoon you can run `c++ --version` and `g++ --version`. – Nemanja Boric Jul 04 '14 at 13:40
  • It seems to be the same: see edit 3 – thedarkside ofthemoon Jul 04 '14 at 13:42
  • 1
    Looks like http://stackoverflow.com/questions/19463602/compiling-multithread-code-with-g . The answer says use **-Wl,--no-as-needed** – cup Jul 04 '14 at 14:32
  • Yes, `-Wl,--no-as-needed` was the problem – thedarkside ofthemoon Jul 07 '14 at 07:25

1 Answers1

1

You need to specify -pthread, not -lpthread, when linking (as well as compiling).

This tells the linker not just to link with the pthread library, but also to use it to support the C++ library.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644