#include <iostream>
#include <mutex>
#include <thread>
std::mutex mMutex; // Define a mutex
int sharedData = 0; // Shared data
void incrementData() {
std::lock_guard<std::mutex> lock(mMutex); // Lock the mutex
// Simulate some work
std::this_thread::sleep_for(std::chrono::milliseconds(100));
sharedData++; // Increment the shared data
// Display the updated shared data
std::cout << "Thread ID: " << std::this_thread::get_id() << " incremented the data to: " << sharedData << std::endl;
}
int main() {
std::thread t1(incrementData);
std::thread t2(incrementData);
t1.join();
t2.join();
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: