HANDLE CreateThread( LPSECURITY_ATTRIBUTES lpThreadAttributes, // pointer to security attributes DWORD dwStackSize, // initial thread stack size LPTHREAD_START_ROUTINE lpStartAddress, // pointer to thread function LPVOID lpParameter, // argument for new thread DWORD dwCreationFlags, // creation flags LPDWORD lpThreadId // pointer to receive thread ID);
在类的文件中定义线程函数:
static DWORD WINAPI TestThreadProc(LPVOID lpParameter);
DWORD WINAPI TestThreadProc();
源文件中实现:
DWORD WINAPI CTestDlg::TestThreadProc( LPVOID lpParameter )
{
CTestDlg* pDataCollecDlg= (CTestDlg*)lpParameter;
return pDataCollecDlg->TestThreadProc();
}
DWORD WINAPI TestThreadProc();//执行线程工作内容
创建线程:
HANDLE hThread= CreateThread(NULL,0,TestThreadProc,this,0,NULL);
CWinThread* pThread =AfxBeginThread(ThreadTests,this,THREAD_PRIORITY_HIGHEST,0,0,NULL);//MFC函数创建,不需要关闭句柄(控制优先级等操作通过pThread操作)
NULL:安全属性继承进程
0:堆栈大小默认1M
线程函数:TestThreadProc
this:传递参数
0:创建即执行
NULL:thread ID
暂停线程:
SuspendThread(hThread);
激活/恢复线程:
ResumeThread(hThread);
设置线程优先级:
SetThreadPriority(hThread,THREAD_PRIORITY_HIGHEST);
用完时,关闭句柄:
CloseHandle(hThread);
也可以使用C运行时函数来创建、结束线程:
_beginthread,_beginthreadex
_endthread,_endthreadex
爱华网本文地址 » http://www.413yy.cn/a/25101014/211709.html
【问题】创建了10个线程,10个线程公用一个线程体,创建如下:intt1=0,t2=1,t3=2,t4=3,t5=4,t6=5,t7=6,t8=7,t9=8,t10=9; int*one=&t1,*two=&t2,*three=&t3,*four=&t4,*five=&t5,*six=&t6,*seven=&t7,*eight=