Skip to content

Instantly share code, notes, and snippets.

@facontidavide
Last active August 22, 2016 10:52
Show Gist options
  • Save facontidavide/7d8724794cf6ad68e147125795517603 to your computer and use it in GitHub Desktop.
Save facontidavide/7d8724794cf6ad68e147125795517603 to your computer and use it in GitHub Desktop.
#include <atomic>
#include <chrono>
#include <deque>
#include <iostream>
#include <mutex>
#include <future>
#include <thread>
// shared stuff:
std::deque<std::packaged_task<void()>> tasks;
std::mutex tasks_mutex;
std::atomic<bool> gui_running;
void message(){
std::cout << std::this_thread::get_id() << std::endl;
}
void one_off()
{
std::packaged_task<void()> task(message);
std::future<void> result = task.get_future();
{
std::lock_guard<std::mutex> lock(tasks_mutex);
tasks.push_back(std::move(task));
}
// wait on result
result.get();
}
void gui_thread()
{
std::cout << "gui thread: "; message();
while (gui_running) { // process messages
{
std::unique_lock<std::mutex> lock(tasks_mutex);
while (!tasks.empty()) {
auto task(std::move(tasks.front()));
tasks.pop_front();
// unlock during the task
lock.unlock();
task();
lock.lock();
}
}
// pretend you are doing "gui work"
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
}
int main()
{
gui_running = true;
std::cout << "main thread: "; message();
std::thread gt(gui_thread);
for (unsigned i = 0; i < 5; ++i) {
// note: these will be launched sequentially because result's
// destructor will block until one_off completes
std::async(std::launch::async, one_off);
}
// the for loop will not complete until all the tasks have been
// processed by gui_thread
// ...
// cleanup
gui_running = false;
gt.join();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment