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)