客户端引入多线程
文章目錄
- 1 客戶端引入多線程
1 客戶端引入多線程
我們在客戶端引入多線程,示例代碼如下:
#include "EasyTcpClient.hpp" #include<thread>bool g_bRun = true; void cmdThread() {while (true){char cmdBuf[256] = {};scanf("%s", cmdBuf);if (0 == strcmp(cmdBuf, "exit")){g_bRun = false;printf("退出cmdThread線程\n");break;}else {printf("不支持的命令。\n");}} }//客戶端數量 const int cCount = 10000; //發送線程數量 const int tCount = 4; //客戶端數組 EasyTcpClient* client[cCount];void sendThread(int id) {//4個線程 ID 1~4int c = cCount / tCount;int begin = (id - 1)*c;int end = id*c;for (int n = begin; n < end; n++){client[n] = new EasyTcpClient();}for (int n = begin; n < end; n++){client[n]->Connect("192.168.1.110", 4567);printf("thread<%d>,Connect=%d\n", id,n);}//std::chrono::milliseconds t(5000);//std::this_thread::sleep_for(t);Login login[10];for (int n = 0; n < 10; n++){strcpy(login[n].userName, "lyd");strcpy(login[n].PassWord, "lydmm");}const int nLen = sizeof(login);while (g_bRun){for (int n = begin; n < end; n++){client[n]->SendData(login, nLen);//client[n]->OnRun();}}for (int n = begin; n < end; n++){client[n]->Close();} }int main() {//啟動UI線程std::thread t1(cmdThread);t1.detach();//啟動發送線程for (int n = 0; n < tCount; n++){std::thread t1(sendThread,n+1);t1.detach();}while (g_bRun)Sleep(100);printf("已退出。\n");return 0; }參考資料:
總結
- 上一篇: 人工智能的未来畅想 将影响每一个人的生活
- 下一篇: 索引处理类的设计与实现