Home  >  Article  >  Backend Development  >  How to efficiently manage shared resources in C++ multi-threaded programming?

How to efficiently manage shared resources in C++ multi-threaded programming?

PHPz
PHPzOriginal
2024-06-04 18:52:031110browse

In C++ multi-threaded programming, using mutex and condition variables can efficiently manage shared resources and avoid data competition and deadlock: a mutex (Mutex) allows only one thread to access resources at a time, ensuring data integrity. Condition variables (Condition Variable) are used to coordinate thread cooperation, allowing one thread to wait for another thread to perform a specific action. In the actual case, the producer thread adds data to the buffer and notifies the consumer. The consumer thread waits for the producer notification and then reads the data from the buffer. Mutexes and condition variables ensure thread-safe access to shared resources.

C++ 多线程编程中如何高效地管理共享资源?

C++ Multi-threaded programming: efficient management of shared resources

In multi-threaded programming, access control of shared resources is crucial . This article will explain how to use synchronization primitives such as mutex and condition variables in C++ to efficiently manage shared resources and avoid data races and deadlocks.

Mutex (Mutex)

Mutex is a key mechanism to protect shared resources from concurrent access. It allows only one thread to access resources at a time, thus ensuring data integrity.

std::mutex m;  // 创建一个互斥量

void access_resource() {
  std::lock_guard<std::mutex> lock(m);  // 加锁
  // 对共享资源执行操作
  lock.unlock();  // 解锁
}

Condition Variable

Condition variable is used to coordinate thread cooperation. It allows one thread to wait for another thread to perform a specific action.

std::condition_variable cv;  // 创建一个条件变量
std::mutex m;  // 创建一个与条件变量关联的互斥量

void produce() {
  std::unique_lock<std::mutex> lock(m);
  // 生产数据
  lock.unlock();
  cv.notify_one();  // 通知消费者生产完成
}

void consume() {
  std::unique_lock<std::mutex> lock(m);
  // 等待生产者通知
  cv.wait(lock);
  // 消费数据
  lock.unlock();
}

Practical case

The following is a simple example of using mutex and condition variables to manage shared resources. It uses a buffer to store data, the producer thread adds data to the buffer, and the consumer thread reads data from the buffer.

#include <mutex>
#include <condition_variable>
#include <thread>
#include <vector>

std::mutex m;
std::condition_variable cv;

std::vector<int> buffer;  // 共享资源缓冲区

void produce() {
  while (true) {
    std::lock_guard<std::mutex> lock(m);
    buffer.push_back(rand());
    cv.notify_one();
  }
}

void consume() {
  while (true) {
    std::unique_lock<std::mutex> lock(m);
    cv.wait(lock, [] { return !buffer.empty(); });
    int data = buffer.back();
    buffer.pop_back();
  }
}

int main() {
  std::thread t1(produce);
  std::thread t2(consume);
  t1.join();
  t2.join();
  return 0;
}

By using mutex and condition variables, we ensure that the producer and consumer threads can access the buffer at the same time, but there will be no data race.

The above is the detailed content of How to efficiently manage shared resources in C++ multi-threaded programming?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn