草庐IT

scoped-lock

全部标签

c++ - thread_guard 与 scoped_thread

在书中"C++ConcurrencyInAction"byAnthonyWilliams您可以找到以下两段代码(我已经引入了一些小的修改):片段1:classthread_guard{std::thread&t;public:explicitthread_guard(std::thread&t_):t(t_){}~thread_guard(){if(t.joinable()){t.join();}}thread_guard(thread_guardconst&)=delete;thread_guard&operator=(thread_guardconst&)=delete;};voi

c++ - try_lock_for 未按预期工作

我正在摆弄一些C++中的代码,由于某种原因不想工作,我将它缩小到这种情况:#include#include#include#include#includeusingnamespacestd;voidtest(){timed_mutexm;m.lock();std::cout问题是test()根本不会阻塞,即使try_lock返回false。有没有我忽略的东西,或者这是gcc中的错误,或者我接下来应该去哪里找出问题所在?感谢任何建议和帮助!我像这样编译了这个小程序:g++-pthread-std=c++11threads.cpp-othreads如果有任何帮助,这是gcc和我的操作系统的

c++ - 将 boost::shared_lock 升级为独占锁

谁能解释一下boost::upgrade_lock的正确用法。以下代码导致死锁//Globaltypedefboost::shared_mutexMutex;typedefboost::shared_lockReadLock;typedefboost::upgrade_lockUpgradeLock;typedefboost::upgrade_to_unique_lockWriteLock;MutexsharedMutex;//Multithreadedreaderandwriter{ReadLockread(sharedMutex);for(intii=0;ii如果我在升级前用rea

c++ - 如何在 std::pair 中返回 std::lock_guard

当我从函数返回std::pair中的std::lock_guard时,我遇到了可怕的错误。但是当我将它打包在一个类中时,我没有任何问题(按预期编译和工作)。我不明白为什么。详情如下:我设计了一个小模板类来方便地锁定和解锁共享对象。它不是特别创新,但C++17允许它非常紧凑并且代码读/写友好:templateclassLocked{public:Locked(T&_object,std::mutex&_mutex):object(_object),lock(_mutex){}T&object;std::lock_guardlock;};templateclassLockable{publ

c++ - 使用自定义删除器 boost scoped_ptr/scoped_array

我不知道如何让scoped_ptr或scoped_array使用自定义删除器。也许还有另一种实现类似于shared_ptr允许受控删除?顺便说一句,为什么shared_ptr允许自定义删除器而scoped_ptr不允许?只是好奇。 最佳答案 Idon'tseehowtogetscoped_ptrorscoped_arraytousecustomdeleter你不能。Maybethereisanotherimplementationwhichallowscontrolleddeletionsimilartoshared_ptr?如果您

c++ - 为什么 std::move 对 std::unique_lock 没有任何影响?

我有以下C++(11)代码:#includevoidunlock(std::unique_lock&&ulock){}intmain(void){std::mutexm;std::unique_lockulock(m);unlock(std::move(ulock));if(ulock.mutex()==&m||ulock.owns_lock()){throwstd::runtime_error("");}return0;}我想不通的是为什么在unlock()返回后互斥体仍然被持有。我的期望是std::move()导致锁在从unlock()调用返回时超出范围(并被析构函数解锁).至少,

C++:引用 "out of scope"对象

关于引用文献,有一件事我一直不明白,我希望有人能帮助我。据我所知,引用不能为空。但是如果你有一个函数foo()返回对堆栈对象的引用会发生什么:Object&foo(){Objecto;returno;}Object&ref=foo();理论上ref将引用一个不存在的对象,因为一旦函数返回,o就会超出范围。这里发生了什么? 最佳答案 这会导致未定义的行为。不要这样做。在实现方面,实际上,引用将指向调用foo的堆栈框架所在的堆栈。在许多情况下,该内存仍然有意义,因此错误通常不会立即显现出来。因此,您应该注意永远不要创建这样的悬空引用。

c++ - Win32 文件锁定读取 : how to find out who's locking them

在C++中(特别是在VisualC++中),有时您无法打开一个文件,因为另一个可执行文件已经打开并且没有共享它以供读取。如果我尝试打开这样的文件,我如何以编程方式找出谁在锁定该文件? 最佳答案 在Windows2000及更高版本中,如果不使用内核模式驱动程序,则无法执行此操作。ProcessExplorer和其他类似工具会自动加载驱动程序来完成此操作。这是因为文件句柄位于内核空间中,用户模式应用程序(EXE文件)无法访问。如果你真的有兴趣这样做,看看thisproject. 关于c++-

c++ - "Nested"scoped_lock

我缩短的简化类如下所示:classA{public://...methodA();methodB();protected:mutableboost::mutexm_mutex;sometype*m_myVar;}A::methodA(intsomeParam){boost::mutex::scoped_lockmyLock(m_mutex);m_myVar->doSomethingElse();}A::methodB(intsomeParam){boost::mutex::scoped_lockmyLock(m_mutex);m_myVar->doSomething();this->m

c++ - 如何在不违反 const 正确性的情况下使用 std::lock_guard?

在一个子类中,我有一个私有(private)的std::mutexm字段,我在基类纯虚方法的实现中使用它以线程安全的方式返回一个值(值可以由另一个线程更新):intSubClass::get()const//implements'virtualintget()=0const'ofthebaseclass{std::lock_guardlck(m);returnvalue;}编译器通过产生错误告诉我这违反了const正确性:error:binding'conststd::mutex'toreferenceoftype'std::lock_guard::mutex_type&{akastd