Skip to content

Instantly share code, notes, and snippets.

@jmlyn
Created April 24, 2020 04:27
Show Gist options
  • Select an option

  • Save jmlyn/131e028f2d53c2242c4119f54d4b6ae6 to your computer and use it in GitHub Desktop.

Select an option

Save jmlyn/131e028f2d53c2242c4119f54d4b6ae6 to your computer and use it in GitHub Desktop.
#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