【笔记】线程---事件之共享读取
当一个主线程跑完,其它子线程都可读取其内容。[
DWORD WINAPI ThreadMain(LPVOID lpParameter )
{ //创建事件,因为第2个参数是true,所以当用SetEvent(g_hEvent)设置改变了事件第3个参数为已通知的话,没有人再去
//改变事件的状态为未通知,所以子进程都能wait到
g_hEvent = CreateEvent(NULL,TRUE,FALSE,NULL); //创建事件后是未通知状态
HANDLE hThread;
hThread = ::CreateThread(NULL,0,ThreadProc2,NULL,0,NULL);
hThread = ::CreateThread(NULL,0,ThreadProc3,NULL,0,NULL);
hThread = ::CreateThread(NULL,0,ThreadProc4,NULL,0,NULL);
SetWindowText( hEdit1,"1000");
SetEvent(g_hEvent);
WaitForMultipleObjects(3,hThread,TRUE,-1);
CloseHandle(hThread);
CloseHandle(hThread);
CloseHandle(hThread);
CloseHandle(g_hEvent);
return 0;
}
BOOL CALLBACK MainDlgProc(HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM lParam) //回调函数是主线程
{
BOOL bRet = FALSE;
switch(uMsg)
{
case WM_CLOSE:
{
EndDialog(hDlg,0);
break;
}
case WM_INITDIALOG: //窗口初始化
{
hEdit1=GetDlgItem(hDlg,IDC_EDIT1 );
SetWindowText( hEdit1,"0");
hEdit2=GetDlgItem(hDlg,IDC_EDIT2 );
SetWindowText( hEdit2,"0");
hEdit3=GetDlgItem(hDlg,IDC_EDIT3 );
SetWindowText( hEdit3,"0");
hEdit4=GetDlgItem(hDlg,IDC_EDIT4);
SetWindowText( hEdit4,"0");
break;
}
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_BUTTON1:
{
hThread1 = ::CreateThread(NULL,0,ThreadMain,NULL,0,NULL);
::CloseHandle(hThread1);
return TRUE;
}
}
break;
}
return bRet;
}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
DialogBox(hInstance,MAKEINTRESOURCE(IDD_DIALOG1),NULL,MainDlgProc);
return 0;
}
]
页:
[1]