草庐IT

try_lock

全部标签

c++ - 函数 try block 处理程序是什么?

我最近遇到了这个问题-什么是函数tryblock处理程序?还有,它有什么用处? 最佳答案 Here你可以找到一个很好的解释。它在构造函数的初始化列表中可能很有用:structA{private:std::strings;public:A(intvalue)try:s(boost::lexical_cast(value)){}catch(boost::bad_lexical_cast){/*handlelexical_castexceptionhere*/}}; 关于c++-函数trybl

c++ - Fedora 22 - 编译 - __atomic_is_lock_free

我尝试在Fedora22上编译一个软件(SuperCollider),但我遇到了一个问题:libsupernova.a(server.cpp.o):Infunction`std::atomic::is_lock_free()const':/usr/include/c++/5.1.1/atomic:212:undefinedreferenceto`__atomic_is_lock_free'collect2:error:ldreturned1exitstatusserver/supernova/CMakeFiles/supernova.dir/build.make:96:recipefo

c++ - 为什么我们应该将 `std::unique_lock` 放在本地范围内?

基于C++EquivalenttoJava'sBlockingQueuevoidpush(Tconst&value){//originalversion{std::unique_locklock(this->d_mutex);d_queue.push_front(value);}this->d_condition.notify_one();}voidpush(Tconst&value){//myquestion//{//commentoutthescopestd::unique_locklock(this->d_mutex);d_queue.push_front(value);//}/

c++ - boost::scoped_lock 不适用于局部静态变量?

我制作了以下示例程序来使用boost线程:#pragmaonce#include"boost\thread\mutex.hpp"#includeclassThreadWorker{public:ThreadWorker(){}virtual~ThreadWorker(){}staticvoidFirstCount(intthreadId){boost::mutex::scoped_lock(mutex_);staticinti=0;for(i=1;i主类://ThreadTest.cpp#include"stdafx.h"#include"boost\thread\thread.hpp

c++ - 错误 : no match for ‘operator<’ in ‘__x < __y’ when trying to insert in two map

在代码中有两个映射。一个存储对和另一个存储,其中值是具有5个变量的类,数据类型为字符串、整数、字符串、整数、整数。但是在插入第二个映射期间,我收到错误g++错误:尝试在map中插入时,'__x如何解决。classValues{private:std::stringC_addr;intC_port;std::stringS_addr;intS_port;intC_ID;public:Values(std::string,int,std::string,int,int);voidprintValues();};Values::Values(std::stringCaddr,intCport

C++:具有 `std::lock_guard` 的互斥量是否足以同步两个 `std::thread`?

我的问题是基于下面的C++代码示例#include#include#include#includeclassClassUtility{public:ClassUtility(){}~ClassUtility(){}voiddo_something(){std::coutlock(g_mutex);std::coutlock(g_mutex);std::cout如果需要,这更像是一个问题,目的是让我的理解更清楚,并获取std::condition_variable的示例用法。我有2个C++std::thread,它们在main方法中启动。它是osx上的控制台应用程序。所以使用clang编

c++ - 关于 RAII,C++ `try`/`catch` block 是否与其他 block 相同?

好吧,如果我使用RAII习惯用法来管理某些上下文属性*,如果我在tryblock的开头直接使用它,它会像我预期的那样工作吗?换句话说,如果我有这个:structraii{raii(){std::cout……我成功地使用了它:{raiido_the_raii_thing;stuff_expecting_raii_context();/*…*/}...如果我这样做,RAII实例会以同样的方式工作吗:try{raiido_the_raii_thing;stuff_expecting_raii_context_that_might_throw();/*…*/}catch(std::except

c++ - std::tr1::shared_ptr 是否会抛出 bad_alloc 并且在 try/catch block 中是个好主意?

我实际上正在制作一个简单的C++SFML游戏,我想学习更多关于C++编程的知识。现在我正在使用shared_ptr来管理资源。创建新资源时,我对shared_ptrs有一些疑问,例如:shared_ptrresource(newResource(World::LEVEL));根据boostshared_ptr(Y*p)throwsbad_alloc。我不知道std::tr1是否也这样做。而且我不知道我是否应该担心将shared_ptr放入try/catchblock中以检查是否抛出bad_alloc。这是一个好的编程习惯吗? 最佳答案

c++ - 错误 : expected primary-expression before ‘>’ : templated function that try to uses a template method of the class for which is templated

这个问题在这里已经有了答案:WhereandwhydoIhavetoputthe"template"and"typename"keywords?(8个答案)关闭8年前。在使用模板和仿函数(未出现在这个问题中)时,我最终遇到了以下简化的问题。以下代码(也可用here)classA{public:templateboolisGood(intin)const{constTf;returninbooltryEvaluator(T&evaluator,intvalue){returnevaluator.isGood(value);}intmain(intargc,constchar*argv[]

c++ - 如何在 boost::unique_lock<boost::mutex> 上尝试锁定

根据标题,如何在boost::unique_lock上尝试锁定?我有这段代码:voidmySafeFunct(){if(myMutex.try_lock()==false){return-1;}//mutexownershipisautomaticallyacquired//dostuffsafelymyMutex.unlock();}现在我想使用unique_lock(它也是一个作用域互斥体)而不是普通的boost::mutex。我希望这样可以避免函数体中的所有unlock()调用。 最佳答案 您可以使用Deferconstruc