Home > Article > Backend Development > How to implement concurrency control in multi-threaded programming?
How to implement concurrency control in multi-thread programming?
With the development of computer technology, multi-threaded programming has become an indispensable part of modern software development. Multi-threaded programming can improve the performance and responsiveness of the program, but it also brings problems with concurrency control. In a multi-threaded environment, multiple threads accessing shared resources at the same time may cause data competition and operation errors. Therefore, achieving effective concurrency control is an important part of ensuring the correct execution of the program.
In the process of implementing concurrency control in multi-thread programming, we usually use the following common technologies:
#include <iostream> #include <mutex> #include <thread> std::mutex mtx; void printHello(int threadNum) { mtx.lock(); std::cout << "Hello from thread " << threadNum << "!" << std::endl; mtx.unlock(); } int main() { std::thread t1(printHello, 1); std::thread t2(printHello, 2); t1.join(); t2.join(); return 0; }
In the above code, we created two threads and called the printHello function to output the thread number. Since the mutex mtx is locked inside the printHello function, only one thread can access std::cout at any time, avoiding confusing output results.
#include <iostream> #include <condition_variable> #include <mutex> #include <thread> std::mutex mtx; std::condition_variable cv; bool ready = false; void printHello(int threadNum) { std::unique_lock<std::mutex> lock(mtx); cv.wait(lock, [] { return ready; }); std::cout << "Hello from thread " << threadNum << "!" << std::endl; } int main() { std::thread t1(printHello, 1); std::thread t2(printHello, 2); std::this_thread::sleep_for(std::chrono::seconds(2)); { std::lock_guard<std::mutex> lock(mtx); ready = true; } cv.notify_all(); t1.join(); t2.join(); return 0; }
In the above code, we created two threads and called the printHello function to output the thread number. In the initial state, the ready variable is false, so the two threads wait on the condition variable cv. When we set ready to true in the main function, we notify the waiting thread through cv.notify_all(), and the two threads are awakened and output the results respectively.
#include <iostream> #include <atomic> #include <thread> std::atomic<int> counter(0); void increment() { for (int i = 0; i < 100000; i++) { counter.fetch_add(1, std::memory_order_relaxed); } } int main() { std::thread t1(increment); std::thread t2(increment); t1.join(); t2.join(); std::cout << "Counter: " << counter << std::endl; return 0; }
In the above code, we created two threads to perform 100,000 atomic addition operations on the counter. Since atomic operations are uninterruptible, concurrent access to counter does not cause data races.
Through common concurrency control technologies such as mutex locks, condition variables and atomic operations, we can achieve effective concurrency control in multi-thread programming and ensure the correct execution of the program.
To sum up, the following points need to be paid attention to when implementing concurrency control in multi-thread programming: First, avoid data competition and operation errors, and adopt appropriate concurrency control technology. Secondly, the synchronization mechanism must be designed reasonably to avoid problems such as deadlock and starvation. Finally, the performance of concurrency control needs to be tested and tuned to ensure efficient execution of the program.
Through continuous learning and practice, the application of concurrency control in multi-threaded programming will become more proficient and flexible, and we can write safer and more efficient multi-threaded programs.
The above is the detailed content of How to implement concurrency control in multi-threaded programming?. For more information, please follow other related articles on the PHP Chinese website!