Win32 multithreading-Rewrite message loop

Source: Internet
Author: User

I am studying Win32 multithreading recently.ProgramDesign, which is a part of rewriting message loopCodeExamples, which may be used later.

While (! Quit | gnumprinting> 0) {// wait for next message or object being signaled DWORD dwwake; dwwake = msgwaitformultipleobjects (gnumprinting, gwintjobs, false, infinite, qs_allevents ); if (dwwake> = wait_object_0 & dwwake <wait_object_0 + gnumprinting) {// object has been signaled // reorder the handle array so we do not leave // empty slots. take the handle at the end of // the array and move It into the now-empty slot. int Index = dwwake-wait_object_0; maid [Index] = maid [gNumPrinting-1]; maid [gNumPrinting-1] = 0; gnumprinting --; sendmessage (hdlgmain, wm_threadcount, gnumprinting, 0l );} // end if else if (dwwake = wait_object_0 + gnumprinting) {While (peekmessage (& MSG, null, 0, 0, pm_remove )) {// get next message in queue if (hdlgmain = NULL |! Isdialogmessage (hdlgmain, & MSG) {If (MSG. message = wm_quit) {quit = true; exitcode = MSG. wparam; break;} // end if translatemessage (& MSG); dispatchmessage (& MSG) ;}// end while} // end while

Note:
Msgwaitformultipleobjects Function
This function not only waits for kernel objects, but also waits for messages. That is, when a message arrives, the function can return and process the message, which gives the job thread the opportunity to exit.

DWORD msgwaitformultipleobjects (
DWORD ncount, // Number of kernel objects to wait
Lphandle phandles, // array pointer of the kernel object handle to wait
Bool fwaitall, // whether to wait for all objects or a single object
DWORD dwmilliseconds, // wait time
DWORD dwwakemask); // type of the waiting message

The following describes how to use the function parameters:
DWORD ncount: Number of kernel objects to wait. If two threads are waiting to exit, ncount = 2;
Lphandle phandles: array pointer of the kernel object handle to wait.

If you only need to wait for a thread to exit, you can directly set the pointer of the thread handle:
Msgwaitformultipleobjects (1, & m_pthread-> m_hthread ,...)

If you want to wait for two threads to exit, use the following method:
Handle harray [2] = {m_pthread1-> m_hthread, m_pthread2-> m_hthread };
Msgwaitformultipleobjects (2, harray ,...)

Bool fwaitall: True-indicates that this function is returned only when all the threads waiting for exit,
False-this function returns if any of the threads to be waited have exited or a message has arrived.
In the onbutton2 () function above, I want to wait for a thread to exit and set fwaitall
False indicates that the function can be returned no matter whether the thread has actually exited or a message has arrived.
If this fwaitall is set to true, the only condition returned by the function is that the thread exits, even if
If a message arrives, this function will not return.

DWORD dwmilliseconds: Wait event, in milliseconds. Can be set to infinite, none
Long wait

DWORD dwwakemask: Type of the waiting message, which can be set to qs_allinput. This macro indicates that messages of any type can be waited. Of course, you can also specify the type of the waiting message.

# Define qs_allinput (qs_input | \
Qs_postmessage | \
Qs_timer | \
Qs_paint | \
Qs_hotkey | \
Qs_sendmessage)

Return Value: DWORD dret returns some valid information through the function. The function return value varies according to the fwaitall settings. Below are several common types of function return values:

Dret = 0 xffffffff: indicates that the function call fails. You can use getlasterror () to obtain the specific error information;

Dret = wait_object_0 + ncount: indicates that a message has arrived;

If fwaitall is set to true

Dret = wait_object_0, indicating that all the waiting core objects are activated or the threads are exited;

If fwaitall is set to false

Dret = wait_object_0 ~ Wait_object_0 + nCount-1: indicates that the waiting kernel object is fired, Index = dret-wait_object_0, indicating that the object indexed as index in the harray [] array is fired.

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.