当前位置: 首页>>代码示例>>C++>>正文


C++ Future::wait方法代码示例

本文整理汇总了C++中Future::wait方法的典型用法代码示例。如果您正苦于以下问题:C++ Future::wait方法的具体用法?C++ Future::wait怎么用?C++ Future::wait使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Future的用法示例。


在下文中一共展示了Future::wait方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1:

TEST(Parallel_Test, Future_Test) {
	reset_counters();
	TestCls* values = new TestCls[DEFAULT_PARALLEL_THREAD_COUNT];
	for (unsigned x = 0; x < DEFAULT_PARALLEL_THREAD_COUNT; ++x)
		values[x] = x;

	Future future;
	parallel.process(values, &TestCls::operator*=, DEFAULT_PARALLEL_THREAD_COUNT, 0, &future, 2);

	ASSERT_EQ(DEFAULT_PARALLEL_THREAD_COUNT, future.worker_count);
	future.wait();
	ASSERT_EQ(future.finished_count, future.worker_count);
	delete[] values;
}
开发者ID:oliverjose,项目名称:Protheus,代码行数:14,代码来源:TestParallel.cpp

示例2: collectAll

TEST(Wait, waitWithDuration) {
 {
  Promise<int> p;
  Future<int> f = p.getFuture();
  f.wait(milliseconds(1));
  EXPECT_FALSE(f.isReady());
  p.setValue(1);
  EXPECT_TRUE(f.isReady());
 }
 {
  Promise<int> p;
  Future<int> f = p.getFuture();
  p.setValue(1);
  f.wait(milliseconds(1));
  EXPECT_TRUE(f.isReady());
 }
 {
  vector<Future<bool>> v_fb;
  v_fb.push_back(makeFuture(true));
  v_fb.push_back(makeFuture(false));
  auto f = collectAll(v_fb);
  f.wait(milliseconds(1));
  EXPECT_TRUE(f.isReady());
  EXPECT_EQ(2, f.value().size());
 }
 {
  vector<Future<bool>> v_fb;
  Promise<bool> p1;
  Promise<bool> p2;
  v_fb.push_back(p1.getFuture());
  v_fb.push_back(p2.getFuture());
  auto f = collectAll(v_fb);
  f.wait(milliseconds(1));
  EXPECT_FALSE(f.isReady());
  p1.setValue(true);
  EXPECT_FALSE(f.isReady());
  p2.setValue(true);
  EXPECT_TRUE(f.isReady());
 }
 {
  auto f = makeFuture().wait(milliseconds(1));
  EXPECT_TRUE(f.isReady());
 }

 {
   Promise<Unit> p;
   auto start = std::chrono::steady_clock::now();
   auto f = p.getFuture().wait(milliseconds(100));
   auto elapsed = std::chrono::steady_clock::now() - start;
   EXPECT_GE(elapsed, milliseconds(100));
   EXPECT_FALSE(f.isReady());
   p.setValue();
   EXPECT_TRUE(f.isReady());
 }

 {
   // Try to trigger the race where the resultant Future is not yet complete
   // even if we didn't hit the timeout, and make sure we deal with it properly
   Promise<Unit> p;
   folly::Baton<> b;
   auto t = std::thread([&]{
     b.post();
     /* sleep override */ std::this_thread::sleep_for(milliseconds(100));
     p.setValue();
   });
   b.wait();
   auto f = p.getFuture().wait(std::chrono::seconds(3600));
   EXPECT_TRUE(f.isReady());
   t.join();
 }
}
开发者ID:BocaiFire,项目名称:folly,代码行数:71,代码来源:WaitTest.cpp

示例3: wait

 static void wait(Future& f, int n) {
   f.wait(n);
 }
开发者ID:fullstackenviormentss,项目名称:sbmt,代码行数:3,代码来源:signal.hpp

示例4: rundemos

