Multi-thread communication C ++ using message Loops

Source: Internet
Author: User

Program Implementation requirements:

My program has a main thread and N working threads. The main thread collects data and assigns it to different worker threads for processing. In the program, I use PostThreadMessage () to notify the corresponding thread to process data, while the worker thread uses GetMessage () to process the data.

Pseudocode:

The code process is as follows:
CreateThread (main); // main thread;
For (int I = 0; I <10; I ++) // assume 10 worker threads.
{
CreateThread (work); // The ThreadID of the working thread is recorded at the same time;
}
ThreadMain ()
{
If (the condition meets the requirements of a thread)
{
While (! PostThreadMessage (WorkID, MSG, parm, 0) sleep (0); // send a message notification to the corresponding worker thread
}
}
ThreadWork ()
{
While (true)
{
IRet = GetMessage (msg, NULL, 0, 0 );
{
The iret judgment is omitted here.
If (msg. Message = my_msg)
{
...
}
}
}
}

Source Code Compiled:

//---------------------------------------------------------------------------
# Include <vcl. h>
# Pragma hdrstop
# Include <map>
# Include <iostream>
Using namespace std;
Typedef map <int, DWORD> value_map;
Typedef value_map: value_type valType;
# Define WM_WORK WM_APP + 0x100
# Define wm_finish wm_app + 0x101
Value_map * vthreadsidles;
//---------------------------------------------------------------------------
# Pragma argsused
DWORD winapi threadmain (lpvoid P );
DWORD winapi threadwork (lpvoid P );
Handle hmutex;
Int main (INT argc, char * argv [])
{
Value_map mthreads;
HANDLE handle;
DWORD dw;
HMutex = CreateMutex (NULL, false, "Sample ");
For (int iThreadIndex = 0; iThreadIndex <10; ++ iThreadIndex)
{
Handle = createthread (null, 0, threadwork, null, 0, & DW );
Mthreads. insert (valtype (ithreadindex, DW ));
}
// Main thread;
Handle = CreateThread (NULL, 0, ThreadMain, (LPVOID) & mThreads, 0, & dw );
WaitForSingleObject (handle, INFINITE );
CloseHandle (handle );
CloseHandle (hMutex );
Return 0;
}
//---------------------------------------------------------------------------
Dword winapi ThreadMain (LPVOID p)
{
TDateTime dtNow;
VThreadsIdles = (value_map *) p;
Int iSecond;
MSG msg;
Int iRet;
Bool blPostTM;
Value_map: iterator iter;
Cout <"Start" <endl;
DWORD dThread;
Int iID;
While (true)
{
DtNow = Date (). CurrentDateTime ();
// Execute when the current time is less than 30 seconds
If (dtNow. FormatString ("ss"). ToInt () <30)
{
ISecond = 1000;
// Obtain the available processing thread
Try
{
WaitForSingleObject (hMutex, INFINITE );
If (0 = vThreadsIdles-> size ())
{
Continue;
}
// In map, it is a forward, forward, and backward
Iter = vThreadsIdles-> end ();
-- Iter;
DThread = (* iter). second;
IID = (* iter). first;
VThreadsIdles-> erase (iID );
}
_ Finally
{
ReleaseMutex (hMutex );
}
// Deliver the message
While (! (BlPostTM = PostThreadMessage (dThread, WM_WORK, iID, 0 )))
{
// Delivery timeout Condition
If (iSecond = 3000)
{
Break;
}
Sleep (iSecond );
ISecond + = 1000;
}
If (! BlPostTM)
{
// The delivery failure thread returns to the idle pool
Waitforsingleobject (hmutex, infinite );
VThreadsIdles-> insert (valType (iID, dThread ));
ReleaseMutex (hMutex );
Cout <ansistring (dthread). c_str () <"Message Delivery failed" <Endl;
Continue;
}
Cout <AnsiString (dThread). c_str () <"message delivered successfully" <endl;
}
}
}
DWORD winapi threadwork (lpvoid P)
{
MSG;
Int iret;
Int iSecond;
Bool blPostTM;
While (true)
{
IRet = GetMessage (& msg, NULL, 0, 0 );
If (WM_WORK = msg. message)
{
Cout <AnsiString (GetCurrentThreadId (). c_str () <"processing..." <endl;
Sleep (5000 );
// Return the processing completed thread to the idle pool
WaitForSingleObject (hMutex, INFINITE );
Vthreadsidles-> insert (valtype (msg. wparam, getcurrentthreadid ()));
Releasemutex (hmutex );
Cout <AnsiString (GetCurrentThreadId (). c_str () <"idle" <endl;
}
}
}

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.