站长论坛

标题: 推荐我写的一个C++多线程程序开发库 [打印本页]

作者: lupd    时间: 2007-9-17 22:23
标题: 推荐我写的一个C++多线程程序开发库
代码从写出来到整理完成有好久了,如果有什么疑问或者代码中发现有缺陷,直接与我联系duanjigang1983@126.com

概述一下原理和每个类的作用
(1): CTask 一个任务基类,作为线程的参数用
(2): CWorkThread:工作线程类,轮询检测参数是否可用,如果可用的话,
     就去调用参数CTask的执  行函数Execute,如果参数不可用就等待
(3): CWorkQueue:是一个任务队列,里面装载CTask子类的对象,是一个对象的容器
(4): CWorkThreadPool:工作线程池,其中包含了工作线程组,调度线程,调度线程不停的检测
     任务队列中是否有可用任务对象,如过取到一个有效的任务对象,就从工作线程组中查找出来
      一个空闲的工作线程,把这个任务交给这个空闲的线程去执行,如果没有空闲线程则创建新
     的工作线程并添加到工作线程组中。
(1),(2),(3),(4)组成了库的核心,可以稍加修改进行应用
  其他附属类:
(5):CSimpleUDP 一个简易的封装的UDP通讯类
(6):CLogHelper 一个线程安全的日志类,将日志存储到指定文件并能打印到标准输出
    特点:支持windows和Linux平台

   使用举例:CUDPClient 和CUDPServer 一个线程发送UDP包,另外一个接收UDP包
使用方法:
在src目录执行make命令,相应的库将生成到/usr/local/lib下
然后在/etc/ld.so.conf中添加/usr/local/lib
保存退出
执行ldconfig -v
开发时,只需要新建的类继承CTask类,然后将编译好的Tasklib库链接进去就可,Tasklib封装了main函数和线程以及任务队

列的管理,最好修改UDPClient的例子
日志:linux系统存放在/cucme.log,windows存放在C:\cucme.log中,如需修改,直接修改头文件,重新编译库。
注意:在windows先使用时最好将库文件一起重新编译,链接使用lib文件有异常存在。另外运行UDPClient例子时,首先
将UDPCLient.cpp中的IP地址进行修改,否则发送目的地错误。server将不能接收到。

代码有些地方可能会写的比较臭,希望高手赐教,更希望有朋友能在系统框架设计上给出好的建议。
如果有改进或者新的应用例子,我会及时贴上来的
  1. #define _COMMON_H_
  2. #define __DEBUG__
  3. #include
  4. #include
  5. #include
  6. #include "stdio.h"
  7. ////////////////
  8. #include
  9. #ifdef _WIN32
  10. #pragma comment(lib, "Ws2_32.lib")
  11. #include
  12. #include
  13. const char * const LogFile = "C://cucme.log";
  14. #define CLOSE_FILE(fd) _close(fd)
  15. #endif

  16. #ifdef __LINUX__
  17. #include
  18. #include
  19. #include
  20. const char* const LogFile = "/cucme.log";
  21. #endif

  22. #ifdef _WIN32
  23. //head file
  24. #include
  25. #include
  26. #pragma comment(lib, "Ws2_32.lib")

  27. //关闭套结字
  28. #define CLOSE(s) closesocket(s);
  29. #define CLEAN_SCREEEN system("cls");

  30. static void SLEEP(int t1, int t2)
  31. {
  32.         Sleep(t1*1000 + t2);
  33. }

  34. //types
  35. typedef CRITICAL_SECTION  LockType;
  36. typedef int socklen_t;
  37. #endif

  38. #ifdef __LINUX__
  39. #include
  40. #include
  41. #include
  42. #include
  43. #include
  44. #include
  45. #include
  46. #define CLEAN_SCREEEN system("clear");
  47. #define CLOSE(s) close(s)
  48. static void SLEEP(int t1, int t2)
  49. {
  50.         struct timeval tm;
  51.         tm.tv_sec = t1;
  52.         tm.tv_usec = t2;
  53.         select(0, NULL, NULL, NULL, &tm);
  54. }
  55. typedef pthread_mutex_t LockType;
  56. #endif

  57. //---------------------------global functions-------------------------
  58. //初始化锁变量
  59. static int InitLock(LockType * lock)
  60. {
  61. #ifdef _WIN32
  62.         InitializeCriticalSection(lock);
  63. #else
  64.         if(pthread_mutex_init(lock, NULL) != 0)
  65.         {
  66.                 return -1;
  67.         }
  68. #endif
  69.         return 1;
  70. }

  71. //加锁操作
  72. static int LockOn(LockType* lock)
  73. {
  74. #ifdef _WIN32
  75.         EnterCriticalSection(lock);
  76. #endif
  77. #ifdef __LINUX__
  78.         if(pthread_mutex_lock(lock) != 0)
  79.         {
  80.                 return -1;
  81.         }
  82. #endif
  83.         return 1;
  84. }
  85. //解锁操作
  86. static int LockOff(LockType* lock)
  87. {
  88. #ifdef _WIN32
  89.     LeaveCriticalSection(lock);
  90. #endif
  91. #ifdef __LINUX__
  92.         if(pthread_mutex_unlock(lock) != 0)
  93.         {
  94.                 return -1;
  95.         }
  96. #endif
  97.         return 1;
  98. }
  99. //初始化网络
  100. static int  InitNetWork()
  101. {
  102. #ifdef _WIN32
  103.         WSAData w;
  104.     if(WSAStartup(0x0101, &w) != 0)
  105.         {
  106.                 return -1;
  107.     }
  108. #endif
  109.         srand((unsigned)time(NULL));
  110.         return 1;
  111. }
  112. //关闭网络连接
  113. static int TerminateNetWork()
  114. {
  115. #ifdef _WIN32
  116.         if(WSACleanup())
  117.         {
  118.                 return -1;
  119.         }
  120. #endif
  121.         return 1;
  122. }

  123. static LockType LockPrint;
  124. static bool Initted = false;
  125. static void ThreadSafePrint(const char * szMsg)
  126. {
  127.         if(!Initted)
  128.         {
  129.                 InitLock(&LockPrint);
  130.                 Initted = true;
  131.         }
  132.         LockOn(&LockPrint);
  133. #ifdef __DEBUG__
  134.         printf(szMsg);
  135. #endif
  136.         LockOff(&LockPrint);
  137. }


  138. #endif
复制代码





欢迎光临 站长论坛 (http://www.tzlink.com/bbs/) Powered by Discuz! X3.2