临界区-IntelSoftware.pptVIP

  • 0
  • 0
  • 约7.49千字
  • 约 27页
  • 2017-03-05 发布于天津
  • 举报
临界区-IntelSoftware

Windows 并行程序设计 华南理工大学 陈虎 博士 tommychen74@ 参考文献 《深入浅出Win32多线程程序设计 》 C/C++多线程 在VC++6.0中,有两种多线程编程方法: 一是使用C运行时库及WIN32 API函数, 另一种方法是使用MFC,MFC对多线程开发有强大的支持。 Visual C++提供的多线程运行时库又分为静态链接库和动态链接库两类,而每一类运行时库又可再分为debug版和release版,因此Visual C++共提供了6个运行时库。如下表: 多线程编程 线程函数 创建线程 终止线程 挂起与恢复线程 睡眠 线程通信 创建线程 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 ); VC中的线程创建 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 ); 终止线程 线程的终止有如下四种方式: 线程函数返回; 线程自身调用ExitThread 函数即终止自己,其原型为: VOID ExitThread(UINT fuExitCode ); 也可使用C/C++运行时库函数 _endthread (_endthreadex)终止线程 其他线程调用TerminateThread函数,其原型为: BOOL TerminateThread(HANDLE hThread,DWORD dwExitCode); 包含线程的进程终止。 挂起与恢复线程 使用ResumeThread恢复线程运行 DWORD ResumeThread(HANDLE hThread); 调用SuspendThread函数强行挂起线程: DWORD SuspendThread(HANDLE hThread); 一个线程可以被挂起多次。如果一个线程被挂起n次,则该线程也必须被恢复n次才可能得以执行。 睡眠 VOID Sleep(DWORD dwMilliseconds);   该函数可使线程暂停自己的运行,直到dwMilliseconds毫秒过去为止。它告诉系统,自身不想在某个时间段内被调度。 获得线程退出码 BOOL WINAPI GetExitCodeThread(  HANDLE hThread,  LPDWORD lpExitCode );   如果执行成功,GetExitCodeThread返回TRUE,退出码被lpExitCode指向内存记录;否则返回FALSE

文档评论(0)

1亿VIP精品文档

相关文档