最简单的创建线程 -- VS2008

头文件相关   

    _beginthread    process.h

    HANDLE          windows.h

线程创建相关

(1)用_beginthreadex创建线程:

    unsigned long _beginthreadex(void * security,unsigned stack_size,unsigned(__stdcall *start_address)(void*),void* arglist,unsigned initflag,unsigned * thrdaddr);

参数:1、安全属性,NULL表示默认安全属性

2、指定线程堆栈的大小。一般用0,0表示线程堆栈的大小和创建它的线程相同。

3、指定线程函数的地址,用函数名。线程函数一般不能为类的普通成员函数,可以是静态成员函数。

4、传递给线程的指针。用于线程函数

5、线程的初始状态。0表示立刻执行,CREATE_SUSPEND表示悬挂。

6、线程ID的地址。

(2)因为_beginthreadex和_endthreadex是CRT线程函数,所以必须注意编译选项runtimelibaray的选择,使用MT或MTD。[MultiThreaded , Debug MultiThreaded]。

(3)_beginthreadex是一个C运行时库的函数,CreateThread是一个系统API函 数,_beginthreadex内部调用了CreateThread。因为_beginthreadex在内部调用了CreateThread,在调用之前_beginthreadex做了很多的工作,从而使得它比CreateThread更安全。

线程返回相关:       

a)线程函数返回;(最佳选择)       

b)线程通过调用ExitThread函数“杀死”自己(尽量不用);杀死主线程         

调用_endthreadex销毁线程,不要用ExitThread           

VOID ExitThread(DWORD dwExitCode);        //参数表明将线程的退出代码设为什么(c/c++不要用,数据块不会被销毁,会导致内存泄露)

HANDLE和线程相关   

CloseHandel(ThreadHandle );只是关闭了一个线程句柄对象,表示不再使用该句柄,即不对这个句柄对应的线程做任何干预了。并没有结束线程。在确定之后不会再使用这个句柄的时候就可以close。   

句柄和线程的生命周期不同。



最简单的代码:


#include "stdafx.h"

#include

#include

#include

#include

using namespace std;

class ThreadX{

public:

ThreadX(int start,int end,int dis)

{

loopStart = start;

loopEnd = end;

dispFrequency = dis;

}

static unsigned __stdcall ThreadStaticEntryPoint(void* pThis)

{

ThreadX* pthx = (ThreadX*)pThis;

pthx->ThreadEntryPoint();

return 1;

}

void ThreadEntryPoint()

{

for(int i=loopStart;i<=loopEnd;++i)

{

if (i%dispFrequency == 0)

{

printf("%s:i = %d.\n",threadName.c_str(),i);

}

}

printf("%s thread terminating.\n",threadName.c_str());

}

string threadName;

private:

int loopStart;

int loopEnd;

int dispFrequency;

};

unsigned __stdcall ThreadFunction3(void* pThis)

{

int j = (int)pThis;

printf("This is thread_3.\n");

return 1;

}

int _tmain(int argc, _TCHAR* argv[])

{

HANDLE hMutex;

ThreadX* o1 = new ThreadX(0,1,2000);

HANDLE hth1;

unsigned uiThreadID1;

hth1 = (HANDLE)_beginthreadex(NULL,0,ThreadX::ThreadStaticEntryPoint,o1,CREATE_SUSPENDED,&uiThreadID1);

if (hth1 == NULL)

{

printf("Failed to create thread.\n");

}

DWORD dwExitCode;

GetExitCodeThread(hth1,&dwExitCode);

printf("initial thread_1 exit code = %u.\n",dwExitCode);

o1->threadName = "thread_2";

ThreadX* o2 = new ThreadX(0,1,2000);

HANDLE hth2;

unsigned uiThreadID2;

hth2 = (HANDLE)_beginthreadex(NULL,0,ThreadX::ThreadStaticEntryPoint,o2,CREATE_SUSPENDED,&uiThreadID2);

if (hth1 == NULL)

{

printf("Failed to create thread_2.\n");

}

GetExitCodeThread(hth2,&dwExitCode);

printf("initial thread_2 exit code = %u.\n",dwExitCode);

o2->threadName = "thread_2";

ResumeThread(hth1);

ResumeThread(hth2);

WaitForSingleObject(hth1,INFINITE);

WaitForSingleObject(hth2,INFINITE);

GetExitCodeThread(hth1,&dwExitCode);

printf("initial thread_1 exit code = %u.\n",dwExitCode);

GetExitCodeThread(hth2,&dwExitCode);

printf("initial thread_2 exit code = %u.\n",dwExitCode);

CloseHandle(hth1);

CloseHandle(hth2);

delete o1;

o1 = NULL;

delete o2;

o2 = NULL;

int j = 2;

HANDLE hth3;

unsigned uiThreadID3;

hth3 = (HANDLE)_beginthreadex(NULL,0,ThreadFunction3,(void*)j,CREATE_SUSPENDED,&uiThreadID3);

if (hth3 == NULL)

{

printf("Failed to create thread.\n");

}

GetExitCodeThread(hth2,&dwExitCode);

ResumeThread(hth3);

WaitForSingleObject(hth3,INFINITE);

CloseHandle(hth3);

printf("Primary thread terminating.\n");

return 0;

}


你可能感兴趣的:(最简单的创建线程 -- VS2008)