关于设置c++std::thread的亲和力和c++11std:thread的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于c–boost::thread和std::thread之间的区别
关于设置c ++ std :: thread的亲和力和c++11 std:thread的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于c – boost :: thread和std :: thread之间的区别、c – CMake链接错误pthread:启用多线程以使用std :: thread:不允许操作、c – OpenMP,MPI,POSIX线程,std :: thread,boost :: thread如何关联?、c – ‘std :: thread :: thread’:没有重载函数需要7个参数等相关知识的信息别忘了在本站进行查找喔。
本文目录一览:- 设置c ++ std :: thread的亲和力(c++11 std:thread)
- c – boost :: thread和std :: thread之间的区别
- c – CMake链接错误pthread:启用多线程以使用std :: thread:不允许操作
- c – OpenMP,MPI,POSIX线程,std :: thread,boost :: thread如何关联?
- c – ‘std :: thread :: thread’:没有重载函数需要7个参数
设置c ++ std :: thread的亲和力(c++11 std:thread)
我试图在UDP数据报到达时在Windows中进行精确的时间测量。 从Microsoft阅读文档我决定使用QueryPerformanceCounter。 在同一个文档中,它build议您设置时序线程的亲和性和性能。 我正在使用Boost和c ++ 11来实现我的asynchronousUDP服务器:
void receiveEvent() { listening_socket.async_receive_from( boost::asio::buffer(event_buffer,max_length),signaling_endpoint,[this](boost::system::error_code ec,std::size_t bytes_recvd) { if (!ec && bytes_recvd > 0) { LARGE_INTEGER prectime; ::QueryPerformanceCounter(&prectime); std::cout << prectime.QuadPart << std::endl; } else if (ec == boost::asio::error::operation_aborted) { std::cout << "socket canceled" << std::endl; return; } receiveEvent(); }); } listening_socket(io,boost::asio::ip::udp::endpoint(boost::asio::ip::udp::v4(),port)); receiveEvent(); async_thread = std::thread(boost::bind(&boost::asio::io_service::run,&io)); // Todo set thread affinity using SetProcessAffinityMask // Todo set thread priority using SetPriorityClass
如何使用SetProcessAffinityMask和SetPriorityClass使用std::thread async_thread SetPriorityClass ?
检索任何窗口的types和值的所有控件
你可以在C / C ++ include指令中使用环境variables吗?
为什么getsockopt返回一个错误?
如何检查一个string是否包含从一个z到任何字母?
如何在ELF中创build一个部分?
总结
以上是小编为你收集整理的设置c ++ std :: thread的亲和力全部内容。
如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。
c – boost :: thread和std :: thread之间的区别
std::vector<boost::shared_ptr<boost::thread> > threads; for (std::size_t i = 0; i < io_services_.size(); ++i) { boost::shared_ptr<boost::thread> thread(new boost::thread( boost::bind(&boost::asio::io_service::run,io_services_[i]))); threads.push_back(thread); }
如果我尝试使用它与std:thread我得到编译错误:
std::vector<std::thread> threads; for (std::size_t i = 0; i < this->ioServices.size(); ++i) { std::thread thread(&boost::asio::io_service::run,ioServices[i]); // compile error std::thread::thread : no overloaded function takes 2 arguments threads.push_back(std::move(thread)); }
解决方法
但是,如果你使用boost :: bind,这是有效的.所以我会使用,并手动手动执行绑定:
std::vector<std::thread> threads; for (std::size_t i = 0; i < this->ioServices.size(); ++i) { std::thread thread(boost::bind(&boost::asio::io_service::run,ioServices[i])); threads.push_back(std::move(thread)); }
编辑:似乎boost :: bind成功,因为它有大量的重载,并且基于它提供的参数数量,在boost :: bind的重载解析和模板替换期间,它可以确定boost :: asio的哪个重载:: io_service :: run是有意的.
但是,由于std :: bind和std :: thread依赖于vararg tempalte参数,因此run的两个重载同样有效,并且编译器无法解析使用哪一个.这种模糊性导致无法确定您所看到的失败的结果.
另一个解决方案是:
std::vector<std::thread> threads; typedef std::size_t (boost::asio::io_service::*signature_type)(); signature_type run_ptr = &boost::asio::io_service::run; for (std::size_t i = 0; i < this->ioServices.size(); ++i) { std::thread thread(run_ptr,ioServices[i]); threads.push_back(std::move(thread)); }
c – CMake链接错误pthread:启用多线程以使用std :: thread:不允许操作
C++ Threads,std::system_error – operation not permitted?
我正在使用完全相同的源代码并进行编译
g++ ../src/main.cpp -pthread -std=c++11
工作没有任何问题.
因为我想在更大的项目中使用线程,所以我必须使用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})
但对我而言,我总是这样做不起作用:
terminate called after throwing an instance of 'std::system_error' what(): Enable multithreading to use std::thread: Operation not permitted Aborted (core dumped)
我的错是什么?
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 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
解决方法
我现在正在使用GCC 5.4,有问题的cmake片段工作正常.
我刚刚面临同样的问题.我这样的最终cmakelist.txt文件,它的工作原理.
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})
c – OpenMP,MPI,POSIX线程,std :: thread,boost :: thread如何关联?
我也知道技术的选择实际上并不是唯一的,因此可以使用另一种技术(再次,MPI和OpenMP).为什么它们被用于不同的效果但仍然使用相同的源(cpu)?
如果我根据每种技术编译一个具有并行性的C程序,那么(从操作系统和硬件的角度来看)会有什么不同?例如,OpenMP或std :: thread是否使用POSIX线程?如果是这样,C 11的线程如何在Windows上运行?或者这些技术中的每一种都通过汇编语言直接与cpu协同工作?
解决方法
Unix libc包含了许多有用功能的OS线程包(如互斥锁,cond vars等).通常这种系统库的外部接口是“POSIX线程”(函数名为pthread_ *):http://en.wikipedia.org/wiki/POSIX_Threads
Windows有自己的难以使用的线程API(WINAPI的CreateThread等).但是有一些围绕Windows API的包装器可以获得类似POSIX线程api的东西(例如mingw32和cygwin有这样的库;检查wikipedia section)
C 11 std :: thread,boost的boost :: thread只是围绕系统线程API的现代OS独立包装器.它们用于创建可在任何支持的平台上编译的可移植程序,而无需创建#ifdef地狱和/或围绕系统线程库编写自己的自定义包装器.如果您正在创建新程序,请考虑使用这种方式.
还有其他几种穿线包装纸,例如包含在QT或GTK等图形库中.
OpenMP实现具有内部支持库(例如,gcc具有libgomp),其使用系统/ libc线程API,例如libgomp使用POSIX线程.一些实现还可以包括经由组件的用户空间线程切换(M:N线程模型).
MPI里面没有线程库. MPI用于创建多个进程并在它们之间建立通信.但是当MPI用于多线程程序时,它将使用一些线程API来进行同步.例如,MPICH将在unix上使用pthread.
c – ‘std :: thread :: thread’:没有重载函数需要7个参数
我可以传递一个结构但最好不要在可能的情况下更改我的代码.
解决方法
通过定义此宏_VARIADIC_MAX,可以将参数的最大数量设置为10.
所以只需做一些像#define _VARIADIC_MAX 10这样的事情.
对于std :: thread,默认值为5,2标准参数5可变参数.总的来说,您可以通过设置上面的值来传递多达12个参数.
关于设置c ++ std :: thread的亲和力和c++11 std:thread的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于c – boost :: thread和std :: thread之间的区别、c – CMake链接错误pthread:启用多线程以使用std :: thread:不允许操作、c – OpenMP,MPI,POSIX线程,std :: thread,boost :: thread如何关联?、c – ‘std :: thread :: thread’:没有重载函数需要7个参数的相关信息,请在本站寻找。
本文标签: