您当前的位置: 主页网站优化软件知识

深入浅出Win32多线程程序设计之线程控制

发布于:2014-03-17 18:40:45  作者:兄弟网络   点击:

深入浅出Win32多线程程序设计之线程控制

2007-12-14 15:18

/**********************************************/

转:

/**********************************************/

WIN32线程控制主要实现线程的创建、终止、挂起和恢复等操作,这些操作都依赖于WIN32提供的一组API和具体编译器的C运行时库函数。

一、线程函数

在启动一个线程之前,软件开发,必须为线程编写一个全局的线程函数,这个线程函数接受一个32位的LPVOID作为参数,返回一个UINT,线程函数的结构为:
UINT ThreadFunction(LPVOID pParam)
{
 //线程处理代码
 return0;
}
在线程处理代码部分通常包括一个死循环,该循环中先等待某事情的发生,再处理相关的工作:

while(1)
{
 WaitForSingleObject(…,…); //或WaitForMultipleObjects(…)
 /*
Do something
*/
}

一般来说,C++的类成员函数不能作为线程函数,这是因为在类中定义的成员函数,编译器会给其加上this指针。请看下列程序:

#include "windows.h"
#include "process.h"
class ExampleTask
{
 public:
  void taskmain(LPVOID param);
  void StartTask();
};
void ExampleTask::taskmain(LPVOID param)
{

}

void ExampleTask::StartTask()
{
_beginthread(taskmain,0,NULL);
}

int main(int argc, char* argv[])
{
 ExampleTask realTimeTask;
 realTimeTask.StartTask();
 return 0;
}

程序编译时出现如下错误:
error C2664: '_beginthread' : cannot convert parameter 1 from 'void (void *)' to 'void (__cdecl *)(void *)' None of the functions with this name in scope match the target type

再看下列程序:

#include "windows.h"
#include
class ExampleTask
{
 public:
  void taskmain(LPVOID param);
};

void ExampleTask::taskmain(LPVOID param)
{

}

int main(int argc, char* argv[])
{
 ExampleTask realTimeTask;
 _beginthread(ExampleTask::taskmain,0,NULL);
 return 0;
}

程序编译时会出错:

error C2664: '_beginthread' : cannot convert parameter 1 from 'void (void *)' to 'void (__cdecl *)(void *)' None of the functions with this name in scope match the target type

如果一定要以类成员函数作为线程函数,通常有如下解决方案:

1、将该成员函数声明为static类型,去掉this指针。我们将上述二个程序改变为:

#include "windows.h"
#include "process.h"

class ExampleTask
{
 public:
  void static taskmain(LPVOID param);
  void StartTask();
};

void ExampleTask::taskmain(LPVOID param)
{

}

void ExampleTask::StartTask()
{
 _beginthread(taskmain,0,NULL);
}

int main(int argc, char* argv[])
{
 ExampleTask realTimeTask;
 realTimeTask.StartTask();
 return 0;
}
以及第二个程序:
#include "windows.h"
#include
class ExampleTask
{
 public:
  void static taskmain(LPVOID param);
};

void ExampleTask::taskmain(LPVOID param)
{

}

int main(int argc, char* argv[])
{
 _beginthread(ExampleTask::taskmain,0,NULL);
 return 0;
}

则均能够编译通过。

将成员函数声明为静态虽然可以解决作为线程函数的问题,但是它带来了新的问题,那就是static成员函数只能访问static成员。解决此问题的途径是:在调用类静态成员函数(线程函数)时将this指针作为参数传入,并在改线程函数中用强制类型转换将this转换成指向该类的指针,通过该指针访问非静态成员。

2、不定义类成员函数为线程函数,手机APP ,而将线程函数定义为类的友元函数。这样,线程函数也可以有类成员函数同等的权限。因此,我们将程序修改为:

#include "windows.h"
#include "process.h"

class ExampleTask
{
 public:
friend void taskmain(LPVOID param);
  void StartTask();
};

void taskmain(LPVOID param)
{
ExampleTask *pTaskMain = (ExampleTask *) param; //通过pTaskMain指针引用
}

void ExampleTask::StartTask()
{
_beginthread(taskmain,0,this);
}
int main(int argc, char* argv[])
{
ExampleTask realTimeTask;
realTimeTask.StartTask();
return 0;
}

3、可以对非静态成员函数实现回调,并访问非静态成员。此法涉及到一些高级技巧,在此不再详述。

二、创建线程

进程的主线程由操作系统自动生成,Win32提供了CreateThread API来完成用户线程的创建,该API的原型为:

HANDLE CreateThread(
//指针指向 SECURITY_ATTRIBUTES 结构体
LPSECURITY_ATTRIBUTES lpThreadAttributes,

//统计堆栈的大小,以字节记
SIZE_T dwStackSize,

//未知
LPTHREAD_START_ROUTINE lpStartAddress,

//指向一个变量来通过这个线程
LPVOID lpParameter,

//Flags that control the creation of the thread
DWORD dwCreationFlags,

//只想一个变量来接受线程的定义
LPDWORD lpThreadId
);

注意:如果使用C/C++语言编写多线程应用程序,一定不能使用操作系统提供的CreateThread API,而应该使用C/C++运行时库中的_beginthread(或_beginthreadex),其函数原型为:

uintptr_t _beginthread(
//Start address of routine that begins execution of new thread
void( __cdecl *start_address )( void * ),  

//Stack size for new thread or 0.
unsigned stack_size,

//Argument list to be passed to new thread or NULL
void *arglist
);

uintptr_t _beginthreadex(

//Pointer to a SECURITY_ATTRIBUTES structure
void *security,

//统计堆栈的大小,以字节记
unsigned stack_size,
  unsigned ( __stdcall *start_address )( void * ),
void *arglist,

//Initial state of new thread (0 for running or CREATE_SUSPENDED for suspended);
unsigned initflag,
unsigned *thrdaddr
);

_beginthread函数与Win32 API 中的CreateThread函数有如下差异:

1、通过_beginthread函数我们可以利用其参数列表arglist将多个参数传递到线程;

2、_beginthread 函数初始化某些 C 运行时库变量,在线程中若需要使用 C 运行时库。

三、终止线程

线程的终止有如下四种方式:

1、线程函数返回;

2、线程自身调用ExitThread 函数即终止自己,它将参数fuExitCode设置为线程的退出码。
其原型为:

VOID ExitThread(UINT fuExitCode );

本文关键词: 线程| 深入浅出| Win32| 程序设计| 控制|

[相关阅读]

我们介绍

  兄弟网络科技工作室,专业从事日照百度推广,日照百度优化,日照网站建设,日照网络公司,日照网站制作,日照网站优化,日照软件制作。如果您感觉我们不错请分享↓给更多的人

收缩