深入浅出Win32多线程程序设计-【2】线程控制

简介:   WIN32线程控制主要实现线程的创建、终止、挂起和恢复等操作,这些操作都依赖于WIN32提供的一组API和具体编译器的C运行时库函数。  1.线程函数  在启动一个线程之前,必须为线程编写一个全局的线程函数,这个线程函数接受一个32位的LPVOID作为参数,返回一个UINT,线程函数的结构...
 

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

  1.线程函数

  在启动一个线程之前,必须为线程编写一个全局的线程函数,这个线程函数接受一个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 <process.h>
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 <process.h>
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)不定义类成员函数为线程函数,而将线程函数定义为类的友元函数。这样,线程函数也可以有类成员函数同等的权限;

  我们将程序修改为:

#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)可以对非静态成员函数实现回调,并访问非静态成员,此法涉及到一些高级技巧,在此不再详述。

 

2.创建线程

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

HANDLE CreateThread(
 LPSECURITY_ATTRIBUTES lpThreadAttributes,//Pointer to a SECURITY_ATTRIBUTES structure
 SIZE_T dwStackSize, //Initial size of the stack, in bytes.
 LPTHREAD_START_ROUTINE lpStartAddress,
 LPVOID lpParameter, //Pointer to a variable to be passed to the thread
 DWORD dwCreationFlags, //Flags that control the creation of the thread
 LPDWORD lpThreadId //Pointer to a variable that receives the thread identifier
);


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

uintptr_t _beginthread(
 void( __cdecl *start_address )( void * ), //Start address of routine that begins execution of new thread
 unsigned stack_size, //Stack size for new thread or 0.
 void *arglist //Argument list to be passed to new thread or NULL
);
uintptr_t _beginthreadex(
 void *security,//Pointer to a SECURITY_ATTRIBUTES structure
 unsigned stack_size,
 unsigned ( __stdcall *start_address )( void * ),
 void *arglist,
 unsigned initflag,//Initial state of new thread (0 for running or CREATE_SUSPENDED for suspended);
 unsigned *thrdaddr
);


  _beginthread函数与Win32 API 中的CreateThread函数类似,但有如下差异:

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

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

  3.终止线程

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

  (1)线程函数返回;

  (2)线程自身调用ExitThread 函数即终止自己,其原型为:

VOID ExitThread(UINT fuExitCode );


  它将参数fuExitCode设置为线程的退出码。

  注意:如果使用C/C++编写代码,我们应该使用C/C++运行时库函数_endthread (_endthreadex)终止线程,决不能使用ExitThread
_endthread
函数对于线程内的条件终止很有用。例如,专门用于通信处理的线程若无法获取对通信端口的控制,则会退出。

  (3)同一进程或其他进程的线程调用TerminateThread函数,其原型为:

BOOL TerminateThread(HANDLE hThread,DWORD dwExitCode);


  该函数用来结束由hThread参数指定的线程,并把dwExitCode设成该线程的退出码。当某个线程不再响应时,我们可以用其他线程调用该函数来终止这个不响应的线程。

  (4)包含线程的进程终止。

  最好使用第1种方式终止线程,第2~4种方式都不宜采用。

  4.挂起与恢复线程

  当我们创建线程的时候,如果给其传入CREATE_SUSPENDED标志,则该线程创建后被挂起,我们应使用ResumeThread恢复它:

DWORD ResumeThread(HANDLE hThread);


  如果ResumeThread函数运行成功,它将返回线程的前一个暂停计数,否则返回0x FFFFFFFF

  对于没有被挂起的线程,程序员可以调用SuspendThread函数强行挂起之:

DWORD SuspendThread(HANDLE hThread);


  一个线程可以被挂起多次。线程可以自行暂停运行,但是不能自行恢复运行。如果一个线程被挂起n次,则该线程也必须被恢复n次才可能得以执行。

