Skip to content

Instantly share code, notes, and snippets.

@JamesWCCheng
Created January 22, 2016 08:15
Show Gist options
  • Save JamesWCCheng/3696d7897012a20c8688 to your computer and use it in GitHub Desktop.
Save JamesWCCheng/3696d7897012a20c8688 to your computer and use it in GitHub Desktop.
/* g++ -std=c++11 std_async.cpp -lpthread */
#include <iostream>
#include <thread>
#include <future>
#include <chrono>
#include <vector>
#include <mutex>
using namespace std;
thread_local int tls = 0;
std::mutex g_pages_mutex;
int Task(int a, int b)
{
cout<<"Thread ID = "<<std::this_thread::get_id()<<" Enter"<<endl;
while(true)
{
std::lock_guard<std::mutex> guard(g_pages_mutex);
std::this_thread::sleep_for(std::chrono::seconds(1));
cout<<"Thread ID = "<<std::this_thread::get_id()<<" Running"<<endl;
}
return b;
}
int main()
{
std::thread::id this_id = std::this_thread::get_id();
cout << "Main thread id = " << this_id << endl;
auto fut = std::async(std::launch::async ,
Task, 1, 7788);
fut = std::async(std::launch::async ,
Task, 1, 7788);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment