1.CWinApp是一个i额用户界面线程对象,派生自CWinThread,处理用户产生的事件和消息。
MFC
2.AfxBeginThread函数创建和初始化CWinThread对象,启动并返回地址。
3.
pThread = new CWinThread();
pThread->m_bAutoDelete = FALSE;
pThread = AfxBeginThread(StartAndCloseThreadProc, NULL);
4.
挂起线程
pThread->SuspendThread();
重新启动线程
pThread->ResumeThread();
5.
线程的同步
11.事件对象
#include "stdafx.h"
#include
#include
#include
#include "process.h"
DWORD WINAPI ThreadProc1(LPVOID lpParam);
DWORD WINAPI ThreadProc2(LPVOID lpParam);
int iGolbalCount=0;
int iMax = 12;
HANDLE hEvent;
int main(int argc, char* argv[])
{
HANDLE hThread1, hThread2;
hEvent=CreateEvent(NULL,FALSE,FALSE,LPCTSTR("iGolbalCount"));
if (hEvent == NULL)
{
printf("创建事件对象失败!\r\n");
return 0;
}
SetEvent(hEvent);
hThread1=CreateThread(NULL,0,ThreadProc1,NULL,0,NULL);
hThread2=CreateThread(NULL,0,ThreadProc2,NULL,0,NULL);
Sleep(40000);
CloseHandle(hEvent); //关闭事件对象句柄
printf("主线程结束!\n");
system("pause");
return 0;
}
DWORD WINAPI ThreadProc1(LPVOID lpParam)
{
while (TRUE)
{
WaitForSingleObject(hEvent,INFINITE);
if (iGolbalCount < iMax)
{
printf("这里是线程1,iGolbalCount=%d\r\n", iGolbalCount++);
SetEvent(hEvent);
}
else
{
SetEvent(hEvent);
break;
}
Sleep(10);
}
return 0;
}
DWORD WINAPI ThreadProc2(LPVOID lpParameter)
{
while (TRUE)
{
WaitForSingleObject(hEvent,INFINITE);
if (iGolbalCount < iMax)
{
printf("这里是线程2,iGolbalCount=%d\r\n", iGolbalCount++);
SetEvent(hEvent);
}
else
{
SetEvent(hEvent);
break;
}
Sleep(10);
}
return 0;
}
22.利用临界区
DWORD WINAPI ThreadProc1(LPVOID lpParam);
DWORD WINAPI ThreadProc2(LPVOID lpParam);
int iGolbalCount=0;
int iMax = 12;
CRITICAL_SECTION cs;
int main(int argc, char* argv[])
{
HANDLE hThread1, hThread2;
InitializeCriticalSection(&cs);
hThread1=CreateThread(NULL,0,ThreadProc1,NULL,0,NULL);
hThread2=CreateThread(NULL,0,ThreadProc2,NULL,0,NULL);
Sleep(50000);
DeleteCriticalSection(&cs); //删除临界区对象
printf("主线程结束!\n");
return 0;
}
DWORD WINAPI ThreadProc1(LPVOID lpParam)
{
while (TRUE)
{
EnterCriticalSection(&cs);
if (iGolbalCount < iMax)
{
printf("这里是线程1,iGolbalCount=%d\r\n", iGolbalCount++);
LeaveCriticalSection(&cs);
}
else
{
LeaveCriticalSection(&cs);
break;
}
Sleep(10);
}
return 0;
}
33.利用信号量
DWORD WINAPI ThreadProc1(LPVOID lpParam);
DWORD WINAPI ThreadProc2(LPVOID lpParam);
int iGolbalCount=0;
int iMax = 12;
int cMax = 1;
HANDLE hSemaphore;
int main(int argc, char* argv[])
{
HANDLE hThread1, hThread2;
hSemaphore = CreateSemaphore( NULL, cMax, cMax, NULL);
if (hSemaphore == NULL)
{
printf("创建信号量对象失败");
return 0;
}
hThread1=CreateThread(NULL,0,ThreadProc1,NULL,0,NULL);
hThread2=CreateThread(NULL,0,ThreadProc2,NULL,0,NULL);
Sleep(5000);
printf("主线程结束!");
system("pause");
return 0;
}
DWORD WINAPI ThreadProc1(LPVOID lpParam)
{
while (TRUE)
{
DWORD dwWaitResult = WaitForSingleObject(hSemaphore, 0L);
if (dwWaitResult == WAIT_OBJECT_0)
{
if (iGolbalCount < iMax)
{
printf("这里是线程1,iGolbalCount=%d\r\n", iGolbalCount++);
ReleaseSemaphore(hSemaphore, 1, NULL);
}
else
{
ReleaseSemaphore(hSemaphore, 1, NULL);
break;
}
}
Sleep(10);
}
return 0;
}
44.利用互斥对象
DWORD WINAPI ThreadProc1(LPVOID lpParam);
DWORD WINAPI ThreadProc2(LPVOID lpParam);
int iGolbalCount=0;
int iMax = 12;
HANDLE hMutex;
int main(int argc, char* argv[])
{
HANDLE hThread1, hThread2;
hThread1=CreateThread(NULL,0,ThreadProc1,NULL,0,NULL);
hThread2=CreateThread(NULL,0,ThreadProc2,NULL,0,NULL);
hMutex=CreateMutex(NULL,TRUE,LPCTSTR("iGolbalCount"));
if (hMutex == NULL)
{
printf("创建互斥对象失败!\r\n");
return 0;
}
WaitForSingleObject(hMutex,INFINITE);
ReleaseMutex(hMutex);
ReleaseMutex(hMutex);
Sleep(5000);
printf("主线程结束!\n");
return 0;
}
DWORD WINAPI ThreadProc1(LPVOID lpParam)
{
while (TRUE)
{
WaitForSingleObject(hMutex,INFINITE);
if (iGolbalCount < iMax)
printf("这里是线程1,iGolbalCount=%d\r\n", iGolbalCount++);
else
break;
ReleaseMutex(hMutex);
Sleep(10);
}
return 0;
}