Skip to content

Instantly share code, notes, and snippets.

@jitpaul
Created February 18, 2020 02:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jitpaul/eb7182c05d4040a6b0e31bfbf8c158f9 to your computer and use it in GitHub Desktop.
Save jitpaul/eb7182c05d4040a6b0e31bfbf8c158f9 to your computer and use it in GitHub Desktop.
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