草庐IT

atomic_bool

全部标签

c++ - 使用 atomic_flag 自旋锁进行内存排序

我正在尝试熟悉c++11的新内存排序概念,并且相信我实际上已经很好地掌握了它们,直到我偶然发现了自旋锁的这个实现:#includenamespaceJayZ{namespaceTools{classSpinLock{private:std::atomic_flagspin_lock;public:inlineSpinLock(void):atomic_flag(ATOMIC_FLAG_INIT){}inlinevoidlock(void){while(spin_lock.test_and_set(std::memory_order_acquire));}inlinevoidunlock

c++ - std::atomic<double> & 的模板特化

我有这个MCVE:#include#includetemplatevoidassertVariableHasBeenSet(T,constchar*);templatevoidassertVariableHasBeenSet&>(std::atomic&myDouble,constchar*variableName){printf("Double:%s=%f\n",variableName,myDouble.load());};intmain(){std::atomicmyDoubleAtomic{23.45};assertVariableHasBeenSet(myDoubleAtom

c++ - 将 ifstream 转换为 bool 和使用 ifstream::is_open() 之间的区别

也许是个伪问题,但我需要一个明确的答案。这些函数的返回有什么不同吗intFileExists(conststd::string&filename){ifstreamfile(filename.c_str());return!!file;}intFileExists(conststd::string&filename){ifstreamfile(filename.c_str());returnfile.is_open();}所以换句话说,我的问题是:将fstream转换为bool会给出与fstream::is_open()完全相同的结果吗>? 最佳答案

c++ - libstdc++ 的 std::vector<bool>::data 有什么作用?

根据标准,std::vector没有成员函数data().但是,以下代码片段可以使用带有libstdc++的最新GCC正常编译:#includeintmain(){std::vectorv;v.data();}如果我们尝试使用结果,结果返回类型是void.这是一些gcc扩展还是一个错误?如果前者为真,它有什么作用? 最佳答案 我的/usr/include/c++/4.8/bits/stl_bvector.h有://_GLIBCXX_RESOLVE_LIB_DEFECTS//DR464.Suggestionfornewmemberfu

c++ - is_lock_free 未在 gcc 4.7.2 的 std::atomic<T> 中定义?

我遇到这个编译器错误functionstd::atomic::is_lock_free()const:error:undefinedreferenceto'__atomic_is_lock_free'whencompilingcodelikebelowusinggcc4.7.2onlinux.structS{inta;intb;};std::atomics;cout 最佳答案 AtomicAPIisn'tcompleteinGCC4.7:Whenlockfreeinstructionsarenotavailable(eitherth

c++ - c++11(atomic)的获取释放操作

#include#include#includeclassatomicAcquireRelease00{public:atomicAcquireRelease00():x(false),y(false),z(0){}voidrun(){std::threada(&atomicAcquireRelease00::write_x,this);std::threadb(&atomicAcquireRelease00::write_y,this);std::threadc(&atomicAcquireRelease00::read_x_then_y,this);std::threadd(&at

c++ - 我需要 std::atomic<bool> 还是 POD bool 足够好?

考虑这段代码://globalstd::atomicrun=true;//thread1while(run){/*dostuff*/}//thread2/*dostuffuntilit'stimetoshutdown*/run=false;我在这里需要与原子变量相关的开销吗?我的直觉是,bool变量的读/写或多或少是原子的(这是一个常见的g++/Linux/Intel设置),如果有一些写/读时序异常,我在线程1上的运行循环会停止一个结果是早晚通过,对于这个应用程序我不是很担心。还是我在这里遗漏了一些其他考虑因素?查看perf,我的代码似乎在std::atomic_bool::opera

c++ - 使用 boost::tuple<bool, T> 来指示是否找到 T 是否好?

假设我们需要一个返回值的函数。但是那个东西是找不到的。我看到选项:1.Tfind(bool&ok);//returndefaultTvalueifnotfound我们可以创建一个结构体:templateclassCheckableValue{public:CheckableValue(),_hasValue(false){}CheckableValue(constT&t):_value(t),_hasValue(true){}inlineboolhasValue()const{return_hasValue}constT&value()const{assert(hasValue());

c++ - 对 null 对象的函数调用返回的 bool 值

在C++中对空对象进行函数调用时返回的bool值是什么?ClassDummy*dummy=NULL;if(!dummy->dummy_function(1,2,3)){//DoSomething}根据C++11标准,这不应该返回错误吗? 最佳答案 除非dummy已在命名空间范围内声明,否则它未初始化且其值未指定,即它可能为null也可能不为null。在nullptr或指向无效内存的指针上调用成员函数是未定义的行为。你可能getawaywiththecorrectresult如果您调用的成员函数不访问该类的任何其他数据成员;换句话说,

c++ - C++ 编译器可以假定 const bool & value 不会改变吗?

C++编译器能否假定“constbool&”值不会改变?例如,假设我有一个类:classtest{public:test(constbool&state):_test(state){}voiddoSomething(){if(_test){doMore();}}voiddoMore();private:constbool&_test;};我按如下方式使用它:voidexample(){boolmyState=true;testmyTest(myState);while(someTest()){myTest.doSomething();myState=anotherTest();}}标准