草庐IT

comma_guard

全部标签

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++ - 如何在 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++ - 如何在不违反 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

c++ - #ifdef/#define Include-Guard 之前的#include 可以吗?

我总是将我的#include放在#ifdef/#defineInclude-Guard之后。现在我的IDE(QtCreator)的重构机制将它放在Include-Guard之前,例如#include"AnotherHeader.h"#ifndefMYHEADER_H#defineMYHEADER_H这会导致任何问题吗?或者我可以这样吗? 最佳答案 如果有问题的header本身包含守卫,您就不会遇到问题。将它放在includeguards中可能仍会加快编译速度。编译器看不到的东西编译时间更短,即使它不会产生任何错误。

c++ - 为什么我们在进行条件变量通知之前需要一个空的 std::lock_guard?

我目前正在研究Google的Filament作业系统。你可以找到源代码here.让我感到困惑的部分是这个requestExit()方法:voidJobSystem::requestExit()noexcept{mExitRequested.store(true);{std::lock_guardlock(mLooperLock);}mLooperCondition.notify_all();{std::lock_guardlock(mWaiterLock);}mWaiterCondition.notify_all();}我很困惑为什么我们需要锁定和解锁,即使在锁定和解锁之间没有任何Ac

C++:为什么这个简单的 Scope Guard 有效?

到目前为止,每个看过的作用域守卫都有一个守卫bool变量。例如,请参阅此讨论:Thesimplestandneatestc++11ScopeGuard但是一个简单的守卫可以工作(gcc4.9,clang3.6.0):templatestructfinally_t:publicC{finally_t(C&&c):C(c){}~finally_t(){(*this)();}};templatestaticfinally_tfinally_create(C&&c){returnstd::forward(c);}#defineFINCAT_(a,b)a##b#defineFINCAT(a,b)

c++ - 返回时复制操作是在 lock_guard 析构函数之前还是之后执行的?

这个问题在这里已经有了答案:C++returnvaluecreatedbeforeorafterautovardestruction?(2个答案)inC++whichhappensfirst,thecopyofareturnobjectorlocalobject'sdestructors?[duplicate](4个答案)关闭4年前。get_a()函数对于竞争条件是否安全,或者我是否需要像在get_b()中那样显式复制str_以便按顺序有一个线程安全的功能?classClass{public:autoget_a()->std::string{auto&&guard=std::lock_

c++ - 如何使用 lock_guard 和 try_lock_for

我可以使用boost::lock_guard获取boost::mutex对象上的锁,并且此机制将确定一旦boost::lock_guard超出范围将释放锁:{boost::lock_guardlock(a_mutex);//Dothework}在这种情况下,无论代码块是否因异常退出,a_mutex都会被释放。另一方面,boost::timed_mutex也支持方法try_lock_for(period),例如if(a_timed_mutex.try_lock_for(boost::chrono::seconds(1))){//Dotheworka_timed_mutex.unlock(

c++ - 为什么不包括 guard 或编译指示一旦工作?

我正在编译一些代码,这些代码依赖于includeguards来防止对象和函数的多个定义,但是VisualStudio2008给我的链接错误是有多个定义。我不明白为什么,因为我以前使用过与此非常相似的代码并且没有引起问题。我一定是在做一些愚蠢的事情,但我不知道那是什么。我还尝试删除包含保护程序并使用一次#pragma,但我遇到了相同的链接错误。我应该检查什么? 最佳答案 如果它们是链接器错误,最可能的原因可能是header中定义的非内联函数。如果您在包含在多个源文件中的header中有一个非内联函数,它将在每个源文件(“翻译单元”)中

c++ - '警告 C4709 : comma operator within array index expression' - but no comma to be seen anywhere!

以这个小示例代码为例:structTest{operatorint()const{return0;}};Testtest(){returnTest();}intmain(){intarr[10];arr[test()]=5;}在VisualStudio2010下使用/W4编译生成saidwarning.有趣的是,如果我只添加一个简单的默认构造函数,警告就会突然消失。此外,它仅适用于该星座。如果我删除函数调用或不返回用户定义的类型,警告就会消失。此外,正如预期的那样,GCC不会产生此类警告。那么,我是否可以将上述代码的警告视为VisualStudio中的一个错误,或者它是否比看起来更重要