Skip to content

Commit e4ccfe3

Browse files
authored
Merge pull request boostorg#765 from lakshayg/future_then
add future::then() method
2 parents f8aa62c + a6879d2 commit e4ccfe3

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

include/boost/compute/async/future.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,21 @@ class future
8282
return m_event;
8383
}
8484

85+
#if defined(BOOST_COMPUTE_CL_VERSION_1_1) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
86+
/// Invokes a generic callback function once the future is ready.
87+
///
88+
/// The function specified by callback must be invokable with zero arguments.
89+
///
90+
/// \see_opencl_ref{clSetEventCallback}
91+
/// \opencl_version_warning{1,1}
92+
template<class Function>
93+
future& then(Function callback)
94+
{
95+
m_event.set_callback(callback);
96+
return *this;
97+
}
98+
#endif // BOOST_COMPUTE_CL_VERSION_1_1
99+
85100
private:
86101
T m_result;
87102
event m_event;
@@ -149,6 +164,21 @@ class future<void>
149164
return m_event;
150165
}
151166

167+
#if defined(BOOST_COMPUTE_CL_VERSION_1_1) || defined(BOOST_COMPUTE_DOXYGEN_INVOKED)
168+
/// Invokes a generic callback function once the future is ready.
169+
///
170+
/// The function specified by callback must be invokable with zero arguments.
171+
///
172+
/// \see_opencl_ref{clSetEventCallback}
173+
/// \opencl_version_warning{1,1}
174+
template<class Function>
175+
future<void> &then(Function callback)
176+
{
177+
m_event.set_callback(callback);
178+
return *this;
179+
}
180+
#endif // BOOST_COMPUTE_CL_VERSION_1_1
181+
152182
private:
153183
event m_event;
154184
};

test/test_event.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <future>
1919
#endif // BOOST_COMPUTE_USE_CPP11
2020

21+
#include <boost/compute/async/future.hpp>
2122
#include <boost/compute/event.hpp>
2223

2324
#include "context_setup.hpp"
@@ -85,6 +86,28 @@ BOOST_AUTO_TEST_CASE(lambda_callback)
8586
BOOST_CHECK_EQUAL(lambda_invoked, true);
8687
}
8788

89+
BOOST_AUTO_TEST_CASE(future_then_callback)
90+
{
91+
REQUIRES_OPENCL_VERSION(1,2);
92+
93+
bool callback_invoked = false;
94+
95+
boost::compute::future<void> future(queue.enqueue_marker());
96+
future.then([&](){
97+
std::lock_guard<std::mutex> lock(callback_mutex);
98+
callback_invoked = true;
99+
callback_condition_variable.notify_one();
100+
});
101+
future.wait();
102+
103+
// wait up to one second for the callback to be executed
104+
std::unique_lock<std::mutex> lock(callback_mutex);
105+
callback_condition_variable.wait_for(
106+
lock, std::chrono::seconds(1), [&](){ return callback_invoked; }
107+
);
108+
BOOST_CHECK_EQUAL(callback_invoked, true);
109+
}
110+
88111
void BOOST_COMPUTE_CL_CALLBACK
89112
event_promise_fulfiller_callback(cl_event event, cl_int status, void *user_data)
90113
{

0 commit comments

Comments
 (0)