One-day training of Windows APIs (48) postthreadmessage Function

Source: Internet
Author: User
One-day training of Windows APIs (48) postthreadmessage Function

In write server programs, the thread pool is used in many places. In particular, the CPU of multi-processor is becoming increasingly popular. Using multiple thread pools significantly improves the performance of server programs. In a message-based Windows system, message-based processing is the simplest thread pool method, which is easy to use and easy to understand. After creating multiple threads, you can send messages to different threads Based on the thread ID. Each thread processes its own messages. The postthreadmessage function is used to send messages to the thread. The following shows how to use this function.

The postthreadmessage function is declared as follows:

Winuserapi
Bool
Winapi
Postthreadmessagea (
_ In DWORD idthread,
_ In uint MSG,
_ In wparam,
_ In lparam );
Winuserapi
Bool
Winapi
Postthreadmessagew (
_ In DWORD idthread,
_ In uint MSG,
_ In wparam,
_ In lparam );
# Ifdef Unicode
# Define postthreadmessage postthreadmessagew
# Else
# Define postthreadmessage postthreadmessagea
# Endif //! Unicode

Idthread is the thread ID.
MSG is the ID of the sent message.
Wparam is a message parameter.
Lparam is a message parameter.

An example of calling a function is as follows:
#001 // run the function in a thread.
#002 // here, you can use the members in the class or make the derived class more powerful.
#003 // Cai junsheng 2007/10/11 QQ: 9073204 Shenzhen
#004 DWORD cthreadmsg: Run (void)
#005 {
#006 // create a thread message queue.
#007 MSG;
#008 peekmessage (& MSG, null, wm_user, wm_user + 1000, pm_noremove );
#009
#010 //
#011 (;;)
#012 {
#013 // find whether there is thread message processing.
#014 bool Bres = peekmessage (& MSG, null, wm_user, wm_user + 1000, pm_remove );
#015 if (Bres)
#016 {
#017 // process the message of this thread here.
#018: outputdebugstring (_ T ("cthreadmsg: Run () has message processing/R/N "));
#019}
#020 else
#021 {
#022 // wait for the thread to exit.
#023 DWORD dwret = waitforsingleobject (m_heventexit, 0 );
#024 if (dwret = wait_timeout)
#025 {
#026 // if nothing is done at present, let the thread release the CPU.
#027 sleep (10 );
#028}
#029 else
#030 {
#031 // exit the thread.
#032: outputdebugstring (_ T ("cthreadmsg: Run () Exit thread/R/N "));
#033 break;
#034}
#035}
#036}
#037
#038 //
#039 return 0;
#040}

The above implements thread message processing.

#001 class cthreadmsg:
#002 public cthread
#003 {
#004 public:
#005 cthreadmsg (void );
#006 virtual ~ Cthreadmsg (void );
#007
#008 // send a message to the thread for processing.
#009 // Cai junsheng 2007/10/11 QQ: 9073204 Shenzhen
#010 void postmessage (void)
#011 {
#012 //
#013: postthreadmessage (m_dwthreadid, wm_user + 100,0, 0 );
#014}
#015 protected:
#016 virtual DWORD run (void );
#017 };

The preceding Code calls the postthreadmessage function to send messages to the thread for processing.

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.