Skip to content

Instantly share code, notes, and snippets.

@jitpaul

jitpaul/packaged_task.cpp Secret

Created Feb 18, 2020
Embed
What would you like to do?
Multithreading
#include <iostream>
#include <future>
#include <thread>
int someTask(){
std::cout << "Some task\n";
}
int main(){
// Execute the task in the same thread.
std::packaged_task<void()> task1(someTask);
auto f1 = task1.get_future();
task();
int ret1 = f1.get();
// Execute the task in a separate thread.
std::packaged_task<void()> task2(someTask);
auto f2 = task2.get_future();
std::thread(task);
int ret2 = f2.get();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment