一。创建线程的三种方式
1.CreateThread (windows中vc++)
CreateThread(
_In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes, //线程属性
_In_ SIZE_T dwStackSize, //栈空间大小
_In_ LPTHREAD_START_ROUTINE lpStartAddress, //线程执行函数地址
_In_opt_ __drv_aliasesMem LPVOID lpParameter, //传递参数
_In_ DWORD dwCreationFlags, //标志,可以选择挂起
_Out_opt_ LPDWORD lpThreadId //线程id
);
1 DWORD WINAPI ThreadProc(LPVOID lpParameter)
2 {
3 int a = (int)lpParameter;
4 CString str;
5 str.Format(_T("%d"),a);
6 AfxMessageBox(str);
7 return 0;
8 }
9 void CSegDlg::OnBnClickedstop()
10 {
11 // TODO: 在此添加控件通知处理程序代码
12 DWORD dwThreadId = 0; //线程id
13 HANDLE hThread=CreateThread(NULL, 0, ThreadProc, (LPVOID)123, 0, &dwThreadId); //返回线程句柄
14 //关闭句柄,线程结束后释放
15 CloseHandle(hThread);
16 }
2.AfxBeginThread(MFC)
CWinThread* AFXAPI AfxBeginThread(AFX_THREADPROC pfnThreadProc, LPVOID pParam, int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0, DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL); CWinThread* AFXAPI AfxBeginThread(CRuntimeClass* pThreadClass, int nPriority = THREAD_PRIORITY_NORMAL, UINT nStackSize = 0, DWORD dwCreateFlags = 0, LPSECURITY_ATTRIBUTES lpSecurityAttrs = NULL);
1 UINT _cdecl ThreadProc(LPVOID lpParameter)
2 {
3 int a = (int)lpParameter;
4 CString str;
5 str.Format(_T("%d"), a);
6 AfxMessageBox(str);
7 return 0;
8 }
9 void CSegDlg::OnBnClickedstop()
10 {
11 // TODO: 在此添加控件通知处理程序代码
12 CWinThread *pThread=AfxBeginThread(ThreadProc, (LPVOID)456);
13
14 }
3._beginthread
二。子线程使用主线程参数
1.子线程对应于一个普通函数,不能被简单的声明成主对话框类的成员函数,因此不能使用主对话框(主线程)的参数,需要在创建线程时,将主对话框类对象的指针传递给子线程。子线程通过指针去访问主线程的变量。
1 UINT _cdecl ThreadProc(LPVOID lpParameter)
2 {
3 CSegDlg* pSegDlg = (CSegDlg *)lpParameter; //转换为主对话框类的指针
4 int a = pSegDlg->num;
5 CString str;
6 str.Format(_T("%d"), a);
7 AfxMessageBox(str);
8 return 0;
9 }
10 void CSegDlg::OnBnClickedstop()
11 {
12 num = 100; //成员变量
13 CWinThread *pThread=AfxBeginThread(ThreadProc, this);
14 }
2.如果一定要将子线程函数声明为类成员函数,需要定义为静态成员函数
1 UINT _cdecl CSegDlg::ThreadProc(LPVOID lpParameter) //static函数
2 {
3 CSegDlg* pSegDlg = (CSegDlg *)lpParameter; //仍然需要传递对象指针,因为static关键词没有this指针
4 int a = pSegDlg->num;
5 CString str;
6 str.Format(_T("%d"), a);
7 AfxMessageBox(str);
8 return 0;
9 }
10 void CSegDlg::OnBnClickedstop()
11 {
12 num = 100;
13 CWinThread *pThread=AfxBeginThread(ThreadProc, this);
14 }