Multithreading common functions in Windows

Source: Internet
Author: User

 

1. Thread Creation

This function creates a new thread in the process space of the called process and returns the handle of the created thread.

Handle createthread (lpsecurity_attributes
Lpthreadattributes, DWORD Dwstacksize, Lpthread_start_routine
Lpstartaddress, Lpvoid Lpparameter, DWORD
Dwcreationflags, Lpdword Lpthreadid);

The parameters are described as follows:

Lpthreadattributes: pointer to a security_attributes structure. This structure determines the thread Security Attribute and is generally set to null;

Dwstacksize: Specifies the stack depth of a thread. Generally, it is set to 0, indicating that the stack size of the thread is the same as that of the thread that created the thread;

Lpstartaddress: the address of the function where the new thread starts to execute the time code, that is, the start address of the thread.

Generally, it is (lpthread_start_routine) threadfunc. threadfunc is the name of the thread function. The function prototype is as follows:

DWORD winapi threadfunc (lpvoid PARAM );

Lpparameter: Specifies the 32-bit parameter transmitted to the thread during thread execution, that is, the parameter of the thread function;

Dwcreationflags: used to control the additional flag created by the thread. Two values can be used: If this parameter is set to 0, the thread starts to execute immediately after being created. If this parameter is create_suincluded, after the system generates a thread, the thread is suspended and not executed immediately until the function resumethread is called;

Lpthreadid: returns the ID of the created thread;

2. Thread suspension and recovery

After a new thread is created, the thread starts execution. However, if the create_suincluded feature is used in dwcreationflags, the thread will not be executed immediately, but will be suspended first. The thread will not start until resumethread is called.

DWORD suspendthread (handle hthread );

// This function is used to suspend a specified thread. If the function is successfully executed, the thread execution is terminated.

DWORD resumethread (handle hthread );

// This function is used to end the suspension status of the thread, the execution thread.

3. thread priority operation

Int getthreadpriority (handle hthread );

// Call this function to obtain the thread priority.

Bool setthreadpriority (handle hthread, int npriority );

// Call this function to set the thread priority.

4. The thread exits.

When the function of the calling thread returns, the thread is automatically terminated.

To terminate a thread during execution, you can call the function:

Void exitthread (DWORD dwexitcode );

If the thread is terminated outside the thread, the following function can be called:

Bool terminatethread (handle hthread, DWORD dwexitcode );

Note:

(1) The terminatethread function may cause system instability and the resources occupied by threads are not released. Therefore, we recommend that you do not use this function.

(2) If the thread to be terminated is the last thread in the process, the corresponding process should be terminated after the thread is terminated.

(3) After resources are released, set the thread handle to null.

(4) After terminatethread is used, call the closehandle () function to release the stack occupied by the thread.

Inter-process communication

1. Global variables:
(1) The process and thread share the global variable, which can be used for communication.

(2) Pass the hadnle of the process to the thread function as a parameter, and then the thread can operate on the variable of the process according to this handle.

2. Message Communication Method:

Bool postmessage (hwnd, uint MSG, wparam, lparam );

Bool postthreadmessage (hwnd, uint MSG, wparam, lparam );

Obviously, they are sent to different objects: postmessage sends messages to the window, and postthreadmessage sends messages to the thread. When the hwnd of the postthreadmessage is null, it indicates that the message is passed to the current thread.

Bool postmessage (uint message, wparam = 0, lparam = 0 );

If a message is published, a non-zero value is returned; otherwise, 0 is returned.

The message parameter specifies the message to be published.

Wparam specifies the additional message information. The content of this parameter depends on the message to be published.

Lparam specifies the additional message information. The content of this parameter depends on the message to be published.

Note: This function puts a message into the message queue of the window and then returns it directly without waiting for the corresponding window to process the message. Messages in a message queue are obtained by calling the getmessage or peekmessage function of windows. You can use the postmessage function in Windows to access other applications.

Bool postthreadmessage (uint message, wparam, lparamlparam); if the return value is successful, a non-zero value is returned; otherwise, 0 is returned.

The user-defined message id.

The first message parameter of wparam.

The second message parameter of lparam.

Note: Call this function to send a custom message to other cwinthread objects. The sent message is mapped to an appropriate message processing function through the message ing macro on_thread_message.

3. Synchronization Method: For details, see http://www.cppblog.com/andxie99/archive/2006/10/10/13517.html.

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.