Created
April 24, 2020 04:27
-
-
Save jmlyn/131e028f2d53c2242c4119f54d4b6ae6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #include <iostream> | |
| #include <utility> | |
| #include <thread> | |
| #include <string> | |
| #include <mutex> | |
| using namespace std; | |
| std::mutex mut; | |
| void SharedPrint(string msg, int id) { | |
| std::lock_guard<std::mutex> lg(mut); | |
| cout << msg << id << endl; | |
| } | |
| void f1() { | |
| for (int i = 0; i > -100; --i) { | |
| SharedPrint("From t1: ", i); | |
| } | |
| } | |
| int main() { | |
| std::thread t1(f1); | |
| for (int i = 0; i < 100; ++i) { | |
| SharedPrint("From main thread: ", i); | |
| } | |
| t1.join(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment