Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Mutex Example---
- #include <iostream>
- #include <thread>
- #include <string>
- #include <mutex> //Get access to mutex
- using namespace std;
- mutex mutex_variable; //Creating a mutex
- void shared_print(string input_string, int id)
- {
- mutex_variable.lock();
- cout << input_string << id << endl;
- mutex_variable.unlock();
- }
- void function_1()
- {
- for(int i = 0; i > -100; i--)
- {
- shared_print(string("From thread 1: "), i);
- }
- }
- int main()
- {
- thread t1(function_1);
- for(int i = 0; i < 100; i++)
- shared_print(string("From main: "), i);
- t1.join();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement