Windows Multithreading Sample

来源:互联网 发布:动态域名解析软件 编辑:程序博客网 时间:2024/05/16 08:28

说明:

最近项目除了需要用到渲染的知识,还涉及socket与multithreads,做如下多线程程序总结


参考书籍:《Multithreading Application in Win32》 Jim Beveridge & Robert


/* * File name : MultiThreads.cpp * Last updated date : 2011.9.19 * Author : Dormy.ET.C * Description : * * 1.释放核心对象API - closeHandle() * 2.获取退出代码API - GetExitCodeThread() * 3.完全强制中断线程API - ExitThread() * */#define WIN32_LEAN_AND_MEAN#include <Windows.h>#include <stdio.h>#include <stdlib.h>#include <conio.h>DWORD WINAPI ThreadFunc(LPVOID);void  ThreadSubFunc(void);#define MAX_THREADS 256int main(int argc, char *argv[]){HANDLE hThread;DWORD  exitCode = 0;DWORD  threadID;hThread = ::CreateThread(NULL, 0, ThreadFunc, (LPVOID)1, 0, &threadID);if( hThread )printf("Thread launched !\n");while(1){BOOL rc;rc = ::GetExitCodeThread(hThread, &exitCode);if( rc && exitCode != STILL_ACTIVE ) break;}::CloseHandle(hThread);printf("Thread returned %d\n", exitCode);::system("Pause");return EXIT_SUCCESS;}DWORD WINAPI ThreadFunc(LPVOID n){printf("Thread running !\n");ThreadSubFunc();return 0;}void ThreadSubFunc(void){printf("About to exit thread !\n");::ExitThread(4);// 以下语句永远不会执行printf("Never print this line\n");}

原创粉丝点击