草庐IT

unique_future

全部标签

c++ - 阻塞许多锁/ future /等。直到任何准备就绪

是否可以阻止一组锁/future/任何可阻止的实体,直到其中任何一个准备好?这个想法是我们可以做到:std::vector>futures=...;autoready_future=wait_until_an_element_is_ready(futures);process(ready_future.get());我记得像libevent、libev和libuv这样的库对于IO任务具有这种能力。但我不知道这些是否可以用于锁/future。我想到的实现这一点的一种方法是让future在完成时调用一个处理程序,但同时将处理程序比较并交换为null,这样其他future就不能调用它。但是这

C++ promise / future : Which to return from a function?

我有一个函数返回对std::promise的引用:std::shared_ptr>play();(更多信息:该函数在某些设备上播放媒体,返回值表示播放完成。如果第二次调用play,则在第一次返回的promise上设置一个值,并新的promise为第二次调用创建并返回)然后调用者可以捕获该值并等待future:autothis_future=play()->get_future();this_future.wait();返回对promise的引用是否有意义,或者我应该返回future,以便调用函数不必调用get_future()? 最佳答案

c++ - Boost Thread 的 boost::unique_lock 是作用域锁吗?

我了解由boost::mutex::scoped_lock锁定的变量在超出范围时会自动解锁。boost::unique_lock怎么样,变量超出范围时会自动解锁吗?任何人也可以指出该功能的引用。doublex;boost::mutexx_mutex;voidfoo(){{boost::unique_locklock(x_mutex);x=rand();}......somecalculationwhichtakes10second............isxstilllockedhere???......}谢谢。 最佳答案 sco

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++ - C++11 中的 unique_ptr vector

我最近切换到C++11,并且正在尝试习惯那里的良好实践。我最终经常处理的是这样的:classOwner{private:vector>_vectorOfHeavyResources;public:virtualconstvector*GetVectorOfResources()const;};这需要我做一些事情,比如添加一个_returnableVector并翻译源vector以便以后能够返回它:_returnableVector=vector;for(inti=0;i有没有人注意到类似的问题?你的想法和解决方案是什么?我在这里得到了完整的所有权想法吗?更新:这是另一件事:如果一个类将

c++ - 如何为 std::for_each 中的 lambda 捕获 std::unique_ptr "by move"

我正在学习c++11中的新功能并遇到了这个问题。我想通过将其移动到lambda中作为for_each的参数来捕获unique_ptr。设置:std::arrayarr={1,3,5,6};std::unique_ptrp(newint);(*p)=3;尝试1-不起作用,因为unique_ptr没有复制构造函数。c++0x没有指定passbymove语法。std::for_each(arr.begin(),arr.end(),[p](int&i){i+=*p;});尝试2-使用bind将p的移动拷贝绑定(bind)到采用int&的函数:std::for_each(arr.begin(),

C++ 数组和 make_unique

作为this的后续行动发布后我想知道它的make_unique实现如何与分配函数临时缓冲区数组一起使用,例如以下代码。f(){autobuf=newint[n];//temporarybuffer//usebuf...delete[]buf;}这可以替换为对make_unique的一些调用,然后会使用[]-version的delete吗? 最佳答案 这是另一个解决方案(除了Mike的):#include#include#includetemplatetypenamestd::enable_if::value,std::unique_

c++ - 来自 T* 的 std::unique_ptr<T> 的构造函数是显式的背后的原因是什么?

由于std::unique_ptr提供了一种方便的方法来避免内存泄漏并确保异常安全,因此传递它们而不是原始指针是明智的。因此,一个人可能想要(成员)具有类似签名的函数std::unique_ptrfoo(somedata);不幸的是,在实现这样的功能时,不能简单地实现std::unique_ptrfoo(somedata){return{newsome_type(data)};//error}但必须改为std::unique_ptrfoo(somedata){returnstd::move(std::unique_ptr(newsome_type(data)));//awkward}因

c++ - 为什么 unique_ptr operator-> 不是 const-overloaded?

std::unique_ptr::operator->有签名pointeroperator->()constnoexcept;所以operator->是const但返回一个可变指针。这允许如下代码:voidmyConstMemberFunction()const{myUniquePtrMember->nonConstFunction();}为什么标准允许这样做,以及防止上述使用的最佳方法是什么? 最佳答案 把它想象成一个普通的指针:int*consti;是const指向非const的指针int.您可以更改int,但不是指针。intc

c++ - std::unique_ptr<T[]> 带有派生对象数组,使用已删除函数

在我的数值物理代码中,我需要使用unique_ptr创建一个派生对象数组,它们的类型是基类。通常,我会://HeaderfileoftheBaseclassclassParticle{public:Particle();//someconstructorvirtual~Particle();//virtualdestructorbecauseofpolymorphismvirtualfunction();//somerandomfunctionfordemonstration};//HeaderfileoftheDerivedclassclassElectron:publicParti