生活随笔
收集整理的這篇文章主要介紹了
网络游戏服务器编程
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
多線程編程示例
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<conio.h>
#define WIN32_LEAN_AND_MEANDWORD WINAPI
ThreadFunc(LPVOID
);int main()
{HANDLE hThred1
;HANDLE hThred2
;DWORD exitCode1
= 0;DWORD exitCode2
= 0;DWORD threadId
;hThred1
=CreateThread(NULL,0,ThreadFunc
,(LPVOID
)1,0,&threadId
);if (hThred1
){printf("Thread 1 launched\n");}hThred2
= CreateThread(NULL, 0, ThreadFunc
, (LPVOID
)2, 0, &threadId
);if (hThred2
){printf("Thread 2 launched\n");}for (;;){printf("Press any key to exit..\n"); _getch();GetExitCodeThread(hThred1
, &exitCode1
);GetExitCodeThread(hThred2
, &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(hThred1
);CloseHandle(hThred2
);printf("Thread 1 returned %d\n", exitCode1
);printf("Thread 2 returned %d\n", exitCode2
);return EXIT_SUCCESS
;
}DWORD WINAPI
ThreadFunc(LPVOID n
)
{Sleep((DWORD
)n
* 1000 * 2);return (DWORD
)n
* 10;
}
總結
以上是生活随笔為你收集整理的网络游戏服务器编程的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。