草庐IT

conditional-execution

全部标签

c++ - 使用 'g++' 结果为 "warning: will never be executed"

我继承了一个C++项目。我在RHELbuild5.5与GCC4.1.2通过makefile。该项目很大(数百个文件),总的来说代码还不错。然而,在编译过程中,我经常收到一个GCC警告,上面写着(prefix"/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2"):/bits/allocator.h:Inconstructor‘std::allocator::allocator()[with_Tp=char]’:/bits/allocator.h:97:warning:willneverbeexecuted

c++ - 什么时候可以在没有谓词的情况下使用 std::condition_variable?

如果std::condition_variable可以由于虚假唤醒而发出信号(并且我们不能确定我们需要的条件是否真的得到满足),为什么C++标准库提供重载没有谓词的wait()方法?什么场景可以使用这种行为? 最佳答案 假设一个复杂条件:A||B。当条件的任何部分为真时,应执行适当的操作,actionA或actionB。使用predicate版本,代码如下:cond.wait(lock,[]{return(A||B);});if(A){actionA();}else{actionB();}但如果使用非谓词等待,代码可能更快:whil

c++ - 为什么 std::condition_variable 的通知和等待函数都需要一个锁定的互斥量

在我对理解std::contion_variable的永无止境的探索中,我遇到了以下问题。在thispage它说了以下内容:voidprint_id(intid){std::unique_locklck(mtx);while(!ready)cv.wait(lck);//...std::cout然后它说:voidgo(){std::unique_locklck(mtx);ready=true;cv.notify_all();}据我所知,这两个函数都将在std::unqique_lock行停止。直到获得唯一锁。也就是说,没有其他线程有锁。假设print_id函数首先执行。将获取唯一锁,函数

c++ - (C++ 线程): Creating worker threads that will be listening to jobs and executing them concurrently when wanted

假设我们有两个worker。每个worker都有一个0和1的id。还假设我们一直有工作到达,每个工作也有一个标识符0或1指定哪个worker必须做这个工作。我想创建2个线程,它们最初是锁定的,然后当两个作业到达时,解锁它们,每个线程都完成它们的工作,然后再次锁定它们,直到其他作业到达。我有以下代码:#include#include#includeusingnamespacestd;structjob{threadjobThread;mutexjobMutex;};jobjobs[2];voidexecuteJob(intworker){while(true){jobs[worker].

c++ - 在 std::conditional 中使用不完整类型的 sizeof

这是一个最小的例子:structincomplete_type;templatestructfoo{usingtype=std::conditional_t,std::conditional_t,double>;};foof;会导致错误,因为它会对类型执行sizeof,即使incomplete_type不是算术类型(iow,它不会在逻辑上进入sizeof分支)。livedemo所以,我想推迟sizeof:第一次尝试(失败)templateautofoo_aux(){if(sizeof(T)conditional_t,decltype(foo_aux()),double>仍然触发相同的错

c++ - 为什么 C++ 标准 1.9/5 谈论 "possible execution sequences"?

根据C++03标准1.9/5Aconformingimplementationexecutingawell-formedprogramshallproducethesameobservablebehaviorasoneofthepossibleexecutionsequencesofthecorrespondinginstanceoftheabstractmachinewiththesameprogramandthesameinput.我不明白“作为其中一个”部分。如果我有一个特定的程序和一个特定的输入,并且我的程序不包含未定义的行为,为什么可观察到的行为会有所不同?“一种可能的执行顺

c++ - 在 C++ 中用 for(;condition;) 替换 while(condition) 有什么理由吗?

看起来像while(condition){//dostuff}完全等同于for(;condition;){//dostuff}是否有任何理由使用后者而不是前者? 最佳答案 据我所知,没有好的理由。您使用不增加任何内容的for循环是故意误导人们。更新:根据OP对问题的评论,我可以推测您如何在实际代码中看到这样的结构。我以前见过(并使用过)这个:lots::of::namespaces::container::iteratoriter=foo.begin();for(;iter!=foo.end();++iter){//dostuff}

c++ - 追踪器 : error TRK0002: Failed to execute command

我在尝试在作为服务运行的buildAgent上构建我的项目时遇到此错误,有人有解决方案吗?TRACKER:errorTRK0002:Failedtoexecutecommand:""C:\ProgramFiles(x86)\MicrosoftVisualStudio14.0\VC\bin\amd64\CL.exe"@C:\BuildAgent\temp\buildTmp\tmpfde187c5fd8a42299ab4d18e25e0c9fe.rsp".Theoperationidentifierisnotvalid.在命令行中构建项目时(使用“_IsNativeEnvironment”

c++ - std::condition_variable::wait_until 相对于 std::this_thread::sleep_for 有什么优势吗?

在时间等待场景中:oursoftwareworksinthebackground,andsynchronizesdatawiththeserverinevery20-30minutes.我想用std::this_thread::sleep_for但我的上级强烈反对任何形式的sleep功能。他推荐std::condition_variable::wait_until(lock,timeout-time,pred)不知道在这种情况下sleep_for有什么缺点吗? 最佳答案 正如评论中已经指出的那样,这仅取决于您的用例。两者之间的主要区

c++ - std::condition_variable::wait with predicate

在std::condition_variable的文档中,有一个以谓词函数作为参数的wait()重载。该函数将等到谓词函数为真的第一个wake_up。在documentation据说这等同于:while(!pred()){wait(lock);}还有:Thisoverloadmaybeusedtoignorespuriousawakeningswhilewaitingforaspecificconditiontobecometrue.Notethatbeforeentertothismethodlockmustbeacquired,afterwait(lock)exitsitisals