Windows Multithreading Synchronization Summary

Source: Internet
Author: User
Tags semaphore

1. Multi-threaded synchronization and multi-threaded mutex relationship

In fact, this is the problem I have been troubled, here I just talk about my understanding.

My understanding is that multi-threaded mutexes are for multithreaded resources.

Multi-threaded synchronization is aimed at multi-threaded timing problems.

Because the concurrency of threads leads to the uncertainty of its run time, we need to

Controls the collaborative work of multiple threads.

Let's take the example of selling tickets to distinguish between mutual exclusion and synchronization:

For example, we have two window to sell tickets, of course, here we need a global variable

To count the current number of tickets sold, the most important is that a ticket can not be sold two times,

So when window 1 is selling the nth ticket, window 2 should not be able to sell the nth ticket,

Otherwise, when there is no mutual exclusion, two of the windows may appear to sell the same ticket.

So the process of selling tickets for the two-ticket window is a mutually exclusive process.

For synchronization, we can give an example of such a ticket sale when there are two ticket-selling windows

To sell the tickets alternately. Buy one hours per window. So when the first window sells a

After hours tell the second window you should start selling, when the second window sells for one hours.

You'll be notified of the first window. You don't have to rest. Go on and sell the tickets. Of course we are not difficult to find in this process

In nature contains the process of mutual exclusion, because when the first window is sold the second window of a ticket

I can't sell it anymore.

I don't know what I'm saying. The process of thread synchronization actually contains thread mutex.

Thread mutex is a special thread synchronization situation.

2. Summary of key areas, mutex objects, event objects, semaphores

Key areas:

CriticalSection CS; Creating a key area object

InitializeCriticalSection (&CS); Initialize critical zone

EnterCriticalSection (&cs); Enter the critical area

LeaveCriticalSection (&cs); Leave the critical area

Mutex objects:

HANDLE Mu; Define mutex objects

Mu=createmutex (...); Creating a Mutex Object

Waitsingleobject (); Wait for mutex to activate

ReleaseMutex (); release ownership of Mutex objects

Event object:

HANDLE Event object;

Event=createevent (); Create Event object

Waitsingleobject (); Wait for event object to be activated

ResetEvent (); Set event object to No signal

SetEvent (); Set event object to be signaled

Signal Volume:

HANDLE sem; define Semaphore events

Sem=createsemaphore (); Create Semaphore event

Waitsingleobject (); Wait for event object to be activated

ReleaseSemaphore (); Semaphore object Count plus 1

3. Differences in how this thread is synchronized:

1. The key area belongs to the user-state object, while the other three are the kernel-state objects.

So the efficiency of using the key area is higher than the other three, because the other three kinds are used,

Will enter the kernel state

2. The key zone and the mutex object are similar, which mainly resolves mutually exclusive access to the resource. And the critical zone can only be used to

Thread synchronization, while mutex objects can be used for process synchronization

3. Event objects are primarily used for timing synchronization of threads, such as a thread that ends a notification that another thread is working

4. The signal volume is mainly used for producer consumer problems. For example, there are four bowls in the kitchen, but there are

10 people want to eat, then at the same time up to four people can eat at the same time. At this point you can put

The semaphore object's count maximum is set to 4.

4. About the difference between the CreateThread () and the _beginthread () function:

Refer to the blog of Morewindows great God: http://blog.csdn.net/morewindows/article/details/7421759

In a word, the difference is that the thread created by CreateThread () did not consider the data coverage problem caused by invoking the C run-Library function

_beginthread () automatically maintains a piece of memory for each thread at creation time for the C run-time library functions that are needed. But on the ground floor

_beginthread () is actually still calling CreateThread ()

5. About CreateThread () the creation thread executes two times :

It is best not to execute the thread immediately at the time of the call, set to Create_suspend

Then ResumeThread () can

Windows Multithreading Synchronization Summary

Related Article

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.