Created
January 22, 2016 08:15
-
-
Save JamesWCCheng/3696d7897012a20c8688 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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