草庐IT

scoped-lock

全部标签

c++ - 应该使用哪个 header 来使用 scoped_ptr

我想在我的C++应用程序中使用智能指针。使用stdscoped_ptr应该包含哪个头文件? 最佳答案 标准C++库中没有scoped_ptr。全部C++11smartpointers在标题中.如果你想要boost::scoped_ptr那么你需要boost/scoped_ptr.hpp. 关于c++-应该使用哪个header来使用scoped_ptr,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/q

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++ - std::unique_lock::release 的用例是什么?

在什么情况下会使用std::unique_lock的release方法?我错误地使用了release方法而不是unlock方法,我花了一段时间才明白为什么下面的代码不起作用。#include#include#include#include#includestd::mutexmtx;voidfoo(){std::unique_locklock(mtx);std::coutthreads;for(inti=0;i 最佳答案 它在thisanswer中有很好的用途其中锁定状态的所有权明确地从函数本地unique_lock转移到外部实体(通

c++ - std::unique_lock 和 std::condition_variable 如何工作

我需要弄清楚lock和condition_variable是如何工作的。在此处的-稍微修改过的代码中cplusplusreferencestd::mutexm;std::condition_variablecv;std::stringdata;boolready=false;boolprocessed=false;voidworker_thread(){//Waituntilmain()sendsdatastd::unique_locklk(m);cv.wait(lk,[]{returnready;});//afterthewait,weownthelock.std::coutlk(m

C++ 错误 : ‘_mm_sin_ps’ was not declared in this scope

我正在尝试对将函数应用于数组的不同方法进行基准测试。为什么是https://software.intel.com/sites/landingpage/IntrinsicsGuide/#expand=3260,2124,4779,4779&cats=Trigonometry&text=_sin_mm_sin_ps在我的范围内未知,但_mm_sqrt_ps是?我如何让它为人所知?并编译无误。#include#include#include#include#include#include#include"immintrin.h"#includeintmain(){std::coutdis(-

c++ - std::lock_guard 导致未定义的行为

编辑:看起来,问题是我实际上并没有创建一个lock_guard的本地实例,而只是一个匿名的临时实例,它立即再次被销毁,如下面的评论所指出的。Edit2:启用clang的线程清理器有助于在运行时查明这些类型的问题。它可以通过启用clang++-std=c++14-stdlib=libc++-fsanitize=thread*.cpp-pthread这在某种程度上可能是一个重复的问题,但我找不到任何东西,所以如果它真的是重复的,我很抱歉。无论如何,这应该是一个初学者问题。我正在玩一个简单的“Counter”类,比如在文件中内联计数器.hpp:#ifndefCLASS_COUNTER_HPP

c++ - QScopedPointer、boost::scoped_ptr - 为什么提示类型不完整?

我有一个c-Structure,我想将它嵌入到一个cpp类中而不破坏我的全局命名空间,所以我不想包含c-header。这就是为什么我想使用具有前向声明结构名称的智能作用域指针(QScopedPointer或boost::scoped_ptr)。我不明白的是上述两个在编译时失败的作用域指针的实现:boost:errorC2027:useofundefinedtype'xxx'templateinlinevoidchecked_delete(T*x){//intentionallycomplex-simplificationcausesregressionstypedefchartype_

C++ 继承 : scoping and visibility of members

你能解释一下为什么这是不允许的吗,#includeclassB{private:inta;public:inta;};intmain(){return0;}这是什么时候?#includeclassA{public:inta;};classB:publicA{private:inta;};intmain(){return0;}在这两种情况下,我们在classB中都有一个名为a的公共(public)变量和一个私有(private)变量。现在编辑! 最佳答案 Inboththecases,wehaveonepublicandonepriv

c++ - std::lock_guard 有什么问题

我有简单的代码:第一个线程将std::strings推送到std::list,第二个线程弹出std::strings从这个std::list。所有std::list的操作都受到std::mutexm的保护。此代码将错误永久打印到控制台:"Error:lst.begin()==lst.end()"。如果我将std::lock_guard替换为构造m.lock()和m.unlock()代码将开始正常工作。std::lock_guard有什么问题?#include#include#include#include#includestd::mutexm;std::listlst;voidf2()

c++ - boost scoped_lock 互斥锁崩溃

我已经保护了一个std::queue的访问函数,push、pop、size,在这些函数中使用boost::mutexes和boost::mutex::scoped_lock有时它会在作用域锁中崩溃调用栈是这样的:00x0040f005boost::detail::win32::interlocked_bit_test_and_setinclude/boost/thread/win32/thread_primitives.hpp36110x0040e879boost::detail::basic_timed_mutex::timed_lockinclude/boost/thread/wi