在window系统中编写控制台程序,创建线程
使用CreateThread()函数创建,则线程函数必须申明为DWORD WINAPI;
使用_beginthreadex()创建,则线程函数必须申明为unsigned int WINAPI;
并需要设置环境:工程->设置->C/C++->Code Generation->Use run-time libray->选 Debug Multithread(多线程),或 Multithread.
例如:
#include <iostream.h>
#include <windows.h>
#include <process.h>
DWROD WINAPI myfun1(void *pvoid)
{
cout<<"CreateThread"<<endl;
return 0;
}
unsigned int WINAPI myfun2(void *pvoid)
{
cout<<"_beginthreadex<endl;
return 0;
}
int main()
{
CreateThread(NULL,NULL, myfun1,NULL,NULL);
_beginthreadex(NULL,NULL,myfun2,NULL,NULL);
return 0;
}
将类成员函数作为线程函数方式:
1.将类成员申明为STATIC成员函数;
2.将函数申明为类的友元函数;
例如:
class MyTest
{
public:
static unsigned int WINAPI mythread(void *pvoid);
friend unsigned int WINAPI myFun(void *pvoid);
};