problems with threads

1. Security issues

Security means "nothing bad can ever happen".

Thread safety issues are mainly related to synchronization. Without good synchronization, the order of operations in multiple threads is unpredictable, and the correctness of the results cannot be guaranteed.

Race Condition : A race condition occurs when the correctness of a computation depends on the timing of alternate execution of multiple threads. The most typical is "detection before execution", such as lazy instantiation (singleton pattern is the most typical lazy instantiation).

2. Activity problems

Activity is concerned with "something right will happen eventually". Liveness problems occur when an operation cannot proceed further.

In serial programs, one of the forms of the liveness problem is an infinite loop. In threads, liveness problems also include: deadlock , starvation , and livelock .

3. Performance issues

Performance problems include many aspects: long service time, insensitive response, low throughput, high resource consumption, low scalability, etc.

In a multithreaded program, when threads switch, a context switch operation occurs. If switching between threads is frequent, this operation will bring great overhead: saving and restoring the execution context, loss of locality, more CPU time is spent on thread scheduling rather than thread execution. However, when threads share data, synchronization mechanisms must be used, and these mechanisms tend to inhibit certain compiler optimizations, invalidate data in memory buffers, and increase synchronization traffic on the shared memory bus. These factors will bring additional performance overhead.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325296303&siteId=291194637