草庐IT

CONDITION_VARIABLE

全部标签

c++ - 正确实现 condition_variable timed_wait

我正在阅读我的STL实现(标准问题g++4.6.2)并在condition_variable中遇到了这种竞争条件:templatecv_statuswait_for(unique_lock&__lock,constchrono::duration&__rtime){returnwait_until(__lock,__clock_t::now()+__rtime);}因为__clock_t是一个std::chrono::system_clock,我们被NTP之类的奇思妙想所束缚(如果时钟在__clock_t::now()+__rtime,那我们就等一天)。C++标准(30.5.1)似乎是

c++ - 编译器 : What if condition is always true/false

我考虑条件和编译器。我正在为Arduino编写一个应用程序,因此我需要该应用程序尽可能快。在我的代码中我有这个:#defineDEBUGfalse...if(DEBUG){StringpinName;pinName="Pin";pinName+=pin;pinName+="initialized";Serial.println(pinName);}我想知道编译器是否不包含二进制文件中的代码(ifblock中的代码)。条件总是假的,所以程序永远不会去那里。从另一边。如果DEBUG为真怎么办?Arduino是测试条件还是编译器只在二进制文件中包含if的主体?我找到了这个网站https://

c++ - 使用 std::mutex、std::condition_variable 和 std::unique_lock

我在理解条件变量及其在互斥锁中的使用方面遇到了一些问题,希望社区可以帮助我。请注意,我来自win32背景,所以我与CRITICAL_SECTION、HANDLE、SetEvent、WaitForMultipleObject等一起使用。这是我第一次尝试使用c++11标准库进行并发,它是programexamplefoundhere的修改版本.#include#include#include#include#include#include#includeint_tmain(intargc,_TCHAR*argv[]){std::queuenNumbers;std::mutexmtxQueu

c++ - 线程池实现 : condition_variables vs. yield()

我尝试用C++开发一个线程池,我想知道是在工作线程的主循环中让线程产生更好还是等待条件变量更好:voidworker_thread(void){//thisismoreorlesspseudocodewhile(!done){if(task_available)run_task();elsestd::this_thread::yield();}}对voidworker_thread(void){//thisismoreorlesspseudocodestd::unique_locklk(mutex_);while(!done){if(task_available)run_task();

c++ - 如何删除 VS 警告 C4091 : 'typedef ' : ignored on left of 'SPREADSHEET' when no variable is declared

此警告在我的代码中由同一个声明多次触发,内容如下://SpreadsheetstructuretypedefstructSPREADSHEET{intID;//IDofthespreadsheetUINTnLines;//NumberoflinesvoidCopyFrom(constSPREADSHEET*src){ID=src->ID;nLines=src->nLines;}};我不想只是关闭该警告,而是更改代码,以免出现警告!注意:我不想在这里声明任何变量(它是一个头文件),只定义结构'SPREADSHEET'应该包含的内容... 最佳答案

C++11 可变参数模板 : return tuple from variable list of vectors

我想写一些类似于pythonzip(http://docs.python.org/2/library/functions.html)的东西。zip应该接受可变数量的不同类型的vector,并返回一个vector元组,截断到最短输入的长度。例如x=[1,2,3]v=['a','b']我希望输出是一个vector[,]如何在C++11中做到这一点? 最佳答案 急切地做到这一点并且只通过复制非常容易:#include#include#includetemplatestd::vector>zip(std::vectorconst&...vs

c++ - scoped_lock 如何避免发出 "unused variable"警告?

boost::mutex::scoped_lock是一个方便的RAII包装器,用于锁定互斥锁。我对其他事情使用了类似的技术:一个RAII包装器,它要求数据接口(interface)从/重新连接到串行设备。不过,我想不通的是,为什么在下面的代码中只有我的对象mst(其实例化和销毁确实有副作用)会导致g++发出“未使用的变量”警告错误,而l设法保持沉默。你知道吗?你能告诉我吗?[generic@sentinel~]$cattest.cpp#include#include#includestructMyScopedThing;structMyWorkerObject{voida(){std:

c++ - 我是否需要同步 std::condition_variable/condition_variable_any::notify_one

是否需要同步std::condition_variable/condition_variable_any::notify_one?据我所知,如果丢失通知是可以接受的-可以调用未protectednotify_one(例如通过互斥锁)。例如,我看到了以下使用模式(抱歉,不记得在哪里):{{lock_guardl(m);//dowork}c.notify_one();}但是,我检查了libstdc++源代码,发现:condition_variable::notify_onevoidcondition_variable::notify_one()noexcept{int__e=__gthre

c++ - 这个 "if e is a pack, then get a template name, otherwise get a variable name"是否有效?

我尝试构建一个不需要typename或template的案例,但仍会根据给定名称t生成变量或模板是否为函数参数包templatestructA{templatestaticvoidf(int){}};templatestructA{staticconstintf=0;};templateusingtype=int;templatevoidf(Tt){A...)>::f(1);}intmain(){f(1);}以上将引用staticconstint,并进行比较。以下刚好有Tt变成了一个包并制作f引用模板,但GCC也不喜欢templatevoidf(T...t){A...)>::f(1);

c++ - 有没有办法执行 "if (condition) typedef ..."

当且仅当满足编译时条件时,我想执行typedef。如果条件不满足,则根本不执行typedef。这在C++11中可行吗?例子:classA{std::conditional_typedef;//Performs"typedefintmyType1".std::conditional_typedef;//Doesnothingatall.};我正在寻找这个虚构的std::conditional_typedef。 最佳答案 另一种方法是从基类的特化中传递//fooisalightstruct(onlyatypedefornotatall)