// The primary thread function, which issues calls through the secondary thread
void rundemos()
{
	// Create an event for which our second thread will wait
	hExternalEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

	// Create the secondary thread
	DWORD dwThreadId;
	HANDLE hThread = CreateThread(NULL, 0, demoThread, NULL, 0, &dwThreadId);

	// Obtain a scheduler instance, through which we can make cross thread calls
	CallScheduler<APCPickupPolicy>* scheduler = CallScheduler<APCPickupPolicy>::getInstance();

	// Do a first cross thread call, to a function returning a string
	try
	{
		string dataString = scheduler->syncCall<string, ExceptionTypes<std::exception>>(dwThreadId, boost::bind(demoFunction, 'a'), INFINITE);
		cout << "demoFunction returned: " << dataString << endl;
	}
	catch(CallTimeoutException&)
	{
		cout << "Call timeout." << endl;
	}
	catch(CallSchedulingFailedException&)
	{
		cout << "Call scheduling failed -- Probably a broken pickup policy." << endl;
	}
	catch(std::exception& e)
	{
		cout << "The scheduled call threw a std exception: " << e.what() << endl;
	}

	// Do a second cross thread call, to a function returning nothing
	try
	{
		// Expect a std::exception or DemoException
		scheduler->syncCall<void, ExceptionTypes<std::exception, DemoException>>(dwThreadId, boost::bind(demoVoidFunction, 'a'), 500);
	}
	catch(CallTimeoutException&)
	{
		cout << "Call timeout" << endl;
	}
	catch(CallSchedulingFailedException&)
	{
		cout << "Call scheduling failed -- Probably a broken pickup policy." << endl;
	}
	catch(std::exception& e)
	{
		cout << "demoVoidFunction threw a std exception: " << e.what() << endl;
	}
	catch(DemoException&)
	{
		cout << "demoVoidFunction threw a DemoException" << endl;
	}

    // Do a third cross thread call, to a function returning an int
    try
    {
        // Expect a std::exception or DemoException
        int demoInt = scheduler->syncCall<int, ExceptionTypes<std::exception, DemoException>>(dwThreadId, boost::bind(demoIntFunction, '!'), 500);
        cout << "demoIntFunction returned: " << demoInt << endl;
    }
    catch(CallTimeoutException&)
    {
        cout << "Call timeout" << endl;
    }
    catch(CallSchedulingFailedException&)
    {
        cout << "Call scheduling failed -- Probably a broken pickup policy." << endl;
    }
    catch(std::exception& e)
    {
        cout << "demoIntFunction threw a std exception: " << e.what() << endl;
    }
    catch(DemoException&)
    {
        cout << "demoIntFunction threw a DemoException" << endl;
    }

    // Do a fourth cross thread call, to a function returning an int
    try
    {
        boost::function<int()> f = boost::bind(demoIntFunctionDelayed, '!');
        Future<int> futureDemoInt = scheduler->asyncCall<ExceptionTypes<DemoException, std::exception>>(dwThreadId, f);
        
        //Future<int> futureDemoInt2 = scheduler->asyncCall<ExceptionTypes<DemoException, std::exception>>(dwThreadId, boost::bind(demoIntFunctionDelayed, '!'));
        Future<void> futureDemoInt3 = scheduler->asyncCall<void>(dwThreadId, boost::bind(demoIntFunctionDelayed, '?'));
        Future<int> futureDemoInt4 = scheduler->asyncCall<int>(dwThreadId, boost::bind(demoIntFunctionDelayed, '!'));
        Future<int> futureDemoInt5 = scheduler->asyncCall<int, ExceptionTypes<DemoException>>(dwThreadId, boost::bind(demoIntFunctionDelayed, '!'));
        Future<void> futureDemoInt6 = scheduler->asyncCall<void, ExceptionTypes<DemoException>>(dwThreadId, boost::bind(demoIntFunctionDelayed, '!'));
        Future<void> futureDemoInt7 = scheduler->asyncCall<ExceptionTypes<DemoException>, void>(dwThreadId, boost::bind(demoIntFunctionDelayed, '!'));
        Future<int> futureDemoInt8 = scheduler->asyncCall<ExceptionTypes<DemoException>, int>(dwThreadId, boost::bind(demoIntFunctionDelayed, '!'));

        while(futureDemoInt.wait(10) == ASYNCH_CALL_PENDING)
        {
            cout << "Still waiting ..." << endl;
        }
        cout << "demoIntFunctionDelayed returned: " << futureDemoInt.getValue() << endl;
    }
    catch(CallSchedulingFailedException&)
//.........这里部分代码省略.........
开发者ID:einarwebtop,项目名称:foo-bar,代码行数:101,代码来源:ThreadSynchDemo.cpp


注:本文中的Future::wait方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。