5.设置线程优先级

  当一个线程被首次创建时,它的优先级等同于它所属进程的优先级。在单个进程内可以通过调用SetThreadPriority函数改变线程的相对优先级。一个线程的优先级是相对于其所属进程的优先级而言的。

BOOL SetThreadPriority(HANDLE hThread, int nPriority);


  其中参数hThread是指向待修改优先级线程的句柄,线程与包含它的进程的优先级关系如下:

   线程优先级 = 进程类基本优先级 + 线程相对优先级

  进程类的基本优先级包括:

  (1)实时:REALTIME_PRIORITY_CLASS

  (2)高:HIGH _PRIORITY_CLASS

  (3)高于正常:ABOVE_NORMAL_PRIORITY_CLASS

  (4)正常:NORMAL _PRIORITY_CLASS

  (5)低于正常:BELOW_ NORMAL _PRIORITY_CLASS

  (6)空闲:IDLE_PRIORITY_CLASS

  我们从Win32任务管理器中可以直观的看到这六个进程类优先级,如下图:


  线程的相对优先级包括:

  (1)空闲:THREAD_PRIORITY_IDLE

  (2)最低线程:THREAD_PRIORITY_LOWEST

  (3)低于正常线程:THREAD_PRIORITY_BELOW_NORMAL

  (4)正常线程:THREAD_PRIORITY_ NORMAL (缺省)

  (5)高于正常线程:THREAD_PRIORITY_ABOVE_NORMAL

  (6)最高线程:THREAD_PRIORITY_HIGHEST

  (7)关键时间:THREAD_PRIOTITY_CRITICAL

  下图给出了进程优先级和线程相对优先级的映射关系:


  例如:

HANDLE hCurrentThread = GetCurrentThread();
//
获得该线程句柄
SetThreadPriority(hCurrentThread, THREAD_PRIORITY_LOWEST);


  6.睡眠

VOID Sleep(DWORD dwMilliseconds);


  该函数可使线程暂停自己的运行,直到dwMilliseconds毫秒过去为止。它告诉系统,自身不想在某个时间段内被调度。

  7.其它重要API

  获得线程优先级

  一个线程被创建时,就会有一个默认的优先级,但是有时要动态地改变一个线程的优先级,有时需获得一个线程的优先级。

Int GetThreadPriority (HANDLE hThread);


  如果函数执行发生错误,会返回THREAD_PRIORITY_ERROR_RETURN标志。如果函数成功地执行,会返回优先级标志。

  获得线程退出码

BOOL WINAPI GetExitCodeThread(
 HANDLE hThread,
 LPDWORD lpExitCode
);


  如果执行成功,GetExitCodeThread返回TRUE,退出码被lpExitCode指向内存记录;否则返回FALSE,我们可通过GetLastError()获知错误原因。如果线程尚未结束,lpExitCode带回来的将是STILL_ALIVE

获得/设置线程上下文
BOOL WINAPI GetThreadContext(
 HANDLE hThread,
 LPCONTEXT lpContext
);
BOOL WINAPI SetThreadContext(
 HANDLE hThread,
 CONST CONTEXT *lpContext
);


  由于GetThreadContextSetThreadContext可以操作CPU内部的寄存器,因此在一些高级技巧的编程中有一定应用。譬如,调试器可利用GetThreadContext挂起被调试线程获取其上下文,并设置上下文中的标志寄存器中的陷阱标志位,最后通过SetThreadContext使设置生效来进行单步调试。

  8.实例

  以下程序使用CreateThread创建两个线程,在这两个线程中Sleep一段时间,主线程通过GetExitCodeThread来判断两个线程是否结束运行:

#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <conio.h>

DWORD WINAPI ThreadFunc(LPVOID);

