草庐IT

thread-priority

全部标签

c++ - STL priority_queue 复制比较器类

我正在尝试使用自定义比较器创建优先级队列:std::priority_queue,MyComparator>pq;我的问题是MyComparator有一个存储附加状态的方法。因为MyComparator被复制到优先级队列(据我所知),所以我无法在优先级队列持有的MyComparator实例上调用此方法。有没有办法:获得对优先级队列持有的MyComparator实例的访问权,或者:以某种方式通过引用传递原始MyComparator实例 最佳答案 STL容器中使用的比较对象以及STL算法中使用的谓词必须是可复制的对象和方法,算法可以随意

c++ - std::priority_queue:自定义排序而不定义比较器类

我想要一个自定义排序的优先级队列,但我很懒惰,不想定义一个实现operator()的比较器类。我真的很想编译这样的东西:std::priority_queue,boost::bind(some_function,_1,_2,obj1,obj2)>queue;其中some_function是一个带有四个参数的bool返回函数,第一个和第二个是队列的整数,最后两个是计算排序所需的一些对象(const引用)。(error:‘boost::bind’cannotappearinaconstant-expression)但这不能编译。甚至更简单std::priority_queue,&compa

c++ - Thread Sanitizer 是否可用?

我想尝试线程清洁剂(http://code.google.com/p/data-race-test/wiki/ThreadSanitizer#Using_ThreadSanitizer)所以我做了一个简单的程序:#include#include#include#include#include#includeusingnamespacestd;intviolated=0;mutexmtx;voidviolator(){lock_guardlg(mtx);violated++;}intmain(){threadt1(violator);t1.join();threadt2(violator

c++ - 从 std::thread 调用 boost::asio::io_service::run

我有一个处理我的连接的类,它有一个boost::asio::io_service成员。我想从std::thread调用io_service::run(),但我遇到了编译错误。std::threadrun_thread(&boost::asio::io_service,std::ref(m_io_service));不起作用。我看到了使用boost::thread执行此操作的各种示例,但我想为此坚持使用std::thread。有什么建议么?谢谢 最佳答案 我知道有两种方法,一种是通过lambda创建std::thread。std::t

c++ - priority_queue<> 比较指针?

所以我将STLpriority_queue与指针一起使用...我不想使用值类型,因为创建一堆仅用于优先级队列的新对象将非常浪费。所以...我正在尝试这样做:classInt{public:Int(intval):m_val(val){}intgetVal(){returnm_val;}private:intm_val;}priority_queuemyQ;myQ.push(newInt(5));myQ.push(newInt(6));myQ.push(newInt(3));现在我如何编写一个比较函数来让那些在Q中正确排序?或者,有人可以建议替代策略吗?我真的需要priority_que

c++ - 使用 shared_ptr 启动 std::thread

当你构造一个新线程时,提供的函数对象被复制到属于新创建线程的存储中。我想在一个新线程中执行一个对象方法。不应复制该对象。所以我将对象的shared_ptr传递给std::thread构造函数。如何使用std::shared_ptr()对象启动新线程?例如classFoo{public:voidoperator()(){//dosomething}};intmain(){std::shared_ptrfoo_ptr(newFoo);//Iwanttolaunchafoo_ptr()inanewthread//Isthisthecorrectway?std::threadmyThread(

c++ - 为什么是 std::this_thread 命名空间?

按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。std::this_thread命名空间是否有技术原因?为什么这个命名空间的成员不能作为std::thread类的静态成员实现?

GCC 中的 C++11 thread_pool、futures、promises 支持

是N2276中的任何一个gcc支持吗?我目前使用的是4.6.1,其中std::thread似乎工作正常。如果不是,我应该使用boost'sthreadpool?什么是可以轻松过渡到最终futures、promises和thread_pool的良好替代方案/权宜之计?也许this有用吗?N2276似乎没有列出here.我想知道N2276的任何特性是否会被纳入-我想我读到了有关futures和promises以及其他进入C++11的范例:将不得不谷歌看看周围发生了什么。 最佳答案 gccC++librarystatuspage显示其中大

c++ - 即使在 num_threads(1) 时,openmp 的性能提升也难以理解

下面几行代码intnrows=4096;intncols=4096;size_tnumel=nrows*ncols;unsignedchar*buff=(unsignedchar*)malloc(numel);unsignedchar*pbuff=buff;#pragmaompparallelforschedule(static),firstprivate(pbuff,nrows,ncols),num_threads(1)for(inti=0;i编译时需要11130usecs在我的i5-3230M上运行g++-omainmain.cpp-std=c++0x-O3也就是说,当openmp

c++ - VC++ 11 中的 std::thread 类导致随机崩溃。任何解决方法?

我在VisualStudio11DeveloperPreview中遇到了一个错误,至少我认为这是一个错误并报告了它,但我很想知道是否有人知道解决方法。当我使用std::thread类创建多个线程时,它会导致应用程序崩溃。有时它会抛出异常,有时会导致访问冲突,有时它会起作用。重现错误的代码如下所示:#include#include#include#includeint_tmain(intargc,_TCHAR*argv[]){std::vectorthreads;for(inti=0;ijoin();deletethreads[i];}return0;}使用静态或动态CRT库并不重要(它