Multi-thread exit Algorithm in multi-core computing

Source: Internet
Author: User
Tags exit in

Multi-core basically means multithreading, so there is a tricky problem in multi-thread processing: when some resident threads access Shared data, you must end these resident threads before releasing shared data. Otherwise, the shared resources will be released first. When the subsequent resident threads access the released shared dataProgramException. For example:


After analyzing the above examples, we can find that the thread accessing the linked list still exists after the linked list is released. How can we safely exit other threads operating on the linked list before releasing the linked list? This requires us to design somethingAlgorithmInstead of simply using the thread exit function to force the exit of a thread, the thread can actively exit in the algorithm.
To safely exit the thread that allows multiple threads to operate on the linked list before the linked list, the first thing to do is to let these threads know that the release operation is required, the following situations are discussed:

1. Algorithm for exiting a single sub-Thread

This is the simplest case, because there is only one thread that can be implemented by setting an exit flag. Specifically, you need to set a exit flag before the release operation. In the sub-process, you must periodically check this flag, if the sub-process detects that this exit flag is true, it will exit the thread. During the sub-process exit process, it is also necessary to let the release thread know that the sub-process of the operation linked list has exited, this allows the subthread to send an event notification at the last step of exit, so that the release thread can be safely released. Specific solutions include:

2. Algorithm for exiting multiple threads

Compared with 1, the trouble is that there are more than one sub-thread at this time. If each sub-thread sends an event notification before exiting to the release operation thread, an event notification may be sent when the first sub-thread exits. At this time, other sub-threads are still accessing the linked list, but the released thread has received the event notification, released. At this time, it is obvious that there will be problems. The key to solving this problem is to release the linked list when the thread knows that all child threads have exited.
The key to this problem is how to let the released thread know that all child threads have been safely exited? Here I bring up a new concept-read and write locks. Write operations must be performed after all read operations are completed. The write thread uses a counting variable to determine whether there are other read operation threads. When a read operation is performed, the counter is added to 1. Once the read operation is completed, the counter is-1. When the counter is set to 0, no read operation is performed. We can use this solution here. We correspond the release operation thread to the write operation, and correspond the chain table operation thread to the read operation. A counter is maintained between them. When a thread with an operation linked list is created, the counter is added to 1. Once the operation is completed, the counter is-1, this thread then detects the counter value again. If the counter is 0, it will send an exit event to the thread that releases the linked list operation. Otherwise, it will not be sent, in this way, when the last thread performs the-1 operation on the counter, it will be 0 and an exit event will be sent. In this way, the thread that releases the operation can safely release the linked list. The implementation process of this algorithm is as follows:

As for the final source code implementation, we will not give it here. There are currently two Methods: Using locks to complete and atomic operations. Here, the addition/unlock and atomic operations are only required to maintain the counter value and prevent multithreading from simultaneously modifying it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.