多线程网络通信的一种情形
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 */
相关文章推荐
- 基于TCP网络通信的简易多线程GUI聊天室
- C#做了个多线程网络通信的例子
- 简单的多线程和网络通信的模拟(求三角形面积)
- 调试posix多线程 socket网络通信有感
- 【JAVA】多线程网络通信---聊天室
- 多线程版本网络通信
- 一种自定义网络通信协议
- 简单的现实多线程的Socket网络通信,对前面一篇的补充的加入线程
- 以libevent网络库为引:网络通信和多线程
- Android移动开发-使用多线程进行网络聊天室通信的实现
- 简单多线程和网络通信模拟聊天
- C#网络通信(2)--TCP编程和多线程
- IOS 中的多线程网络(二)—— 线程间通信与线程安全
- 无线通信中功率/速率调度的优化问题:一种最简单的情形
- 网络编程 UDP通信的过程 TCP通信过程 多线程文件上传
- 网络多线程-NSOperation线程间通信
- 网络多线程-NSThread实现线程间通信
- 网络编程_TCP_Socket通信_聊天室_客户端多线程_群聊JAVA191-192
- android多线程网络通信
- 数据库,界面,多线程,网络通信