主线程与子线程之间通讯, 子线程与子线程之间的通讯

 线程之间的通讯可以有两种方法.

一.利用全局变量. 大家都可以访问,所以使用很方便.

 

二.利用PostThreadMesagge(ThreadId,Msg,0,0)来进行通讯.

其中要注意的问题是,因为利用CreateThread创建的线程是无消息获取通道,消息映射这些功能,只有有窗体的线程才具备这些功能,所以要自己获取消息队列,利用PeekMessage既可.

 

以下是我做的例子.

 

void CCTestDlg::OnBnClickedButtoncreatethread()
{
 // TODO: Add your control notification handler code here
 int by = sizeof(BYTE);
 int a = sizeof(char);
 int b = sizeof(short);
 int c = sizeof(WORD);
 int d = sizeof(int);
 int e = sizeof(DWORD);
 int f = sizeof(long);
 int m =100;
 int*p = &m;
 
 int n=10;
 const int* const pp = &n;

 ABB ca(1,2,3);
 ca.GetNum();


 ABB dd(4,5,6);
 dd.GetNum();


 m_hWait1 = ::CreateEvent(0,TRUE,FALSE,0);
 m_hWait2 = ::CreateEvent(0,TRUE,FALSE,0);
 m_hWait3 = ::CreateEvent(0,TRUE,FALSE,0);
 m_hWait4 = ::CreateEvent(0,TRUE,FALSE,0);

 DWORD dwID;
 HANDLE hHandle = ::CreateThread(NULL,0,ThreadProc,this,0,&dwID);


 DWORD dwThread1,dwThread2;
 HANDLE hThread1,hThread2;
 m_pThread1 = new CThread1;
 m_pThread2 = new CThread2;
 hThread1 = ::CreateThread(NULL,0,CThread1::ThreadPoc,m_pThread1,0,&dwThread1);
 hThread2 = ::CreateThread(NULL,0,CThread2::ThreadPoc,m_pThread2,0,&dwThread2);

 if(hThread1)
 {
  
  m_Arry.Add(dwThread1);
  m_pThread1->m_pMainWnd = this;
 }

 if(hThread2)
 {
  m_Arry.Add(dwThread2);
  m_pThread2->m_pMainWnd = this;
 }

}

 

CThread1::CThread1()
{
 m_hStop = ::CreateEvent(0,true,false,0);
}

CThread1::~CThread1()
{
}

DWORD WINAPI CThread1::ThreadPoc(LPVOID lparam)
{
 CThread1 *pThread1 = (CThread1*)lparam;
 return pThread1->MainFunc();
}

DWORD CThread1::MainFunc()
{
 while(::WaitForSingleObject(m_hStop, 10) != WAIT_OBJECT_0)//main loop
 {
  MSG   msg;  
  if(PeekMessage(&msg , NULL, 0, 0,  PM_REMOVE))
  {
   switch(msg.message)  
   {  
   case   WM_THREAD_EXIT_MSG:  
    //   
    AfxMessageBox("Thread1 receive Thread2 exit");
    break; 
   }
  }  
 }
 DWORD dwid = m_pMainWnd->m_Arry.GetAt(1);
 PostThreadMessage(dwid,WM_THREAD_EXIT_MSG,0,0);
 return 0;
}

void CThread1::StopServ()
{
 SetEvent(m_hStop);
}

void CThread1::ThreadMsgFunction(WPARAM wParam,LPARAM lParam)
{

}

CThread2::CThread2()
{
 m_hStop = ::CreateEvent(0,true,false,0);
}

CThread2::~CThread2()
{
}

DWORD WINAPI CThread2::ThreadPoc(LPVOID lparam)
{
 CThread2 *pThread2 = (CThread2*)lparam;
 return pThread2->MainFunc();
}

DWORD CThread2::MainFunc()
{
 //while(::WaitForSingleObject(m_hStop,10)!=WAIT_OBJECT_0)
 while(::WaitForSingleObject(m_hStop, 10) != WAIT_OBJECT_0)//main loop
 {
  MSG   msg;  
  if(PeekMessage(&msg , NULL, 0, 0,  PM_REMOVE))
  {
   switch(msg.message)  
   {  
    case   WM_THREAD_EXIT_MSG:  
    // 
    AfxMessageBox("Thread2 receive Thread1 exit");
    break; 
   }
  }  
 }
 DWORD dwid = m_pMainWnd->m_Arry.GetAt(0);
 PostThreadMessage(dwid,WM_THREAD_EXIT_MSG,0,0);
 return 0;
}

void CThread2::StopServ()
{
 SetEvent(m_hStop);
}

void CThread2::ThreadMsgFunction(WPARAM wParam,LPARAM lParam)
{

}
void CCTestDlg::OnBnClickedButtonExitThread1()
{
 // TODO: Add your control notification handler code here
 m_pThread1->StopServ();
}

void CCTestDlg::OnBnClickedButtonExithread2()
{
 // TODO: Add your control notification handler code here
 m_pThread2->StopServ();
}

你可能感兴趣的:(thread,object,null,byte,通讯,winapi)