网络游戏服务器编程

网络游戏服务器编程,第1张

网络游戏服务器编程 多线程编程示例
#include
#include
#include
#include
#define WIN32_LEAN_AND_MEAN

DWORD 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 launchedn");

	}

	hThred2 = CreateThread(NULL, 0, ThreadFunc, (LPVOID)2, 0, &threadId);
	if (hThred2)
	{
		printf("Thread 2 launchedn");
	}

	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 %dn", exitCode1);
	printf("Thread 2 returned %dn", exitCode2);

	return EXIT_SUCCESS;
}

DWORD WINAPI ThreadFunc(LPVOID n)
{
	Sleep((DWORD)n * 1000 * 2);
	return (DWORD)n * 10;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5694818.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存