草庐IT

thread-local

全部标签

c++ - 将参数传递给 std::thread 的区别,C++

引自NikolaiJosuttis-StandardLibraryC++11:Detachedthreadscaneasilybecomeaproblemiftheyusenonlocalresources.Theproblemisthatyoulosecontrolofadetachedthreadandhavenoeasywaytofindoutwhetherandhowlongitruns.Thus,makesurethatadetachedthreaddoesnotaccessanyobjectsaftertheirlifetimehasended.Forthisreason,

c++ - 主线程中 block 作用域静态与命名空间作用域 thread_local 的初始化和销毁​​顺序

我正在尝试了解在主线程的上下文中使用静态存储持续时间和线程本地存储持续时间来初始化和销毁​​命名空间范围和block范围对象的顺序规则。考虑这两个类:structFoo{Foo(){std::cout除了它们的静态实例成员函数的实现之外,它们是相同的:thread_localFoot_foo;Foo&Foo::instance(){returnt_foo;}Bar&Bar::instance(){staticBars_bar;returns_bar;}Bar是一个Meyers单例,一个具有静态存储持续时间的block范围对象。Foo的实例是具有线程本地存储持续时间的namespace范

c++ - 关于multi-probe Local Sensitive Hashing的问题

很抱歉问这种菜鸟问题,但因为我真的非常急需一些关于如何使用MultiprobeLSH的指导,所以我自己没有做太多研究。我意识到有一个lib调用LSHKIT可以实现该算法,但我在尝试弄清楚如何使用它时遇到了麻烦。现在,我有几千个296维的特征向量,每个代表一个图像。该vector用于查询用户输入的图像,以检索最相似的图像。我用来推导vector之间距离的方法是欧氏距离。我知道这可能是一个相当菜鸟的问题,但是你们知道我应该如何实现多探针LSH吗?我真的非常感谢任何答复或回复。--更新--尝试使用提供的工具fitdata为我的数据创建模型,但它似乎没有包含我的文件。我用于输入的格式是这种格式

c++ - 打印 std::this_thread::get_id() 给出 "thread::id of a non-executing thread"?

这曾经工作得很好(然后外星人一定黑了我的电脑):#include#includeintmain(){std::cout现在它打印thread::idofanon-executingthread。ideone.com打印了一些ID,但有趣的是是什么导致了我平台上的这种行为。$uname-aLinuxxxx3.13.0-77-generic#121-UbuntuSMPWedJan2010:50:42UTC2016x86_64x86_64x86_64GNU/Linux有什么想法吗?编辑:嗯..当我添加std::cout两行打印相同的ID,但是当我删除它时,结果仍然相同-“非执行线程”。

c++ - 为什么 std::thread 通过转发引用接受仿函数

为什么std::thread对象通过转发引用接受函数参数,然后使用decay_copy复制对象?只按值接受函数对象不是更容易吗?一般来说,为什么不对函数进行模板化以便按值获取函数对象?引用性不能用reference_wrapper来模仿吗(这会更明确,并且还方便地有一个成员operator()来调用存储的函数)? 最佳答案 Whydoesastd::threadobjectacceptthefunctionparameterbyforwardingreferenceandthenmakeacopyoftheobjectwithdec

c++ - 与 std::condition_variable 相比,使用 std::atomic 的方法 wrt 在 C++ 中暂停和恢复 std::thread

这是一个单独的问题,但与我之前提出的问题有关here我正在使用std::thread在我的C++不断轮询某些数据并将其添加到缓冲区的代码。我用C++lambda像这样启动线程:StartMyThread(){thread_running=true;the_thread=std::thread{[this]{while(thread_running){GetData();}}};}thread_running是一个atomic在类头中声明。这是我的GetData功能:GetData(){//Someheavylogic}接下来我还有一个StopMyThread我设置的功能thread_r

C++ - thread_local 变量存储在哪里?

我想了解thread_local限定符究竟是如何工作的,以及实际变量存储在哪里?这是在C++上。假设我有一个包含多个成员变量的类。该类的对象在堆上实例化,该对象在2个线程之间共享。使用适当的锁定机制来确保两个线程不会同时踩踏一个成员变量。线程需要跟踪少数线程特定项目。所以我想在与类声明相同的头文件中创建一个thread_local变量。据我了解,两个线程都将获得该变量的自己的拷贝,对吗?线程局部变量究竟存放在内存的什么位置?如果是数据段,在执行过程中如何准确地获取正确的变量? 最佳答案 1。据我了解,两个线程都将获得自己的此变量拷贝

c++ - 使用空字符串 ""创建 std::locale

问题是否使用空字符串构造std::locale以使用户首选的native语言环境成为标准的一部分?如果是,您能否指出明确说明这一点的来源?问题描述std::locale文档中的示例有这一行:std::wcout这暗示使用空字符串创建语言环境将返回用户首选的本地语言环境。快速谷歌搜索后,这个article还提到:Theemptystringtellssetlocaletousethelocalespecifiedbytheuserintheenvironment.但是,在查看documentation时对于std::locale构造函数,没有提及提供空字符串时的特殊情况。引用如下:3-4

c++ - 不合格的名称查找 : Why local declaration hides declaration from using directive

考虑这段代码:namespaceA{inti=24;}namespaceB{usingnamespaceA;inti=11;intk=i;//findsB::i,noambiguity}和basic.lookup.unqual.2:§6.4.1Unqualifiednamelookup[basic.lookup.unqual]Thedeclarationsfromthenamespacenominatedbyausing-directivebecomevisibleinanamespaceenclosingtheusing-directive;see[namespace.udir].F

c++ - "pure virtual method called"实现 boost::thread 包装器接口(interface)时

我有一个小包装器,它集中了与线程相关的内容:classThread{protected:boost::thread*m_thread;virtualvoidwork()=0;voiddo_work(){work();}public:Thread():m_thread(NULL){}virtual~Thread(){catch_up();deletem_thread;}inlinevoidcatch_up(){if(m_thread!=NULL){m_thread->join();}}voidrun(){m_thread=newboost::thread(boost::bind(&Thr