草庐IT

atomic_thread_fence

全部标签

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++11 - 无法使用 std::thread 和 std::condition_variable 唤醒线程

当我试图通过另一个线程唤醒一个线程时遇到了一个问题。一个简单的生产者/消费者。代码下方。第85行是我不明白为什么它不起作用的地方。生产者线程填充std::queue并调用std::condition_variable.notify_one()而消费者线程正在等待NOTstd::queue.empty()。在此先感谢您的帮助#include#include#include#include#include#include//requestclassrequest:publicstd::mutex,publicstd::condition_variable,publicstd::queue{

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++ - 在 C++11 智能指针中存储 std::thread

在C++11及更高版本中,像这样直接将std::thread存储为类的成员时,有什么优点或缺点:std::threadmy_thread;与像这样将std::shared_ptr或std::unique_ptr存储到线程相反:std::shared_ptrmy_thread_ptr;是否有任何代码选项比其他选项更好?或者没关系,只需2种不同的方式来处理线程对象。 最佳答案 使用指针(或智能指针)成员可能有一些不太常见的原因,但对于常见用法,似乎是std::thread要么不适用,要么本身就足够灵活:我们可能希望更好地控制对象的生命周

c++ - 如何在你的 C++ 项目中包含 boost::thread?

我需要做什么才能在我的项目中包含boost::thread?我已将整个线程文件夹复制到我的工作路径(我希望能够在多台计算机上运行它)并且我得到了fatalerrorC1083:Cannotopenincludefile:'boost/thread/detail/platform.hpp':Nosuchfileordirectory来自#include"thread/thread.hpp"行什么给了?编辑:即使我只是链接到安装预编译二进制文件的boost文件夹,并且我使用#include我明白了fatalerrorLNK1104:cannotopenfile'libboost_threa

c++ - 使用 std::shared_ptr 对象实例创建 boost::thread

我有以下两个代码段。第一个block按预期编译和工作。但是第二个block不编译。我的问题是,给定下面的代码,当尝试基于由shared_ptr代理的对象实例创建线程时,正确的语法是什么?#include#include#include#includestructfoo{voidboo(){}};intmain(){//Thisworks{foo*fptr=newfoo;boost::threadt(&foo::boo,fptr);t.join();deletefptr;}//Thisdoesn'twork{std::shared_ptrfptr(newfoo);boost::threa

c++ - std::thread Visual Studio 2012 警告

我试图了解如何通过VisualStudio2012使用新的std::thread。我正在尝试编译以下代码。#include#includeclassscoped_thread{std::threadt_;public:explicitscoped_thread(std::thread&t):t_(std::move(t)){if(!t_.joinable())throwstd::logic_error("Nothread");}~scoped_thread(){t_.join();}private:scoped_thread(scoped_threadconst&);scoped_th

c++ - 链接到 boost_thread 的问题

我正在尝试将xubuntu上的boost1.47与gcc4.6和glibc2.13链接起来。到目前为止,我无法编译以下简单程序main.cpp:#include#include#includeintmain(){size_tn_threads=boost::thread::hardware_concurrency();return0;}当我编译时:g++-lboost_thread-lboost_regex-omcmain.cpp-static-lpthread/usr/local/lib/libboost_regex.a/usr/local/lib/libboost_thread.a

c++ - 指向 std::thread 的共享指针是一种不好的做法吗?

使用std::shared_ptr有意义吗?逻辑很简单:如果不需要线程,则删除它,如果需要新线程-重新分配它。有什么方法可以将这个概念与线程池进行比较吗?我确实知道我系统中线程的确切数量(我开发了图像处理算法,我想给“算法”类的每个子级一个单独的线程(也许让它私有(private),然后不需要shared_ptr),该算法将在何处运行,如果未提供图像,则将此私有(private)线程闲置。这是一个糟糕的概念吗? 最佳答案 您可能错过了事实std::thread析构函数不会终止线程。正如评论中已经提到的,ifdetachorjoinw