gpt4 book ai didi

c++ - 无法在 C++ 中使用多线程

转载 作者:行者123 更新时间:2023-11-28 06:48:05 30 4
gpt4 key购买 nike

<分区>

我用 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;
}

我在 CMake 文件中添加了 pthread:

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

问题是我仍然得到错误:

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

我不明白...我做错了什么?


编辑:

如果我用 g++ -std=c++11 -pthread ../main.cpp 编译它,它会生成一个运行良好的 a.out。所以它是关于 cmake 文件的:什么?


编辑 2:使用 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

编辑 3:c++ 和 g++ 版本:

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.

会不会是因为我用的是VirtualBox? (我不这么认为)

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com