您的位置:首页 > 理论基础 > 计算机网络

多线程网络通信的一种情形

2010-05-15 23:01 176 查看
考虑在网络通信中,模拟一种情形:server需要完成一项任务,首先它根据一张可以规定的client优先顺序表,将此项任务依次发送给每个client。当server第一时间收到某一client返回的任务结果后,将停止向其余client发送此项任务,并且显示任务结果,并终止server自己完成任务。然而,当server在第一时间没有收到某一client返回任务的时候,可以选择自己来完成该项任务,如果选择自己完成任务,将终止向client继续发送任务。

下面代码是对上述过程的一个模拟。

/*
2010-5-15 wcdj
*/
#include <cstdio>
#include <windows.h>// CreateThread
#include <ctime>
#include <conio.h>
DWORD WINAPI MyThread(LPVOID lparam);
typedef struct _para 
{
	int num;
	bool bRes;
	unsigned int ntime;
}PARA;
int main()
{
	PARA myPara;
	myPara.bRes=false;// initialize to false
	int i=0;	
	srand((unsigned)time(NULL));// set seed
	while (++i,1)// start two thread
	{
		DWORD  dwThreadId;  
		HANDLE  hThread;  
		
		myPara.num=i;
		myPara.ntime=rand()%9001;//0~9000 millisecond
		//create a thread to manage recv
		hThread=CreateThread(NULL,NULL,MyThread,(LPVOID)&myPara,0,&dwThreadId);// note, myPara 
		if(hThread==NULL)  
		{  
			printf("The %d CreatThread MyThread() failed/n",myPara.num);  
		}  
		else  
		{  
			printf("The %d CreateThread OK/n",myPara.num);  
		}
		CloseHandle(hThread);// if not using this handle release it
		// wait a while, otherwise we will start another thread to work
		Sleep(1000);
		if (myPara.bRes)
		{
			printf("There have been: %d threads...and go out/n",myPara.num);
			break;
		}
	}
	// wait other thread to have done work
	printf("wait other thread to have done work.../n");
	
	// assuming all the threads have not done work, the father thread do it 
	// (note, in this codes this case does not exist, but we consider the case that in networking programming) 
	printf("===============================/n");
	printf("if all the threads have not done work, you do it/n");
	printf("Enter a  random character to simulate:/n");
	printf("===============================/n");
	int ch=getch();// will block here...
	// check
	if (myPara.bRes)
	{
		printf("call _ungetch function to put the character %c back into keyboard buffer/n",ch);
	} 
	else
	{
		printf("the character you input is: %c/n");
	}	
	
	getch();
	return 0;
}
DWORD WINAPI MyThread(LPVOID lparam)
{
	PARA * p=(PARA *)lparam;
	int id=p->num;// save the current num
	// wait for a  random time	
	printf("wait %d millisecond.../n",p->ntime);
	Sleep(p->ntime);
	p->bRes=true;// set to true
	printf("[%d] thread has done the work/n",id);	
	// simulate input 
	_ungetch('q');
	return 0;
}
/*
output:
The 1 CreateThread OK
wait 8185 millisecond...
The 2 CreateThread OK
wait 4966 millisecond...
The 3 CreateThread OK
wait 6828 millisecond...
The 4 CreateThread OK
wait 2267 millisecond...
The 5 CreateThread OK
wait 6026 millisecond...
The 6 CreateThread OK
wait 3102 millisecond...
[4] thread has done the work
[2] thread has done the work
There have been: 6 threads...and go out
wait other thread to have done work...
===============================
if all the threads have not done work, you do it
Enter a  random character to simulate:
===============================
call _ungetch function to put the character q back into keyboard buffer
[6] thread has done the work
[1] thread has done the work
[3] thread has done the work
[5] thread has done the work
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: