草庐IT

needs-lock

全部标签

c++ - 为什么 is_lock_free 是成员函数?

is_lock_free需要实例(它是成员函数)的原因是什么?为什么不是该类型的元函数,或者静态constexpr成员函数?我正在寻找一个实际的例子来说明为什么它是必要的。 最佳答案 标准允许类型有时无锁。section29.4Lock-freepropertyTheATOMIC_..._LOCK_FREEmacrosindicatethelock-freepropertyofthecorrespondingatomictypes,withthesignedandunsignedvariantsgroupedtogether.The

c++ - 使用 boost::lock_guard 进行简单的共享数据锁定

我是Boost库的新手,我正在尝试实现一个在共享队列上运行的简单生产者和消费者线程。我的示例实现如下所示:#include#include#includeboost::mutexmutex;std::dequequeue;voidproducer(){while(true){boost::lock_guardlock(mutex);std::coutlock(mutex);if(!queue.empty()){std::cout这段代码按我的预期运行,但是当main退出时,我得到/usr/include/boost/thread/pthread/mutex.hpp:45:boost::

C++ 模板类特化 : why do common methods need to be re-implemented

在示例中:#includeusingnamespacestd;classB{public:virtualvoidpvf()=0;};templateclassD:publicB{public:D(){}virtualvoidpvf(){}private:stringdata;};templateclassD:publicB{public:D();virtualvoidpvf(){coutd1;Dd2;}我收到以下错误:test.cpp:(.text+0x1c):undefinedreferenceto`D::D()'请注意,我不只是专门化D()本身的原因是我想消除对字符串D::data

c++ - scoped_lock 可以在读取模式下锁定 shared_mutex 吗?

C++17引入了std::shared_mutex和std::scoped_lock。我现在的问题是,当它作为参数传递时,scoped_lock将始终以独占(写入器)模式锁定共享互斥锁,而不是在共享(读取器)模式下。在我的应用程序中,我需要使用来自对象src的数据更新对象dst。我想锁定src共享和dst独占。不幸的是,如果同时调用另一个带有src和dst切换的更新方法,这可能会导致死锁。所以我想使用std::scoped_lock的花哨的死锁避免机制。我可以使用scoped_lock在独占模式下同时锁定src和dst,但是这种不必要的严格锁定会在其他地方产生性能回退。但是,似乎可以将

c++ - scoped_lock 如何避免发出 "unused variable"警告?

boost::mutex::scoped_lock是一个方便的RAII包装器,用于锁定互斥锁。我对其他事情使用了类似的技术:一个RAII包装器,它要求数据接口(interface)从/重新连接到串行设备。不过,我想不通的是,为什么在下面的代码中只有我的对象mst(其实例化和销毁确实有副作用)会导致g++发出“未使用的变量”警告错误,而l设法保持沉默。你知道吗?你能告诉我吗?[generic@sentinel~]$cattest.cpp#include#include#includestructMyScopedThing;structMyWorkerObject{voida(){std:

c++ - 第一次锁定和创建 lock_guard(adopt_lock) 和创建 unique_lock(defer_lock) 和锁定有什么区别?

我找到了以下两段代码:http://en.cppreference.com/w/cpp/thread/lockvoidassign_lunch_partner(Employee&e1,Employee&e2){//usestd::locktoacquiretwolockswithoutworryingabout//othercallstoassign_lunch_partnerdeadlockingus{//misthestd::mutexfieldstd::unique_locklk1(e1.m,std::defer_lock);std::unique_locklk2(e2.m,st

c++ - 为什么 std::unique_lock 使用类型标签来区分构造函数?

在C++11中,std::unique_lockconstructor被重载以接受类型标签defer_lock_t、try_to_lock_t和adopt_lock_t:unique_lock(mutex_type&m,std::defer_lock_tt);unique_lock(mutex_type&m,std::try_to_lock_tt);unique_lock(mutex_type&m,std::adopt_lock_tt);这些是空类(类型标签)definedasfollows:structdefer_lock_t{};structtry_to_lock_t{};stru

c++ - "Default member initializer needed within definition of enclosing class outside of member functions"- 我的代码格式不正确吗?

#includestructfoo{intx{0};foo()noexcept=default;voidf()noexcept(noexcept(std::declval())){}};intmain(){}liveexampleongodbolt上面的代码可以用我测试过的任何版本的g++,以及3.6到3.9.1的clang++编译,但是不能用clang++4.0.0编译:test.cpp:6:5:error:defaultmemberinitializerfor'x'neededwithindefinitionofenclosingclass'foo'outsideofmemberf

node.js - Mongoose 连接/模型 : Need to always run on open?

我使用的是Mongoose3,最明显的连接数据库的方法是conn=mongoose.createConnection(...)conn.on("open",...)问题是,我需要在open回调中定义我的所有模型吗?如果是这样,我将不得不创建一个看起来像的initMongoose.coffee#initMongoose.coffeemongoose=require"mongoose"module.exports=mongoose.createConnection...#modelExample.coffeeconn=require"./initDatabase"conn.on"open"

node.js - Mongoose 连接/模型 : Need to always run on open?

我使用的是Mongoose3,最明显的连接数据库的方法是conn=mongoose.createConnection(...)conn.on("open",...)问题是,我需要在open回调中定义我的所有模型吗?如果是这样,我将不得不创建一个看起来像的initMongoose.coffee#initMongoose.coffeemongoose=require"mongoose"module.exports=mongoose.createConnection...#modelExample.coffeeconn=require"./initDatabase"conn.on"open"