我有一个必须向客户端返回未来结果( future /shared_future)的API。有些情况下,我知道我可以多次返回“相同的未来”对象,因为我有一个关联的std::promise,它可以/应该将结果传递给多个客户端(伪代码)。
std::future<int> getVal()
{
//I have a list of promises kept
if (promiseExists)
{
//this throws an exception because get_future() can be called only one time, unfo
一旦调用了std::future.get(),就会变得无效,因为对future.valid()的调用将确认。以下代码段将在运行时失败,错误为g++ 4.7.0
terminate called after throwing an instance of 'std::future_error'
what(): No associated state
我试图对th1和th2的依赖进行编码,两者都在等待th0的完成。
问题是不能从两个线程调用std::future.get()。
我可以想到一些涉及condition_variable的修复,或者通过队列来传递结果等等。
是合法的,因为std::shared_future是可以复制的。
#include <future>
#include <vector>
#include <iostream>
int factorial(std::shared_future<int> sf)
{
int res = 1;
int num = sf.get();
for(int i=num; i>1; i--)
{
res *= i;
}
return res;
}
int main()
{
s
我试着测试std::shared_future是如何在不同线程之间共享的,因为这些线程都调用了它的wait()函数,并且在调用它的signal之后醒来。详情如下:
#include <iostream>
#include <future>
using namespace std;
int main() {
promise<void> p, p1, p2;
auto sf = p.get_future().share(); // This line
auto f1 = [&]() {
p1.set_value()
为了执行一些性能测试,我需要在程序中的特定点启动一批线程。不幸的是,我不得不采用基于线程的方式,不能使用任务(std::异步),因为我必须将给定的线程固定到特定的核心(使用关联)。为了实现这种行为,我使用了提到的RAII“一枪”方法。
到目前为止,这是我的代码:
template < class T >
typename std::decay< T >::type decay_copy( T&& v ) {
return std::forward< T >( v );
}
/**
* delayed thread - more or
我希望启动一个未定义的线程数量(在编译时),并一起等待它们的结果。
我想使用boost::线程(和boost::boost,::shared_future)。我有以下代码:
为了简单起见,它只返回一个整数值。
class Test
{
public:
static void histograms(boost::promise<int>& p)
{
p.set_value(16);
}
};
以下是我所说的使用承诺和未来:
int main()
{
// vector with final results
std::vector<int>
如果我这样做的话:
std::promise<void> p;
int a = 1;
std::thread t([&] {
a = 2;
p.set_value();
});
p.get_future().wait();
// Is the value of `a` guaranteed to be 2 here?
对set_value()有这样的看法,但我不知道它意味着什么:
对该函数的调用不引入调用get_future的数据竞赛(但它们不需要彼此同步)。
set_value()和wait()是否提供了获取/发布同步(或其他形式)?
对于如何删除从异步函数中调用的指针,我感到非常困惑。我有什么?
Obj* doStuff(int size){
Obj *myObject = new Obj(size);//where do I delete this?
myObject.doSomeOtherStuffWhichChangesMyObject();
return myObject;
}
int main (){
for (int i = 0; i < CORES; i++)
{
std::shared_future<Obj*> f
C++11标准说:
30.6.6类模板未来
(3)“对未定义valid() == false的未来对象调用析构函数、移动赋值运算符或有效的任何成员函数的效果。”
那么,这是否意味着下面的代码可能会遇到未定义的行为?
void wait_for_future(std::future<void> & f)
{
if (f.valid()) {
// what if another thread meanwhile calls get() on f (which invalidates f)?
f.wait();
}
我有一个异步C++函数,它需要将工作传递给另一个线程,然后等待工作完成。我使用std::promise对象完成了这一任务,如下所示:
void task(std::function<void()> const& func) {
std::promise<void> promise;
//You can think of 'thread_pool' as being a wrapper around a std::vector<std::thread>
//where all the threads have a
我试图将我的类实例传递到线程中,并从线程返回已处理的对象。我在谷歌上搜索了一下C++多线程,发现std::promising可能会很有帮助。
然而,我在一开始就被卡住了。下面是我的代码:
void callerFunc()
{
//...
std::promise<DataWareHouse> data_chunks;
// DataWareHouse is my customized class
//data_chunks has a vector<vector<double>> member variable
考虑到以下几点,是否有可能递归到函数中?我不认为std::move会起作用,因为在解开变量时,变量会消失,你会如何处理这样的事情呢?
auto TestFunc(std::future<bool> promisedExit
, int max
, const char* pszTesting)
{
if (promisedExit.get()) { // The calling thread will set this to cause this thread to exit.
return false;
}
...
...
当使用C++标志编译托管/clr代码时,编译器不允许包含。我试图将我的非托管C++代码移植到托管C++环境中。我看到C#有替代任务和TaskCompletionSource,以取代未来和承诺,但我没有看到这些选项在托管C++中可用。我需要与一些C++非托管库执行互操作,以便不能完全切换到C#。我还需要一个C++层在中间。如何在托管C++中实现未来/承诺功能?
下面是C++中的一个非托管代码示例,它在没有/clr标志的情况下编译:
int Foo(std::future<int> &fur) {
int result = 1;
int value = fut.