- 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
您可能关注的文档
最近下载
- 《信任五环》超级销售拜访技巧_分享.pptx VIP
- 五四以来反动派、地主资产阶级学者尊孔复古言论辑录-北京出版.pdf VIP
- 2025年上海市初中学业水平考试英语试卷试题(含答案详解)_可搜索.pdf VIP
- CECS382-2014 水平定向钻法管道穿越工程技术规程.pdf
- 第9课 改革开放的起步-新教材八下优质课件.pptx VIP
- THMDZT-1型机械装调技术综合实训装置实训指导书(总33页.pptx VIP
- 个人客户融资租赁合同(挖掘机).doc VIP
- 新青岛版(六三制)四年级上册科学全册知识点(超全).doc
- 2025译林版新教材七年级下册英语单词衡水体字帖.pdf VIP
- 信任五环-销售沟通拜访技巧.pptx VIP
原创力文档

文档评论(0)