草庐IT

thread_safe

全部标签

C++11 std::thread vs windows CreateThread

关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题吗?更新问题,以便editingthispost提供事实和引用来回答它.关闭8年前。Improvethisquestion哪个选项更适合在VisualC++下创建(和管理)线程:C++11std::thread或WinAPI函数(例如CreateThread、_beginthreadex等)以及为什么? 最佳答案 便携性std::thread是C++11标准的新内容-使用它,您可以在支持C++11的编译器之间用C++编写可移植代码。你可以在其中感受到fut

C++11:GCC 4.8 静态 thread_local std::unique_ptr 未定义引用

我需要为将通过宏访问的每个线程存储一个唯一指针。我想我应该用一个单例和静态thread_localstd::unique_ptr对象来解决这个问题。这是代码的简化版本:main.cpp#include#include#include#includeusingnamespacestd;#include"yay.hpp"mutexcoutMutex;voidyay(intid){int*yayPtr=getYay();//IknowthisisbadcoutMutex.lock();couthappy;for(inti=0;i耶.hpp#ifndefBE_HAPPY#defineBE_HA

c++ - 可以使用 boost::asio::thread_pool 来代替 boost::asio::io_context 与 boost::thread::thread_group 的组合吗?

我正在努力解决我的一些困惑。我偶然发现了boost::asio::thread_pool并且我认为可以使用以某种方式自动组合boost::asio::io_context和boost::thread::thread_group就像经常建议的那样(here或here)。似乎这个asio特定的池可用于post任务,但另一方面,一些网络类型,如resolver需要将对象io_context作为构造函数参数传递,而thread_pool不是也不是从该参数派生的。 最佳答案 假设你有一个单独的io_context对象,名为ioc。您可以创建多

c++ - 为什么这个计算在 boost::thread 和 std::thread 中给出不同的结果?

当这个浮点计算在boost::thread中执行时,它给出的结果与在std::thread或主线程中执行时不同。voidprint_number(){doublea=5.66;doubleb=0.0000001;doublec=500.4444;doubled=0.13423;doublev=std::sin(d)*std::exp(0.4*a+b)/std::pow(c,2.3);printf("%llX\n%0.25f\n",*reinterpret_cast(&v),v);}这似乎是因为boost::thread默认使用53位内部精度进行float学运算,而主线程使用64位精度。

c++ - 混合 C++11 std::thread 和 C 系统线程(即 pthreads)

我正在编写一个多线程C++程序并希望使用多线程C库。这个库希望我使用native系统方法为其创建一些工作线程,并使用如下代码将控制权传递给它的run()函数:voidsystem_specific_thread_init();#ifdef_WIN32DWORDWINAPIsystem_specific_thread_run(LPVOIDunused){library_run();return0;}voidsystem_specific_thread_init(){Createthread(NULL,0,system_specific_thread_run,NULL,0,NULL);}#

c++ - 引用参数:这是 std::thread 和 std::bind 之间的不一致吗?

std::bind和std::thread共享一些设计原则。由于它们都存储与传递的参数相对应的本地对象,因此如果需要引用语义,我们需要使用std::ref或std::cref:voidf(int&i,doubled){/*...*/}voidg(){intx=10;std::bind(f,std::ref(x),_1)(3.14);std::threadt1(f,std::ref(x),3.14);//...}但我对最近的一个个人发现很感兴趣:std::bind将允许您在上述情况下传递一个值,即使这不是人们通常想要的。std::bind(f,x,_1)(3.14);//Usuallyw

C++11:类中的 std::thread 在构造函数中执行具有线程初始化的函数成员

我正在尝试使用C++11中的std::thread。如果可以在执行其函数成员之一的类中拥有std::thread,我无法找到任何地方。考虑下面的例子......在我的尝试中(如下),函数是run()。我使用带有-std=c++0x标志的gcc-4.4进行编译。#ifndefRUNNABLE_H#defineRUNNABLE_H#includeclassRunnable{public:Runnable():m_stop(false){m_thread=std::thread(Runnable::run,this);}virtual~Runnable(){stop();}voidstop(

c++ - 如何在 sleep 时唤醒 std::thread

我正在使用C++11,我有一个std::thread,它是一个类成员,它每2分钟向监听器发送一次信息。其他的,它只是sleep。所以,我让它休眠2分钟,然后发送所需的信息,然后再次休眠2分钟。//MyClass.hppclassMyClass{~MyClass();RunMyThread();private:std::threadmy_thread;std::atomicm_running;}MyClass::RunMyThread(){my_thread=std::thread{[this,m_running]{m_running=true;while(m_running){std:

c++ - 在 c++0x 中使用 __thread

我读到C++中有一个新关键字:它是我读过的__thread。我只知道它是一个可以像static关键字一样使用的关键字,但我什么都不知道。这个关键字是否仅仅意味着,例如,如果一个变量是这样声明的:__threadintfoo;那么与该变量有关的任何事情都将使用新线程执行? 最佳答案 它是thread_local,而不是__thread。用于定义具有线程存储时长的变量。thread_local是在C++0x中添加的new存储持续时间说明符。还有其他存储时长:static、automatic和dynamic。来自thislink:thre

c++ - 通过引用将参数传递给 std::thread 函数是否安全?

#include#include#include#includeusingnamespacestd;voidf(constvector&coll){this_thread::sleep_for(1h);////Iscollguaranteedtobevalidbeforeexitingthisfunction?//}intmain(){{vectorcoll(1024*1024*100);thread(f,coll).detach();}////Iknowstd::threadwillcopyargumentsintoitselfbydefault,//butIdon'tknowwhe