[Personal record] C++ interview summary 2 (multithreaded)

[Personal record] C++ interview summary 2 (multithreaded)

Since I am a rookie, I am not rigorous, welcome to correct me, and keep running on the road of learning

For example, the interviewer asks a multi-threaded question, and then can appropriately expand the multi-threaded question, so as not to ask you to answer this question, you can not fully demonstrate your skills, proper development can add points to the interview.

Start:

What is a process and what is a thread?

Process: The smallest unit of system scheduling and allocation. Thread: The smallest unit of CPU scheduling and allocation.
A process has at least one thread, which is the main thread.
The main purpose of using multithreading in development is to improve the concurrency of the product! Improve operational efficiency!

How to create threads?

Multi-threading technology is introduced in C++11. To manage threads through thread-like objects, you
only need #include. The creation of thread-like objects means the beginning of a thread.
Thread first(thread function name, parameter 1, parameter 2,...);
first.join(); // Wait for the thread to end, join is a blocking function.
Each thread has a thread function, and what the thread has to do is written in the thread function.

Thread synchronization problem

The resources in a process are accessible by all threads. Some resources can be accessed at the same time, and some resources can only be accessed by one thread at a time, such as a static global variable a. Thread 1 is responsible for writing data into it, and thread 2 needs to read it. Thread 2 must have written data to Thread 1 before reading. If not, Thread 2 must stop and wait for the end of Thread 1's operation. This is the cooperative relationship between threads in some places. It is coordinated. The "same" of thread synchronization means collaborative cooperation.

So how to wait, you need to add lock control before thread 1 uses this variable a to ensure that a is only used by thread 1 at this time, release the lock after use, and other threads can queue to use a after release.

Following the previous paragraph, if the lock is not released after the thread is locked at this time, the other threads that are to use the variable a will enter the waiting blocking state. If the lock has not been released in thread 1, but the lock cannot be opened, then it is dead A state of the lock, once released, the deadlock will be resolved. This is also the easiest way to cause and solve deadlocks.

How to resolve deadlock

The principle of solving deadlock: If another thread will depend on the current thread, then don't let the current thread depend on that thread anymore, even if there is a multi-layer relationship, the principle remains the same.

(1) If the two threads both lock lock A first and then lock B, the deadlock is avoided. If thread 1 locks lock A first and waits for lock B, thread 2 locks lock B first, and waits for lock A, causing deadlock and waiting for each other.
(2), you can use std::lock(mutex1,mutex2); instead of mutex1.lock(); mutex2.lock();.
Because the function of std::lock(mutex1,mutex2); is to lock mutex1 and mutex2 successfully at the same time. Once a mutex cannot be locked, the thread will be stuck here until the two locks can be locked simultaneously. The deadlock phenomenon is avoided.

Guess you like

Origin blog.csdn.net/hwx802746/article/details/108862963