Vii. Communication between threads
In general, a secondary thread in an application always performs a specific task for the main thread, so that there must be a channel of information passing between the main thread and the secondary threads, that is, communication between the main thread and the secondary threads. This communication between threads is not only unavoidable, but also complex and frequent in multithreaded programming, as described below.
Using global variables for communication
Because individual threads belonging to the same process share the resources that the operating system allocates for the process, one of the easiest ways to resolve the communication between threads is to use global variables. For a standard type of global variable, we recommend that you use the volatile modifier, which tells the compiler that you do not need to make any optimizations to the variable, that is, you do not have to place it in a register, and the value can be externally altered. If the information needed to be passed between threads is more complex, we can define a structure that delivers information by passing pointers to that structure.
Using custom messages
We can send a custom message to another thread in the execution function of one thread to achieve the purpose of communication. A thread sends a message to another thread that is implemented through the operating system. With the Windows operating system's message-driven mechanism, when a thread emits a message, the operating system first receives the message, and then forwards the message to the target thread, and the thread that receives the message must have established the message loop.
Routine 7 MultiThread7
This routine demonstrates how to use custom messages for communication between threads. First, the main thread sends a message to the Ccalculatethread thread wm_calculate,ccalculatethread The message is calculated, then sends a WM_DISPLAY message to the main thread, and the main thread receives the message and displays the results.
Create a dialog box based project MULTITHREAD7, add three radio button Idc_radio1,idc_radio2,idc_radio3 in the dialog idd_multithread7_dialog, the title is 1+2+3+4+ ... +10,1+2+3+4+......+50,1+2+3+4+......+100. Add button idc_sum, titled "Sum". Add the Label box Idc_status, the property check "border";
The following variables are defined in MultiThread7Dlg.h: protected:
int nAddend; representing the size of the addends.
Double-click three radio buttons to add a message response function:void CMultiThread7Dlg::OnRadio1()
{
nAddend=10;
}
void CMultiThread7Dlg::OnRadio2()
{
nAddend=50;
}
void CMultiThread7Dlg::OnRadio3()
{
nAddend=100;
}
and completes the corresponding initialization work in the OnInitDialog function:
BOOL CMultiThread7Dlg::OnInitDialog()
{
……
((CButton*)GetDlgItem(IDC_RADIO1))->SetCheck(TRUE);
nAddend=10;
……
Add in MultiThread7Dlg.h:#include "CalculateThread.h"
#define WM_DISPLAY WM_USER+2
class CMultiThread7Dlg : public CDialog
{
// Construction
public:
CMultiThread7Dlg(CWnd* pParent = NULL); // standard constructor
CCalculateThread* m_pCalculateThread;
……
protected:
int nAddend;
LRESULT OnDisplay(WPARAM wParam,LPARAM lParam);
……
Add in MultiThread7Dlg.cpp:BEGIN_MESSAGE_MAP(CMultiThread7Dlg, CDialog)
……
ON_MESSAGE(WM_DISPLAY,OnDisplay)
END_MESSAGE_MAP()
LRESULT CMultiThread7Dlg::OnDisplay(WPARAM wParam,LPARAM lParam)
{
int nTemp=(int)wParam;
SetDlgItemInt(IDC_STATUS,nTemp,FALSE);
return 0;
}