Share c++ mutex source code

tuquynh237

New member
## C ++ Mã nguồn Mutex

[Liên kết đến bài viết tham khảo]

Mutex là một nguyên thủy đồng bộ hóa cho phép một luồng truy cập vào một tài nguyên được chia sẻ trong khi các luồng khác bị chặn.Trong C ++, mutexes được triển khai bằng lớp `std :: mutex`.Mã sau đây cho thấy cách tạo một mutex và sử dụng nó để bảo vệ tài nguyên được chia sẻ:

`` `C ++
#include <Istream>
#include <slex>

int main () {
// Tạo một mutex.
std :: mutex mutex;

// Tạo một tài nguyên được chia sẻ.
int chia sẻ_resource = 0;

// Bắt đầu hai chủ đề.
std :: ThreadRen1 ([&] {
// có được mutex.
MUTEX.LOCK ();

// Cập nhật tài nguyên được chia sẻ.
Shared_Resource ++;

// Phát hành mutex.
Mutex.unlock ();
});

std :: ThreadRen2 ([&] {
// có được mutex.
MUTEX.LOCK ();

// Cập nhật tài nguyên được chia sẻ.
Shared_Resource ++;

// Phát hành mutex.
Mutex.unlock ();
});

// Chờ các chủ đề kết thúc.
Thread1.join ();
Chủ đề2.join ();

// In giá trị của tài nguyên được chia sẻ.
std :: cout << "shared_resource =" << shared_resource << std :: endl;

trả lại 0;
}
`` `

Trong mã này, đối tượng `std :: Mutex` được sử dụng để bảo vệ tài nguyên được chia sẻ` shared_resource`.Khi một luồng gọi phương thức `lock ()` trên mutex, nó bị chặn cho đến khi mutex được mở khóa bởi một luồng khác.Điều này đảm bảo rằng chỉ có một luồng có thể truy cập tài nguyên được chia sẻ tại một thời điểm.

Mutexes là một công cụ mạnh mẽ để ngăn chặn các điều kiện chủng tộc trong các chương trình đa luồng.Tuy nhiên, điều quan trọng là sử dụng chúng một cách chính xác để tránh bế tắc.

## hashtags

* #C ++
* #Mutex
* #MultithReading
* #synchronization
* #Điều kiện của cuộc đua
=======================================
## C++ Mutex Source Code

[Link to reference article]

Mutex is a synchronization primitive that allows one thread to access a shared resource while other threads are blocked. In C++, mutexes are implemented using the `std::mutex` class. The following code shows how to create a mutex and use it to protect a shared resource:

```c++
#include <iostream>
#include <mutex>

int main() {
// Create a mutex.
std::mutex mutex;

// Create a shared resource.
int shared_resource = 0;

// Start two threads.
std::thread thread1([&] {
// Acquire the mutex.
mutex.lock();

// Update the shared resource.
shared_resource++;

// Release the mutex.
mutex.unlock();
});

std::thread thread2([&] {
// Acquire the mutex.
mutex.lock();

// Update the shared resource.
shared_resource++;

// Release the mutex.
mutex.unlock();
});

// Wait for the threads to finish.
thread1.join();
thread2.join();

// Print the value of the shared resource.
std::cout << "shared_resource = " << shared_resource << std::endl;

return 0;
}
```

In this code, the `std::mutex` object is used to protect the shared resource `shared_resource`. When a thread calls the `lock()` method on the mutex, it is blocked until the mutex is unlocked by another thread. This ensures that only one thread can access the shared resource at a time.

Mutexes are a powerful tool for preventing race conditions in multi-threaded programs. However, it is important to use them correctly to avoid deadlocks.

## Hashtags

* #C++
* #Mutex
* #MultithReading
* #synchronization
* #RACE condition
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top