草庐IT

c++ - boost 条件不适用于具有两个生产者和一个消费者的线程安全队列

我有两个线程添加到“线程安全”队列中。但是,当第二个线程尝试“推送”内容时。不会通知消费者内容可用。队列继续增长,但notify_one()从不通知消费方法中的条件。这是为什么?#ifndefCONCURRENT_QUEUE_H#defineCONCURRENT_QUEUE_H#include#includetemplateclassconcurrent_queue{private:std::queuethe_queue;mutableboost::mutexthe_mutex;boost::condition_variablethe_condition_variable;public

c++ - 获取/释放内存顺序

在例子中:#include#include#include#includestd::vectordata;std::atomicflag=ATOMIC_VAR_INIT(0);voidthread_1(){data.push_back(42);flag.store(1,std::memory_order_release);}voidthread_2(){intexpected=1;while(!flag.compare_exchange_strong(expected,2,std::memory_order_acq_rel)){expected=1;}}voidthread_3(){w

c++ - win32 std::thread 是否泄漏内存?

我在Windows732位下使用MinGWgcc4.8.2编译了以下代码://t.cpp#include#includeclassMine{public:Mine(){std::cout使用g++t.cpp-std=c++11-static-ggdb,并使用Dr.Memory和drmemorya.exe进行调试,我得到了以下输出:Dr.Memoryversion1.6.1739build42builtonFeb23201422:29:25Dr.Memoryresultsforpid7400:"a.exe"Applicationcmdline:"a.exe"Recorded102supp

c++ - 安装摩西翻译软件。错误消息 : "ld: library not found for -lboost_thread"

我正在使用Xcode6.1的MacOSX10.9.5上安装Moses翻译软件。Theinstructionssay我需要安装g++和Boost。执行此操作后,我将gitclone,“cd”到目录中,然后键入./bjam-j8。首先,我确认我具备先决条件。首先,g++(我只是单击TAB以查看可用的内容):$g++g++g++-4.9然后boost:$brewinstallboostWarning:boost-1.56.0alreadyinstalled然后我尝试安装:$./bjam-j8Tip:installtcmallocforfasterthreading.SeeBUILD-INST

c++ - std::thread 在使用参数创建时抛出访问冲突异常?

关闭。这个问题需要debuggingdetails.它目前不接受答案。编辑问题以包含desiredbehavior,aspecificproblemorerror,andtheshortestcodenecessarytoreproducetheproblem.这将有助于其他人回答问题。关闭6年前。Improvethisquestion我正在使用VS2015,在使用std::thread时遇到一个非常奇怪的问题。voidKlass::myfunc(inta,intb){std::cout它在Debug模式下运行良好,但当我转到Release模式时抛出“访问冲突异常”。此外,如果我尝试将

c++ - 跨内核线程迁移后是否可以强制重新加载 thread_local 变量?

我在内核和线程之上实现用户线程并观察到,当用户线程在内核线程之间迁移时,thread_local变量会从先前的内核位置读取,即使变量也被标记作为volatile。由于编译器仅将用户级swapcontext视为函数调用,因此下面的示例演示了简单函数调用的问题。#includestructFoo{intx;inty;};__threadFoo*volatilefoo;voidbar(){asm("nop");}voidf(){foo->x=5;bar();asmvolatile("":::"memory");//Wedesireasecondcomputationoftheaddresso

c++ - 内部类和初始化

我有一个这样定义的类:这还不完整,可能无法编译。classServer{public:Server();~Server();classWorker{public:Worker(Server&server):_server(server){}~Worker(){}voidRun(){}voidStop(){}private:Server&_server;}voidRun(){while(true){//dowork}}voidStop(){//HowdoIstopthethread?}private:std::vector_workers;};我的问题是,如何初始化传入名为Server的

c++ - 使用线程池进行模拟: boost-thread and boost-asio

我想使用boost::asio来设置线程池。我的问题是:如何将特定数据附加到创建的每个线程,以及如何管理单独的输出?更具体地说,我编写了一个类Simulation,它通过一种在输入中获取一些参数的方法来执行模拟。该类包含计算所需的所有数据。由于数据不是太大,我想复制它以便在池的每个线程中使用类Simulation的不同实例。我想做这样的事情:(这里解释了如何设置线程池:SO和Asiorecipes)classParallelSimulation{public:staticconststd::size_tN=10;protected:std::vector>simuInst;//Ncop

c++ - 使用 native_handle() + pthread_cancel() 取消 std::thread

我正在将之前围绕pthreads的线程包装器转换为std::thread。但是c++11没有办法取消线程。尽管如此,我还是需要取消线程,因为它们可能正在外部库中执行非常冗长的任务。我正在考虑在我的平台中使用给我pthread_id的native_handle。我在Linux(Ubuntu12.10)中使用gcc4.7。这个想法是:#include#include#includeusingnamespacestd;intmain(intargc,char**argv){cout线程被pthreads抛出的异常取消。我的问题是:这种做法会不会有什么问题(除了不可移植)?

c++ - CLOCK_THREAD_CPUTIME_ID 在 MacOS 上

我有一个要从Linux移植到MacOSX的函数,它使用带有CLOCK_THREAD_CPUTIME_ID的clock_gettime来测量在进程上花费的时间。我在Internet上找到了这段代码,它可以给我相当于CLOCK_REALTIME的代码:#ifdef__MACH__//OSXdoesnothaveclock_gettime,useclock_get_timeclock_serv_tcclock;mach_timespec_tts;host_get_clock_service(mach_host_self(),CALENDAR_CLOCK,&cclock);clock_get_