草庐IT

lock_wrapper

全部标签

c++ - Fedora 22 - 编译 - __atomic_is_lock_free

我尝试在Fedora22上编译一个软件(SuperCollider),但我遇到了一个问题:libsupernova.a(server.cpp.o):Infunction`std::atomic::is_lock_free()const':/usr/include/c++/5.1.1/atomic:212:undefinedreferenceto`__atomic_is_lock_free'collect2:error:ldreturned1exitstatusserver/supernova/CMakeFiles/supernova.dir/build.make:96:recipefo

c++ - 为什么我们应该将 `std::unique_lock` 放在本地范围内?

基于C++EquivalenttoJava'sBlockingQueuevoidpush(Tconst&value){//originalversion{std::unique_locklock(this->d_mutex);d_queue.push_front(value);}this->d_condition.notify_one();}voidpush(Tconst&value){//myquestion//{//commentoutthescopestd::unique_locklock(this->d_mutex);d_queue.push_front(value);//}/

c++ - std::reference_wrapper *this 周围

我有一个场景,我需要转换一个可以被*this链接的函数返回std::optional>而不是T&(原因超出了这个问题的范围)。我使用std::reference_wrapper的原因是因为std::optional不能引用,至少在C++11中不能。但是,这不起作用,因为我似乎遇到了终身问题。这是一个最小的例子:#include#includestructtest{std::reference_wrapperfoo(){val=42;return*this;}test&foo2(){val=50;return*this;}intval;};voidbar(testt){std::cout

c++ - boost::scoped_lock 不适用于局部静态变量?

我制作了以下示例程序来使用boost线程:#pragmaonce#include"boost\thread\mutex.hpp"#includeclassThreadWorker{public:ThreadWorker(){}virtual~ThreadWorker(){}staticvoidFirstCount(intthreadId){boost::mutex::scoped_lock(mutex_);staticinti=0;for(i=1;i主类://ThreadTest.cpp#include"stdafx.h"#include"boost\thread\thread.hpp

c++ - 为 std::unordered_map 中的 const std::reference_wrapper 重载 operator==

我不知道如何使用std::reference_wrapper将std::string引用获取到std::unordered_map中>。根据以下链接,我知道我需要重载operator==。Whycantemplateinstancesnotbededucedin`std::reference_wrapper`s?但是,我不知道如何编写operator==以使其采用conststd::reference_wrapper。如果包装器不是const,那将不是问题。使用char而不是std::string效果很好(不需要重载operator==)。代码:#include#include#inc

C++:具有 `std::lock_guard` 的互斥量是否足以同步两个 `std::thread`?

我的问题是基于下面的C++代码示例#include#include#include#includeclassClassUtility{public:ClassUtility(){}~ClassUtility(){}voiddo_something(){std::coutlock(g_mutex);std::coutlock(g_mutex);std::cout如果需要,这更像是一个问题,目的是让我的理解更清楚,并获取std::condition_variable的示例用法。我有2个C++std::thread,它们在main方法中启动。它是osx上的控制台应用程序。所以使用clang编

c++ - 如何在 boost::unique_lock<boost::mutex> 上尝试锁定

根据标题,如何在boost::unique_lock上尝试锁定?我有这段代码:voidmySafeFunct(){if(myMutex.try_lock()==false){return-1;}//mutexownershipisautomaticallyacquired//dostuffsafelymyMutex.unlock();}现在我想使用unique_lock(它也是一个作用域互斥体)而不是普通的boost::mutex。我希望这样可以避免函数体中的所有unlock()调用。 最佳答案 您可以使用Deferconstruc

c++ - std::lock_guard<std::mutex> 施工段错误?

我正在尝试使用std::mutex和std::lock_guard访问共享的std::queue。mutex(pending_md_mtx_)是另一个对象(其地址有效)的成员变量。我的代码似乎在lock_guard的构造上出现了段错误。有什么想法吗?我应该改用std::unique_lock(或其他对象)吗?在UbuntuLinux下运行GCC4.6(--std=c++0x)。我无法发布整个类(class),但只能访问下面列出的互斥锁和队列。templateclassDriver{public:templateDriver(Args&&...args):listener_(std::f

c++ - std::reference_wrapper 何时转换为 T&?

考虑以下代码:#include#includeusingnamespacestd;templatevoidfun(Tt){t+=8;}intmain(){inti=0;fun(ref(i));cout此代码打印“8”。我假设fun()中的t自动转换为int&。但是如果我用t=8替换t+=8,程序将无法编译。为什么? 最佳答案 reference_wrapper有一个到T&的隐式转换运算符,所以它会被转换成T&无论哪里T&是比reference_wrapper更好的匹配.在扩充赋值表达式中,唯一可行的运算符是int&operator+

C++ weak_ptr.lock() 段错误

所以我有这个函数来添加监听器,它转换一个类的共享指针,以便我稍后在收到通知时可以调用它。voidregisterListener(std::shared_ptrlistener){if(!listener){qCWarning(OBSERVER_LOGGER)lock()){if(listener==shared){return;}iter++;}else{iter=listeners.erase(iter);}}autoweak=std::weak_ptr(listener);listeners.push_back(weak);}voidnotify(std::function)>o