int main()
{
 HANDLE hThrd1;
 HANDLE hThrd2;
 DWORD exitCode1 = 0;
 DWORD exitCode2 = 0;
 DWORD threadId;

 hThrd1 = CreateThread(NULL, 0, ThreadFunc, (LPVOID)1, 0, &threadId );
 if (hThrd1)
  printf("Thread 1 launched"n");

 hThrd2 = CreateThread(NULL, 0, ThreadFunc, (LPVOID)2, 0, &threadId );
 if (hThrd2)
  printf("Thread 2 launched"n");

 // Keep waiting until both calls to GetExitCodeThread succeed AND
 // neither of them returns STILL_ACTIVE.
 for (;;)
 {
  printf("Press any key to exit.."n");
  getch();

  GetExitCodeThread(hThrd1, &exitCode1);
  GetExitCodeThread(hThrd2, &exitCode2);
  if ( exitCode1 == STILL_ACTIVE )
   puts("Thread 1 is still running!");
  if ( exitCode2 == STILL_ACTIVE )
   puts("Thread 2 is still running!");
  if ( exitCode1 != STILL_ACTIVE && exitCode2 != STILL_ACTIVE )
   break;
 }

 CloseHandle(hThrd1);
 CloseHandle(hThrd2);

 printf("Thread 1 returned %d"n", exitCode1);
 printf("Thread 2 returned %d"n", exitCode2);

 return EXIT_SUCCESS;
}

/*
* Take the startup value, do some simple math on it,
* and return the calculated value.
*/
DWORD WINAPI ThreadFunc(LPVOID n)
{
 Sleep((DWORD)n*1000*2);
 return (DWORD)n * 10;
}


  通过下面的程序我们可以看出多线程程序运行顺序的难以预料以及WINAPICreateThread函数与C运行时库的_beginthread的差别:

#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

DWORD WINAPI ThreadFunc(LPVOID);

int main()
{
 HANDLE hThrd;
 DWORD threadId;
 int i;

 for (i = 0; i < 5; i++)
 {
  hThrd = CreateThread(NULL, 0, ThreadFunc, (LPVOID)i, 0, &threadId);
  if (hThrd)
  {
   printf("Thread launched %d"n", i);
   CloseHandle(hThrd);
  }
 }
 // Wait for the threads to complete.
 Sleep(2000);

 return EXIT_SUCCESS;
}

DWORD WINAPI ThreadFunc(LPVOID n)
{
 int i;
 for (i = 0; i < 10; i++)
  printf("%d%d%d%d%d%d%d%d"n", n, n, n, n, n, n, n, n);
 return 0;
}


  运行的输出具有很大的随机性,这里摘取了几次结果的一部分(几乎每一次都不同):


  如果我们使用标准C库函数而不是多线程版的运行时库,则程序可能输出"3333444444"这样的结果,而使用多线程运行时库后,则可避免这一问题。

  下列程序在主线程中创建一个SecondThread,在SecondThread线程中通过自增对Counter计数到1000000,主线程一直等待其结束:

#include <Win32.h>
#include <stdio.h>
#include <process.h>

unsigned Counter;
unsigned __stdcall SecondThreadFunc(void *pArguments)
{
 printf("In second thread..."n");

 while (Counter < 1000000)
  Counter++;

 _endthreadex(0);
 return 0;
}

int main()
{
 HANDLE hThread;
 unsigned threadID;

 printf("Creating second thread..."n");

 // Create the second thread.
 hThread = (HANDLE)_beginthreadex(NULL, 0, &SecondThreadFunc, NULL, 0, &threadID);

 // Wait until second thread terminates
 WaitForSingleObject(hThread, INFINITE);
 printf("Counter should be 1000000; it is-> %d"n", Counter);
 // Destroy the thread object.
 CloseHandle(hThread);
}

 

目录
相关文章
|
3月前
|
安全 算法 Java
Java 多线程:线程安全与同步控制的深度解析
本文介绍了 Java 多线程开发的关键技术,涵盖线程的创建与启动、线程安全问题及其解决方案,包括 synchronized 关键字、原子类和线程间通信机制。通过示例代码讲解了多线程编程中的常见问题与优化方法,帮助开发者提升程序性能与稳定性。
142 0
|
3月前
|
数据采集 监控 调度
干货分享“用 多线程 爬取数据”:单线程 + 协程的效率反超 3 倍,这才是 Python 异步的正确打开方式
在 Python 爬虫中,多线程因 GIL 和切换开销效率低下,而协程通过用户态调度实现高并发,大幅提升爬取效率。本文详解协程原理、实战对比多线程性能,并提供最佳实践,助你掌握异步爬虫核心技术。
|
4月前
|
Java 数据挖掘 调度
Java 多线程创建零基础入门新手指南:从零开始全面学习多线程创建方法
本文从零基础角度出发,深入浅出地讲解Java多线程的创建方式。内容涵盖继承`Thread`类、实现`Runnable`接口、使用`Callable`和`Future`接口以及线程池的创建与管理等核心知识点。通过代码示例与应用场景分析,帮助读者理解每种方式的特点及适用场景,理论结合实践,轻松掌握Java多线程编程 essentials。
248 5
|
8月前
|
Python
python3多线程中使用线程睡眠
本文详细介绍了Python3多线程编程中使用线程睡眠的基本方法和应用场景。通过 `time.sleep()`函数,可以使线程暂停执行一段指定的时间,从而控制线程的执行节奏。通过实际示例演示了如何在多线程中使用线程睡眠来实现计数器和下载器功能。希望本文能帮助您更好地理解和应用Python多线程编程,提高程序的并发能力和执行效率。
258 20
|
8月前
|
安全 Java C#
Unity多线程使用(线程池)
在C#中使用线程池需引用`System.Threading`。创建单个线程时,务必在Unity程序停止前关闭线程(如使用`Thread.Abort()`),否则可能导致崩溃。示例代码展示了如何创建和管理线程,确保在线程中执行任务并在主线程中处理结果。完整代码包括线程池队列、主线程检查及线程安全的操作队列管理,确保多线程操作的稳定性和安全性。
|
10月前
|
NoSQL Redis
单线程传奇Redis,为何引入多线程?
Redis 4.0 引入多线程支持,主要用于后台对象删除、处理阻塞命令和网络 I/O 等操作,以提高并发性和性能。尽管如此,Redis 仍保留单线程执行模型处理客户端请求,确保高效性和简单性。多线程仅用于优化后台任务,如异步删除过期对象和分担读写操作,从而提升整体性能。
161 1
|
11月前
|
数据采集 Java Python
爬取小说资源的Python实践:从单线程到多线程的效率飞跃
本文介绍了一种使用Python从笔趣阁网站爬取小说内容的方法,并通过引入多线程技术大幅提高了下载效率。文章首先概述了环境准备,包括所需安装的库,然后详细描述了爬虫程序的设计与实现过程,包括发送HTTP请求、解析HTML文档、提取章节链接及多线程下载等步骤。最后,强调了性能优化的重要性,并提醒读者遵守相关法律法规。
292 0
|
5月前
|
机器学习/深度学习 消息中间件 存储
【高薪程序员必看】万字长文拆解Java并发编程!(9-2):并发工具-线程池
🌟 ​大家好,我是摘星!​ 🌟今天为大家带来的是并发编程中的强力并发工具-线程池,废话不多说让我们直接开始。
191 0
|
8月前
|
Linux
Linux编程: 在业务线程中注册和处理Linux信号
通过本文,您可以了解如何在业务线程中注册和处理Linux信号。正确处理信号可以提高程序的健壮性和稳定性。希望这些内容能帮助您更好地理解和应用Linux信号处理机制。
133 26
|
8月前
|
Linux
Linux编程: 在业务线程中注册和处理Linux信号
本文详细介绍了如何在Linux中通过在业务线程中注册和处理信号。我们讨论了信号的基本概念,并通过完整的代码示例展示了在业务线程中注册和处理信号的方法。通过正确地使用信号处理机制,可以提高程序的健壮性和响应能力。希望本文能帮助您更好地理解和应用Linux信号处理,提高开发效率和代码质量。
137 17