①, the most common way: global variables or multiple threads can see an East 1, global variable mode; int g_num = 100; UINT __cdecl Threadwriteproc (lpvoid lpparameter) {while (TRUE) {++g_num;Sleep (50);}return 100;}
UINT __cdecl Threadreadproc (lpvoid lpparameter) {CString strtipmsg;while (TRUE) {Strtipmsg.format (_t ("%d"), g_num);OutputDebugString (STRTIPMSG);Sleep (50);}
return 100;}
void Cthreadtestdlg::onbnclickedbtn () {CWinThread *pthread = AfxBeginThread (Threadwriteproc, NULL);AfxBeginThread (Threadreadproc, NULL);}
2, everyone can access to an east; 3, the global variable declaration way; ===================================================②, send message: postthreadmessage
#define MY_THREAD_MSG (wm_user+100) UINT __cdecl threadwriteproc (lpvoid lpparameter) {int ncount = 0;DWORD Dwthreadreadid = (DWORD) lpparameter;while (TRUE) {PostThreadMessage (Dwthreadreadid, My_thread_msg, ncount++, NULL);Sleep (50);}return 0;}
UINT __cdecl Threadprintproc (lpvoid lpparameter) {msg msg = {0};while (GetMessage (&msg, 0, 0, 0)){Switch (msg.message) {Case MY_THREAD_MSG:{int ncount = (int) Msg.wparam;CString StrText;Strtext.format (_t ("%d"), ncount);OutputDebugString (StrText);}Break}}
return 0;}
void Cthreadtestdlg::onbnclickedbtn () {CWinThread *pthreadprint = AfxBeginThread (Threadprintproc, NULL);CWinThread *pthreadwrite = AfxBeginThread (Threadwriteproc,
(LPVOID) pthreadprint->m_nthreadid);//
Cwinthread::m_nthreadid |
The ID of the current thread. |
}===================================================
From for notes (Wiz)
Inter-thread Communication 1 global Variable 2 current main dialog pointer 3 message way 4 AfxBeginThread return value (pointer to CWinThread type) 5AfxGetApp