草庐IT

scoped-lock

全部标签

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++ - 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++ - "Function not declared in this scope"编译openCV代码出错

我正在尝试编写一些使用openCV函数的代码。我从文档中提供的一些示例代码开始:#include#include#includeusingnamespacecv;usingnamespacestd;intmain(intargc,char**argv){if(argc!=2){cout当我尝试在Eclipse-CDT中构建它时,我得到了这个:****BuildofconfigurationDebugforprojectopenCV1****makeallBuildingtarget:openCV1Invoking:CrossG++Linkerg++-L/usr/local/lib-o"

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++ - (C++) 错误 : 'invalid_argument' was not declared in this scope

我正在使用EclipseC/C++和MinGW编译器。我已将标志-std=c++11添加到项目属性中C/C++Build下的MiscellaneousGCCCCompilerSettings中。我知道这可能是一件简单的事情,但我无法解决此错误。Date.h#includeusingnamespacestd;classDate{public:Date(intm=1,intd=1,inty=1900);voidsetDate(int,int,int);private:intmonth;intday;intyear;staticconstintdays[];};日期.cpp#include#

c++ - C++ 中的 "Variable ' i ' was not declared in scope "是什么?

在练习C++代码时,我使用了在for循环中声明的变量。我希望它在另一个for循环中再次使用它。但它向我显示了一个错误,即variableiwasnotdeclaredinscope我在EclipseIDE中尝试了相同的循环thesymboliwasnotresolved.示例代码与此类似:#includeusingnamespacestd;intmain(){for(inti=0;i 最佳答案 您必须为每个范围声明变量:#includeusingnamespacestd;intmain(){for(inti=0;i在第一个循环之后,

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++ 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

c++ - C++11 中的 BOOST scoped_lock 替换

我面临这样一种情况,我必须用C++11中的等效项替换BOOSTscoped_lock。在visualstudio2013下。由于c++11不支持scoped_lock,我不确定下面的替换代码是什么。我应该选择lock_guard还是try_lock?boost::mutex::scoped_lockobjectLock(ObjectVectorMutex,boost::try_to_lock);if(objectLock){//...}在代码中我有以下“等待”语句if(ObjectsCollection.empty()){//Thisiswherewewaittilsomethingi