草庐IT

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++ - 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++ - 在运行时本地启用/禁用 OpenMP

是否可以在运行时启用或禁用OpenMP并行化?我有一些代码应该在某些情况下并行运行,而不是在不同情况下并行运行。同时,其他线程中的其他计算也使用OpenMP,应该始终并行运行。有没有办法告诉OpenMP不要在当前线程中并行化?我知道omp_set_num_threads,但我假设全局设置OpenMP使用的线程数。 最佳答案 您可以使用的替代方法是将if条件添加到#pragmaomp结构中。只要条件为假,这些将跳过对从编译指示派生的OpenMP运行时调用的调用。考虑以下使用基于变量t和f(分别为true和false)的条件的程序:#i

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++ - 为什么这个简单的线程代码会失败?

我正试图做到这一点,我不能从循环中调用线程。但是当我运行它时,我得到一个运行时错误:terminatecalledafterthrowinganinstanceof'std::system_error'what():InvalidargumentThread#1#include#include#include#include#includestd::mutexm;staticintthread_count;autofoo=[&]{std::lock_guardlock(m);std::cout>threads(20,std::make_shared(foo));for(constaut

c++ - C++中 "main"线程的id

在C++中有没有办法获取“主”程序线程的ID?我看到std::this_thread::get_id()获取当前正在执行的线程的ID,但我需要main的ID,即原始程序线程。我没有看到任何功能来获取它。原因是我有一些非线程安全的内部函数,它们只能在应用程序的原始线程上调用,所以为了安全我想这样做:-assert(std::this_thread::get_id()==std::main_thread::get_id());但是当然没有执行此操作的功能,而且我看不到任何获取该信息的方法。 最佳答案 您可以在this_thread仍然是

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