一、创建工作者线程
1、创建一个基于对话框的应用程序。
2、在对话框上面添加一个按钮控件,其ID为ID_START,标题为“开始工作者线程”,再添加一个进度条控件,并利用类向导添加变量:CprogressCtrl m_progress;
3、在C*Dlg.cpp中添加结构体如下:
struct threadInfo
{
int i;
CProgressCtrl * pctrlProgress;
}Info;
4、编写一个函数fun(),实现进度条的步进.
UINT fun(LPVOID p)
{
threadInfo * pInfo=(threadInfo *)p;
for(int i=0;i<100;i++)
{
int nTemp=pInfo->i;
pInfo->pctrlProgress->SetPos(i);
Sleep(nTemp);
}
// pThread=AfxBeginThread(RUNTIME_CLASS(CMyThread));
return 0;
}
5、添加函数创建工作者线程。
void CThreadDlg::OnStart()
{
// TODO: Add your control notification handler code here
Info.i=9;
Info.pctrlProgress=&m_progress;
pThread=AfxBeginThread(fun,&Info);//其他线程
}
二、创建用户界面线程
CWinThread *pThread=AfxBeginThread(RUNTIME_CLASS(CMyThread));
三、CEvent事件顺序
1、
#include "afxmt.h"
CEvent event;
UINT write1(LPVOID pParam);
UINT write2(LPVOID pParam);
2、
void CTestDlg::OnStart()
{
// TODO: Add your control notification handler code here
CWinThread *pwrite1=AfxBeginThread(write1,
&m_pro1,
THREAD_PRIORITY_NORMAL,
0,
CREATE_SUSPENDED);
pwrite1->ResumeThread();
CWinThread *pwrite2=AfxBeginThread(write2,
&m_pro2,
THREAD_PRIORITY_NORMAL,
0,
CREATE_SUSPENDED);
pwrite2->ResumeThread();
}
UINT write1(LPVOID pParam)
{
CProgressCtrl *p=(CProgressCtrl*)pParam;
for(int i=0;i<100;i++)
{
p->SetPos(i);
Sleep(100);
}
event.SetEvent();
return 0;
}
UINT write2(LPVOID pParam)
{
WaitForSingleObject(event.m_hObject,INFINITE);//注释后,同时出现
CProgressCtrl *p=(CProgressCtrl*)pParam;
for(int i=0;i<100;i++)
{
p->SetPos(i);
Sleep(100);
}
return 0;
}
四、线程的通信与同步
注意:包含头文件:#include"afxmt.h",将CSemaphore类添加进来。
1、创建对话框
2、添加4个进度条控件,一个按钮,并添加相应的变量。
3、在*Dlg.cpp中添加全局变量。
CCriticalSection section;//临界区对象
CSemaphore semaphore(2,2);//最多2,初始化2;
CEvent event;//事件对象
4、添加5个线程函数:
UINT write1(LPVOID pParam);
UINT write2(LPVOID pParam);
UINT write3(LPVOID pParam);
UINT write4(LPVOID pParam);
UINT write5(LPVOID pParam);
void CMyDialog::OnStart()
{
// TODO: Add your control notification handler code here
CWinThread * pWrite1=AfxBeginThread(write1,&m_pro1,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
pWrite1->ResumeThread();
CWinThread * pWrite2=AfxBeginThread(write2,&m_pro2,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
pWrite2->ResumeThread();
CWinThread * pWrite3=AfxBeginThread(write3,&m_pro3,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
pWrite3->ResumeThread();
CWinThread * pWrite4=AfxBeginThread(write4,&m_pro4,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
pWrite4->ResumeThread();
CWinThread * pWrite5=AfxBeginThread(write5,&m_pro1,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
pWrite5->ResumeThread();
}
UINT write1(LPVOID pParam)
{
WaitForSingleObject(semaphore.m_hObject,INFINITE);
CProgressCtrl *p=(CProgressCtrl *)pParam;
for(int i=0;i<100;i++)
{
p->SetPos(i);
Sleep(100);
}
ReleaseSemaphore(semaphore.m_hObject,1,NULL);
return 0;
}
UINT write2(LPVOID pParam)
{
WaitForSingleObject(semaphore.m_hObject,INFINITE);
CProgressCtrl *p=(CProgressCtrl *)pParam;
for(int i=0;i<100;i++)
{
p->SetPos(i);
Sleep(100);
}
ReleaseSemaphore(semaphore.m_hObject,1,NULL);
return 0;
}
UINT write3(LPVOID pParam)
{
// WaitForSingleObject(semaphoreWrite.m_hObject,INFINITE);
WaitForSingleObject(semaphore.m_hObject,INFINITE);
section.Lock();
CProgressCtrl *p=(CProgressCtrl *)pParam;
for(int i=0;i<100;i++)
{
p->SetPos(i);
Sleep(100);
}
section.Unlock();
ReleaseSemaphore(semaphore.m_hObject,1,NULL);
return 0;
}
UINT write4(LPVOID pParam)
{
WaitForSingleObject(semaphore.m_hObject,INFINITE);
section.Lock();
CProgressCtrl *p=(CProgressCtrl *)pParam;
for(int i=0;i<100;i++)
{
p->SetPos(i);
Sleep(100);
}
section.Unlock();
ReleaseSemaphore(semaphore.m_hObject,1,NULL);
event.SetEvent();
return 0;
}
UINT write5(LPVOID pParam)
{
WaitForSingleObject(event.m_hObject,INFINITE);
CProgressCtrl *p=(CProgressCtrl *)pParam;
for(int i=0;i<100;i++)
{
p->SetPos(i);
Sleep(100);
}
// ReleaseSmaphore(semaphore.m_hObject,1,NULL);
return 0;
}
5、创建工作者线程:
void CMyDialog::OnStart()
{
// TODO: Add your control notification handler code here
CWinThread * pWrite1=AfxBeginThread(write1,&m_pro1,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
pWrite1->ResumeThread();
CWinThread * pWrite2=AfxBeginThread(write2,&m_pro2,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
pWrite2->ResumeThread();
CWinThread * pWrite3=AfxBeginThread(write3,&m_pro3,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
pWrite3->ResumeThread();
CWinThread * pWrite4=AfxBeginThread(write4,&m_pro4,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
pWrite4->ResumeThread();
CWinThread * pWrite5=AfxBeginThread(write5,&m_pro1,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
pWrite5->ResumeThread();
}
五、线程的同步
注意:包含头文件:#include"afxmt.h",将CSemaphore类添加进来。
1、创建对话框
2、添加3个编辑框,一个按钮,并添加相应的变量。
3、在*Dlg.cpp中添加全局变量。
CSemaphore semaphoreWrite(3,3);//信号量对象 初始化3,最大个数3;
char g_Array[10];//字符数组
4、添加3个函数:
//添加3个函数:
UINT WriteA(LPVOID pParam)
{
CEdit * pEdit=(CEdit *)pParam;
pEdit->SetWindowText("");
WaitForSingleObject(semaphoreWrite.m_hObject,INFINITE);
CString str;
for(int i=0;i<10;i++)
{
pEdit->GetWindowText(str);
g_Array[i]='A';
str=str+g_Array[i];
pEdit->SetWindowText(str);
Sleep(1000);
}
ReleaseSemaphore(semaphoreWrite.m_hObject,1,NULL);//释放信号量
return 0;
}
UINT WriteB(LPVOID pParam)
{
CEdit * pEdit=(CEdit *)pParam;
pEdit->SetWindowText("");
WaitForSingleObject(semaphoreWrite.m_hObject,INFINITE);
CString str;
for(int i=0;i<10;i++)
{
pEdit->GetWindowText(str);
g_Array[i]='B';
str=str+g_Array[i];
pEdit->SetWindowText(str);
Sleep(1000);
}
ReleaseSemaphore(semaphoreWrite.m_hObject,1,NULL);//释放信号量
return 0;
}
UINT WriteC(LPVOID pParam)
{
CEdit * pEdit=(CEdit *)pParam;
pEdit->SetWindowText("");
WaitForSingleObject(semaphoreWrite.m_hObject,INFINITE);
CString str;
for(int i=0;i<10;i++)
{
pEdit->GetWindowText(str);
g_Array[i]='C';
str=str+g_Array[i];
pEdit->SetWindowText(str);
Sleep(1000);
}
ReleaseSemaphore(semaphoreWrite.m_hObject,1,NULL);//释放信号量
return 0;
}
5、创建同步线程进行输出:
CWinThread * pWriteA=AfxBeginThread(WriteA,&m_A,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
pWriteA->ResumeThread();
CWinThread * pWriteB=AfxBeginThread(WriteB,&m_B,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
pWriteB->ResumeThread();//结束线程
CWinThread * pWriteC=AfxBeginThread(WriteC,&m_C,THREAD_PRIORITY_NORMAL,0,CREATE_SUSPENDED);
pWriteC->ResumeThread();