The third multi-threaded programming - communication between threads

Seven inter-thread communication

  In general, the application of a secondary thread is always the main thread to perform a specific task, so that, between the main thread and secondary threads must have a channel for the transmission of information, that is, between the main thread and secondary threads to be communication. This inter-thread communication is not only inevitable, but also in multi-threaded programming is complex and frequent, will be explained below.

  1. Use of global variables to communicate

    due to the various threads share a process belonging to the same operating system to allocate resources to the process, inter-thread communication most simplest way to resolve it is to use global variables. For standard-type global variable, we recommend the use of volatile modifier, which tells the compiler that the variables do not need to make any optimizations that do not need to put it in a register, and the value can be changed externally. If the required information is transferred between threads more complex, we can define a structure, the information is passed by passing a pointer to the structure.
  2. Use custom message

    and we send custom thread to another thread performs a function to achieve the purpose of the message communication. A thread implemented by the operating system to send a message to another thread. The use of message-driven mechanism for the Windows operating system, when a thread issues a message that the operating system first receives the message, and then forwards the message to the target thread, the thread receives the message must have been established message loop.

Routine 7 MultiThread7

  This example demonstrates how to use a custom message for inter-thread communication. First, the main thread sends a message thread CCalculateThread WM_CALCULATE, CCalculateThread calculated after receiving the message thread, the main thread sends WM_DISPLAY message again, it shows the calculated main thread after receiving the message.

  1. Establish a project-based dialog MultiThread7, adding three radio buttons in the dialog box IDD_MULTITHREAD7_DIALOG in IDC_RADIO1, IDC_RADIO2, IDC_RADIO3, title were 1 + 2 + 3 + 4 + 2 + ...... + 10,1 + 3 + ...... + 50, 1 + 4 + 4 + 3 + 2 + ...... + 100. Join button IDC_SUM, titled "sum." Join label box IDC_STATUS, property, select the "Border";
  2. The variables are defined as follows MultiThread7Dlg.h:
    protected:
    	int nAddend;
    
    It represents the size of addend.

    Double-three radio buttons, respectively, add the message response function:
    void CMultiThread7Dlg::OnRadio1() 
    {
    	nAddend=10;
    }
    
    void CMultiThread7Dlg::OnRadio2() 
    {
    	nAddend=50;
    	
    }
    
    void CMultiThread7Dlg::OnRadio3() 
    {
    	nAddend=100;
    	
    }
    And complete the initialization function in the OnInitDialog:
    BOOL CMultiThread7Dlg::OnInitDialog()
    {
    ……
    	((CButton*)GetDlgItem(IDC_RADIO1))->SetCheck(TRUE);
    	nAddend=10;
    ……
    
    In MultiThread7Dlg.h added:
    #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);
    ……
    
    In MultiThread7Dlg.cpp added:
    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;
    
    }
    The above code so that the main thread class CMultiThread7Dlg WM_DISPLAY message can be processed, i.e., displaying the calculation result in IDC_STATUS label box.
  3. Double-click button IDC_SUM, add a message response function:
    void CMultiThread7Dlg::OnSum() 
    {
    	m_pCalculateThread=
    		(CCalculateThread*)AfxBeginThread(RUNTIME_CLASS(CCalculateThread));
    
    	Sleep(500);
    
    	m_pCalculateThread->PostThreadMessage(WM_CALCULATE,nAddend,NULL);
    }
    Action OnSum () function is to establish CalculateThread thread, the thread sending WM_CALCULATE to delay message.
  4. Right-click the project and select "New Class ..." Add the base class for the project is derived CWinThread thread class CCalculateThread.

    Add in the file CalculateThread.h
    #define WM_CALCULATE WM_USER+1 
    class CCalculateThread : public CWinThread
    {
    ……
    protected:
    	afx_msg LONG OnCalculate(UINT wParam,LONG lParam);
    ……
    
    Add in the file CalculateThread.cpp
    LONG CCalculateThread::OnCalculate(UINT wParam,LONG lParam)
    {
    	int nTmpt=0;
    	for(int i=0;i<=(int)wParam;i++)
    	{
    		nTmpt=nTmpt+i;
    	}
    
    	Sleep(500);
        ::PostMessage((HWND)(GetMainWnd()->GetSafeHwnd()),WM_DISPLAY,nTmpt,NULL);
    
    	return 0;
    }
    BEGIN_MESSAGE_MAP(CCalculateThread, CWinThread)
    	//{{AFX_MSG_MAP(CCalculateThread)
    		// NOTE - the ClassWizard will add and remove mapping macros here.
    	//}}AFX_MSG_MAP
    	ON_THREAD_MESSAGE(WM_CALCULATE,OnCalculate)
    //和主线程对比,注意它们的区别
    END_MESSAGE_MAP()
    
    Add a CalculateThread.cpp at the beginning of the file:
    #include "MultiThread7Dlg.h"
    
    Add CCalculateThread class code above the WM_CALCULATE message, the message is the OnCalculate response function, which is a function of the value of the parameter wParam, accumulated, the accumulation result in the temporary variable nTmpt, the delay 0.5 seconds, sending a message to the main thread WM_DISPLAY display , nTmpt passed as parameters.
Compile and run the routine, understand how to pass messages between threads.

(To be continued)

Reproduced in: https: //www.cnblogs.com/rogee/archive/2011/03/22/1990899.html

Guess you like

Origin blog.csdn.net/weixin_34055787/article/details/94680638