本文整理汇总了C++中std::future类的典型用法代码示例。如果您正苦于以下问题:C++ future类的具体用法?C++ future怎么用?C++ future使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了future类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: check_for_computed_move
void GoApp::check_for_computed_move()
{
//return;
if (game_status != COMPUTER_THINKING) {
return;
}
auto status = computed_move.wait_for(std::chrono::seconds(0));
if (status == std::future_status::ready) {
try {
auto move = computed_move.get();
state.do_move(move);
// Are there any more moves possible?
if (state.get_moves().empty()) {
game_status = GAME_OVER;
}
else {
next_player();
}
} catch (std::exception& error) {
game_status = GAME_ERROR;
error_string = error.what();
}
}
}
示例2: StopHTTPServer
void StopHTTPServer()
{
LogPrint(BCLog::HTTP, "Stopping HTTP server\n");
if (workQueue) {
LogPrint(BCLog::HTTP, "Waiting for HTTP worker threads to exit\n");
workQueue->WaitExit();
delete workQueue;
workQueue = nullptr;
}
if (eventBase) {
LogPrint(BCLog::HTTP, "Waiting for HTTP event thread to exit\n");
// Give event loop a few seconds to exit (to send back last RPC responses), then break it
// Before this was solved with event_base_loopexit, but that didn't work as expected in
// at least libevent 2.0.21 and always introduced a delay. In libevent
// master that appears to be solved, so in the future that solution
// could be used again (if desirable).
// (see discussion in https://github.com/bitcoin/bitcoin/pull/6990)
if (threadResult.valid() && threadResult.wait_for(std::chrono::milliseconds(2000)) == std::future_status::timeout) {
LogPrintf("HTTP event loop did not exit within allotted time, sending loopbreak\n");
event_base_loopbreak(eventBase);
}
threadHTTP.join();
}
if (eventHTTP) {
evhttp_free(eventHTTP);
eventHTTP = 0;
}
if (eventBase) {
event_base_free(eventBase);
eventBase = 0;
}
LogPrint(BCLog::HTTP, "Stopped HTTP server\n");
}
示例3: IsReady
static bool IsReady(std::future<void>& future)
{
#if defined(__clang__) || (defined(__GNUC__) && __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 6))
return future.wait_for(std::chrono::seconds(0)) == std::future_status::ready;
#else
return future.wait_for(std::chrono::seconds(0));
#endif
}
示例4: close
/**
* Close the task. This will raise in this thread any exception the
* task generated in the other thread. Calling this function is
* optional, because the destructor will also call this function.
* But because it can throw an exception, it is better to call it
* explicitly.
*/
void close() {
// If an exception happened in the task, re-throw
// it in this thread. This will block if the task
// isn't finished.
if (m_future.valid()) {
m_future.get();
}
// Make sure task is done.
if (m_thread.joinable()) {
m_thread.join();
}
}
示例5: handleFuture
void handleFuture( std::future< _2Real::BlockResult > &obj, std::string const& info = "" )
{
obj.wait();
_2Real::BlockResult val = obj.get();
switch ( val )
{
case _2Real::BlockResult::CARRIED_OUT:
std::cout << "---- " << info << " was carried out" << std::endl;
break;
case _2Real::BlockResult::IGNORED:
std::cout << "---- " << info << " was ignored" << std::endl;
break;
}
}
示例6: getResult
std::pair<long double, int> getResult(
std::chrono::system_clock::time_point deadline, int maxPrecision)
{
long double result = 1;
int precision = 1;
if (std::chrono::system_clock::now() < deadline) {
while (precision < maxPrecision) {
const int nextPrecision = std::min(maxPrecision, precision * 2);
future = std::async(std::launch::async, compute, nextPrecision);
if (future.wait_until(deadline) == std::future_status::timeout)
break;
result = future.get();
precision = nextPrecision;
}
}
return { result, precision };
}
示例7: interruptible_wait
void interruptible_wait(std::future<T>& uf)
{
while (!this_thread_interrupt_flag.is_set())
{
if (uf.wait_for(lk, std::future_status::ready == std::chrono::milliseconds(1)))
break;
}
}
示例8: GetTimePerMove
void AI::WaitForFuture(const std::future<void>& fut, bool bPondering)
{
std::uint64_t timePerMove = GetTimePerMove();
if(bPondering)
{
timePerMove /= 2;
}
// Wait until the thread finishes or it gets timed out
if(fut.wait_for(std::chrono::nanoseconds(timePerMove)) == std::future_status::timeout)
{
// If the thread did not finish execution, signal the thread to exit, and wait till the thread exits.
m_bStopMinimax = true;
fut.wait();
m_bStopMinimax = false;
}
}
示例9: factorial1
int factorial1(std::future<int>& f)
{
int n = f.get();
int result = 1;
for (int i = 1; i <= n; ++i)
result *= i;
return result;
}
示例10: test
void test(std::future<void> fut)
{
switch (fut.wait_for(std::chrono::milliseconds(500)))
{
case std::future_status::ready: std::cout << "ready\n"; break;
case std::future_status::deferred: std::cout << "deferred\n"; break;
case std::future_status::timeout: std::cout << "timeout\n"; break;
};
}
示例11: while
//----------------------------------------------------------------------//
void SoftServo::threadFunc(std::future<bool> shutdown,
SoftServo *const servo) // change this in the future to use a thread safe shared pipe reader/writer
{
while (shutdown.wait_for(std::chrono::nanoseconds(0)) !=
std::future_status::ready)
{
servo->updateMove();
}
}
示例12: io_error
/**
* Get the header data from the file.
*
* @returns Header.
* @throws Some form of osmium::io_error if there is an error.
*/
osmium::io::Header header() {
if (m_status == status::error) {
throw io_error("Can not get header from reader when in status 'error'");
}
try {
if (m_header_future.valid()) {
m_header = m_header_future.get();
if (m_read_which_entities == osmium::osm_entity_bits::nothing) {
m_status = status::eof;
}
}
} catch (...) {
close();
m_status = status::error;
throw;
}
return m_header;
}
示例13: checkIfFinished
static bool checkIfFinished(std::future<void>& match) {
auto status = match.wait_for(std::chrono::seconds(0));
if(status == std::future_status::timeout) {
return false;
}
else if(status == std::future_status::ready) {
return true;
}
else {
return false;
}
}
示例14: while
//----------------------------------------------------------------------//
void JoyStick::threadFunc(std::future<bool> shutdown,
const JoyStick *const js)
{
while (shutdown.wait_for(std::chrono::nanoseconds(0)) !=
std::future_status::ready)
{
if (!js->readEvent())
{
js->d_owner->handleReadError();
}
}
}
示例15: on_update
void on_update(const UpdateEvent & e) override
{
// Around 60 fps
if (fixedTimer.milliseconds().count() >= 16 && turret.fired)
{
float timestep_ms = fixedTimer.milliseconds().count() / 1000.f;
turret.projectile.fixed_update(timestep_ms);
std::cout << timestep_ms << std::endl;
//std::cout << turret.projectile.p.position << std::endl;
fixedTimer.reset();
}
cameraController.update(e.timestep_ms);
time += e.timestep_ms;
shaderMonitor.handle_recompile();
// If a new mesh is ready, retrieve it
if (pointerFuture.valid())
{
auto status = pointerFuture.wait_for(std::chrono::seconds(0));
if (status != std::future_status::timeout)
{
auto m = pointerFuture.get();
supershape = m;
supershape.pose.position = {0, 2, -2};
pointerFuture = {};
}
}
// If we don't currently have a background task, begin working on the next mesh
if (!pointerFuture.valid() && regeneratePointer)
{
pointerFuture = std::async([]() {
return make_supershape_3d(16, ssM, ssN1, ssN2, ssN3);
});
}
}