C Concurrent programming takes full advantage of multi-core CPUs by creating threads, mutexes, and condition variables. Creating threads allows tasks to be executed in parallel. A mutex acts as a lock to ensure that shared data is not accessed by multiple threads simultaneously, thus avoiding data corruption. Condition variables are used to notify a thread that a specific condition has been met, and are used with mutex locks to prevent a thread from continuing to execute until the condition is met.
C Concurrent Programming: Unlocking Multi-Core CPUs
Preface
Modern CPUs Typically Having multiple cores, by fully utilizing these cores, we can significantly improve code efficiency when executing tasks in parallel. C provides a variety of concurrent programming tools that allow programmers to easily create applications that can perform multiple tasks simultaneously.
Creating threads
Creating threads is the basic building block that represents concurrency. In C, new threads can be created using the std::thread
class. It accepts as argument a callable object that specifies the task to be performed in a separate thread.
#include <iostream> #include <thread> void hello_world() { std::cout << "Hello, world!" << std::endl; } int main() { std::thread thread1(hello_world); thread1.join(); return 0; }
In the above code, the hello_world()
function is a callable object that simply prints a message to the console. std::thread
The constructor creates a new thread and executes the callable object. thread1.join()
Blocks the main thread until the new thread completes.
Mutex lock
Mutex lock is very important when threads access shared data concurrently. They act as locks to prevent multiple threads from accessing critical sections simultaneously, thereby avoiding data corruption. In C, mutex locks can be created using the std::mutex
class.
#include <iostream> #include <thread> #include <mutex> std::mutex m; // 全局互斥锁 void increment(int& counter) { std::lock_guard<std::mutex> lock(m); // 获取互斥锁 ++counter; } int main() { int counter = 0; std::thread thread1(increment, std::ref(counter)); std::thread thread2(increment, std::ref(counter)); thread1.join(); thread2.join(); std::cout << "Final counter value: " << counter << std::endl; return 0; }
In this example, the increment()
function increments the shared variable counter
. We use std::lock_guard
to obtain a mutex lock, ensuring that only one thread can execute the critical section at the same time. This mechanism ensures that two threads do not increment counter
at the same time, thus avoiding data races.
Condition variable
Condition variable is used to notify the thread that a specific condition has been met. They are used with mutex locks to ensure that threads do not continue execution until a condition is met. In C, condition variables can be created using the std::condition_variable
class.
#include <iostream> #include <thread> #include <condition_variable> #include <mutex> std::mutex m; // 全局互斥锁 std::condition_variable cv; // 全局条件变量 bool ready = false; // 共享布尔标志 void producer() { std::lock_guard<std::mutex> lock(m); // 获取互斥锁 ready = true; // 设置共享标志为 true cv.notify_one(); // 通知一个等待的线程 } void consumer() { std::unique_lock<std::mutex> lock(m); // 获取互斥锁(并锁定它) while (!ready) // 等待共享标志为 true cv.wait(lock); // 释放互斥锁并等待 } int main() { std::thread producer_thread(producer); std::thread consumer_thread(consumer); producer_thread.join(); consumer_thread.join(); return 0; }
In this example, we use condition variables to coordinate the interaction between producer and consumer threads. producer()
The function sets the shared flag ready
to true and notifies the consumer thread. consumer()
The function waits for the shared flag to be true by waiting on the condition variable, and then continues execution.
The above is the detailed content of C++ Concurrent Programming: How to use multi-core CPU to achieve concurrency?. For more information, please follow other related articles on the PHP Chinese website!