Little bit more expensive than gold

Source: Internet
Author: User

I. Thread Synchronization

Basic Principles:

In the synchronization process, the two most important concepts are:Synchronization object and wait Function.

During thread synchronization, a synchronization object needs to be defined first. The synchronization object generally has two states: flag (set bit, signaled) and unsignaled (unsignaled, nonsignaled ). The thread sets the synchronization object as a flag or not based on whether the operation has been completed. The wait function is used to wait for the state change of the synchronization object. When a thread calls a function and waits for the function, the execution will be paused until the state of the synchronization object changes. The thread will continue to execute the function only after the function is returned. Wait functions are divided into "single object" Wait functions and "multi-object" Wait functions.

Thread Synchronization Process:

The synchronization object works with the wait function to implement thread synchronization. For example, if thread a requires thread B to prepare data for a certain operation, then thread synchronization is required. Thread a waits for thread B to run until the required data is ready. The process of using the synchronization object and the wait function is basically as follows.
◇ Define a synchronization object in the process for thread synchronization. The synchronization object must be global to ensure that both thread a and thread B can access the synchronization object.
◇ At the beginning, thread a and thread B are executed independently of each other.
◇ Thread B sets the synchronization object to "unlabeled" before preparing the data required by thread A. After thread B prepares the data required by thread, change the status of the synchronization object to marked ".
◇ Thread a runs until the data prepared by thread B is required, and the waiting function is called. If the synchronization object is not "marked", wait until the state of the synchronization object changes to "marked. After the synchronization object is set to "marked" by "B" (indicating that thread B has completed data preparation), wait until the function returns, and thread a continues to execute.
◇ So forth.

Ii. pointer Parameters

1. the return value of the function is pointer type. check whether it is static memory pointer or heap memory pointer or stack memory pointer. The stack memory pointer must never be dropped!

2. The function needs to pass in and out through the pointer parameter. In the function, you can only modify the value (* P) pointed to by the pointer, but cannot modify the pointer pointing, that is, the pointer address! (The address of the pointer parameter cannot be modified in the function; otherwise, use the pointer !).


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.