Windows program design Reading Notes-Multithreading

Source: Internet
Author: User
Tags apc

I. multi-threaded Architecture

The main thread creates all the windows required by the program and includes all the window processes to process all the messages in these windows. Other threads only perform background processing, except for communication with the main thread, they do not communicate with users.

Events over 1/10 seconds (large jobs, such as spelling detection, database sorting, and printing) should not be placed in the message queue, but should be processed in a separate thread.

When the thread ends, the resources created by the thread are not automatically released.

 

Ii. multi-thread API:

Hthread = createthread (...)

DWORD winapi threadproc ([pvoid pparam)

 

_ Beginthread (...)

Void _ cdecl threadproc (void * pparam) // at the end of this function, there are usually _ endthread ();

 

Resumethread // restore the thread

 

Sleep (0) // The thread hand over the unfinished time slice.

 

Iii. msdn


Synchronization Functions

The following functions are used in synchronization.

Asynchronous Function Description
Apcproc An application-defined callback function used withQueueuserapcFunction.
Getoverlappedresult Retrieves the results of an overlapped operation.
Queueuserapc Adds a user-mode asynchronous procedure call (APC) object to the APC queue of the specified thread.

 

Critical-Section Function // critical section Description
Deletecriticalsection Releases all resources used by an unowned critical section object.
Entercriticalsection Waits for ownership of the specified critical section object.
Initializecriticalsection Initializes a critical section object.
Initializecriticalsectionandspincount Initializes a critical section object and sets the spin count for the critical section.
Leavecriticalsection Releases ownership of the specified critical section object.
Setcriticalsectionspincount Sets the spin count for the specified critical section.
Tryentercriticalsection Attempts to enter a critical section without blocking.

 

Event function // event object Description
Createevent Creates or opens a named or unnamed event object.
Openevent Opens an existing named event object.
Pulseevent Sets the specified event object to the signaled state and then resets it to the nonsignaled state after releasing the appropriate number of waiting threads.
Resetevent Sets the specified event object to the nonsignaled state.
Setevent Sets the specified event object to the signaled state.

 

Interlocked Function Description
Interlockedcompareexchange Performs an atomic comparison of the specified values and exchanges the values, based on the outcome of the comparison.
Interlockedcompareexchangepointer Performs an atomic comparison of the specified values and exchange of the values, based on the outcome of the comparison.
Interlockeddecrement Decrements (decreases by one) the value of the specified variable and checks the resulting value.
Interlockedexchange Atomically exchanges a pair of values.
Interlockedexchangeadd Performs an atomic addition of an increment value to an addend variable.
Interlockedexchangepointer Atomically exchanges a pair of values.
Interlockedincrement Increments (increases by one) the value of the specified variable and checks the resulting value.

 

Mutex function // mutex Description
Createmutex Creates or opens a named or unnamed mutex object.
Openmutex Opens an existing named mutex object.
Releasemutex Releases ownership of the specified mutex object.

 

Semaphore Function Description
Createsemaphore Creates or opens a named or unnamed semaphore object.
Opensemaphore Opens an existing named semaphore object.
Releasesemaphore Increases the count of the specified semaphore object by a specified amount.

 

Timer-queue timer function Description
Changetimerqueuetimer Updates a timer-queue timer.
Createtimerqueue Creates a queue for timers.
Createtimerqueuetimer Creates a timer-queue timer.
Deletetimerqueue Deletes a timer queue.
Deletetimerqueueex Deletes a timer queue.
Deletetimerqueuetimer Cancels a timer-queue timer.

 

Wait Function Description
Msgwaitformultipleobjects Returns when the specified criteria for the specified objects is met.
Msgwaitformultipleobjectsex Returns when the specified criteria for the specified objects is met.
Registerwaitforsingleobject Directs a wait thread in the thread pool to wait on the object.
Signalobjectandwait Allows the caller to atomically signal an object and wait on another object.
Unregisterwait Cancels a registered wait operation.
Unregisterwaitex Cancels a registered wait operation.
Waitformultipleobjects Returns when the specified criteria for the specified objects is met.
Waitformultipleobjectsex Returns when the specified criteria for the specified objects is met.
Waitforsingleobject Returns when the specified criteria for the specified object is met.
Waitforsingleobjectex Returns when the specified criteria for the specified object is met.
Waitortimercallback Returns when the specified criteria is met.

 

Waitable timer function Description
Cancelwaitabletimer Sets the specified waitable timer to the inactive state.
Createwaitabletimer Creates or opens a waitable timer object.
Openwaitabletimer Opens an existing named waitable timer object.
Setwaitabletimer Activates the specified waitable timer.
Timerapcproc Application-defined timer completion routine used withSetwaitabletimerFunction.

========================

 

Annotation.

In the critical section, only one thread can enter the critical section at any time:

Init: bool isused = false; // indicates that no thread enters the critical section.

Enter: While (isused) {sleep (1);} isused = true;

Leave: isused = false;

The critical section can only be used for coordination between different threads in the same process. Coordination between different processes must be mutually exclusive.

 

Event object usage:

Initialization

Hevent = createevent (null, false, false, null );

Beginthread (thread, 0, & Params );

Thread functions:

Void thread (pvoid)
{

Hevent =... // obtain from pvoid
While (true)
{
Waitforsingleobject (hevent, infinite );
//.......... Subject
}
}

Start execution:

Setevent (hevent );

 

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.