Skip to content
Snippets Groups Projects
Commit 06f0f807 authored by Yichao Yu's avatar Yichao Yu
Browse files

async

parent 40da708e
No related branches found
No related tags found
No related merge requests found
#include "async.h" #include "async.h"
#include "function.h"
#include <queue> #include <queue>
#include <thread> #include <thread>
...@@ -14,7 +15,7 @@ private: ...@@ -14,7 +15,7 @@ private:
std::mutex m_mutex; std::mutex m_mutex;
std::condition_variable m_cond; std::condition_variable m_cond;
public: public:
T PYOPENCL_INLINE T
pop() pop()
{ {
std::unique_lock<std::mutex> mlock(m_mutex); std::unique_lock<std::mutex> mlock(m_mutex);
...@@ -25,10 +26,11 @@ public: ...@@ -25,10 +26,11 @@ public:
m_queue.pop(); m_queue.pop();
return item; return item;
} }
void PYOPENCL_INLINE void
push(const T &item) push(const T &item)
{ {
{ {
// Sub scope for the lock
std::unique_lock<std::mutex> mlock(m_mutex); std::unique_lock<std::mutex> mlock(m_mutex);
m_queue.push(item); m_queue.push(item);
} }
...@@ -58,12 +60,12 @@ private: ...@@ -58,12 +60,12 @@ private:
t.detach(); t.detach();
} }
public: public:
void PYOPENCL_INLINE void
ensure_thread() ensure_thread()
{ {
std::call_once(m_flag, &AsyncCaller::start_thread, this); std::call_once(m_flag, &AsyncCaller::start_thread, this);
} }
void PYOPENCL_INLINE void
push(const std::function<void()> &func) push(const std::function<void()> &func)
{ {
ensure_thread(); ensure_thread();
......
...@@ -5,7 +5,9 @@ ...@@ -5,7 +5,9 @@
namespace pyopencl { namespace pyopencl {
// Start the helper thread
void init_async(); void init_async();
// Call @func in the helper thread
void call_async(const std::function<void()> &func); void call_async(const std::function<void()> &func);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment