socketref,再见!高德

https://github.com/adoggie

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  246 Posts :: 4 Stories :: 312 Comments :: 0 Trackbacks

常用链接

留言簿(54)

我参与的团队

搜索

  •  

最新评论

阅读排行榜

评论排行榜

我记得有个Omni的类库中有对线程的封装,看了看,不爽
ACE也有线程类,不过要用必须连接其庞大的ace.so 不爽
ICE也有,不过太简单
还是自己写:
哈哈,支持win32和posix接口
这个咚咚已经在好多地方用过,希望提点

 1 
 2 #ifndef _THREAD_H
 3 #define _THREAD_H
 4 
 5 #include "nv_vartype.h"
 6 #include "nvplat.h"
 7 
 8 #ifdef WIN32
 9 #include <windows.h>
10 typedef HANDLE NVHANDLE;
11 
12 #define NVCreateThead(handle,proc,param,bret) {\
13                                                 DWORD thrid;\
14                                                 handle = CreateThread(0,0,(LPTHREAD_START_ROUTINE)proc,(void*)param,0,&thrid);\
15                                                 bret  = true;\
16                                               }
17 #define THREAD_DETACH
18 
19 #endif
20 
21 #ifdef _UNIX
22 typedef pthread_t NVHANDLE;
23 
24 #define NVCreateThead(handle,proc,param,bret)    {\
25                                                  int ret;\
26                                                  ret= pthread_create(&handle,0,proc,param);\
27                                                  bret= (ret!=0)?false:true;\
28                                                 }
29 #define THREAD_DETACH            pthread_detach(pthread_self());
30 #endif
31 
32 
33 typedef void *(*THREAD_PROC)(void *param);
34 
35 
36 class NVThread{
37 public:
38     enum STATUS{
39         RUNNING,
40         STOP
41     };
42     enum WHERE{
43         HEAD,
44         TAIL
45     };
46     NVThread();
47     ~NVThread();    
48     bool        Create(void *param=0);
49     void        Terminate();
50     void        Wait();
51     STATUS         GetStatus();
52     void         SetStatus(STATUS status);
53     WHERE         GetWhere();
54     void         SetWhere(WHERE where);
55     static void *     NewThread(void *);
56     virtual void     Run(void *param);
57 protected:    
58     NVHANDLE    _thrhand;
59     STATUS        _status;
60     WHERE        _where;
61     void *        _param;
62     void *        _void;
63 };
64 
65 #define  THREAD_START(t)    (t).SetStatus(RUNNING); (t).SetWhere(HEAD);
66 #define  THREAD_END(t)    (t).SetStatus(STOP); (t).SetWhere(TAIL);THREAD_DETACH;
67 #define  THREAD_CONTINUE(t) ((t).GetStatus()== NVThread::RUNNING)?true:false
68 
69 #define THREAD_START_T    THREAD_START(*this)
70 #define THREAD_END_T    THREAD_END(*this)
71 #define THREAD_CONTINUE_T THREAD_CONTINUE(*this)
72 
73 
74 
75 #include "nvthread.i"
76 
77 #endif
78 //--
79 
 1 
 2 
 3 #include "nvthread.h"
 4 
 5 inline  
 6 NVThread::NVThread(){
 7     _where = TAIL;
 8     _status = STOP;
 9     
10 }
11 inline  
12 NVThread::~NVThread(){
13 }
14 
15 inline  
16 bool NVThread::Create(void *param){
17     bool ret;
18     NVCreateThead(_thrhand,NVThread::NewThread,this,ret);
19     if( ret){ //--线程创建成功,等待线程运行状态改变
20         while( GetStatus()!=NVThread::RUNNING){
21             SLEEP_MSEC(100);
22         }
23     }
24     return ret;
25 }
26 
27 inline 
28 void NVThread::Terminate(){
29     _status = STOP;
30 }
31 
32 inline 
33 void * NVThread::NewThread(void *param){
34     NVThread *thread = (NVThread *)param;
35     THREAD_START(*thread);
36     thread->Run(thread->_param);
37     THREAD_END(*thread);
38     return 0;
39 }
40 
41 inline 
42 void NVThread::Run(void *param){
43 /*    while(THREAD_CONTINUE(*this)){
44         Sleep(100);
45     }    */
46 }
47 
48 inline 
49 void NVThread::Wait(){
50     while(GetWhere()!=TAIL){
51         SLEEP_MSEC(100);
52     }
53 }
54 
55 inline 
56 NVThread::STATUS NVThread::GetStatus(){
57     return _status;
58 }
59 
60 inline 
61 void NVThread::SetStatus(STATUS status){
62     _status = status;
63 }
64 
65 inline 
66 NVThread::WHERE NVThread::GetWhere(){
67     return _where;
68 }
69 
70 inline 
71 void NVThread::SetWhere(WHERE where){
72     _where = where;
73 }
74 
75 
76 
77 

派生线程类实现分派外部全局函数
 1
 2#ifndef _THREADOBJECT_H
 3#define _THREADOBJECT_H
 4
 5/*
 6    file:    nvthreadobj.h
 7    class:     NVThreadObject
 8    desc:    控制外部线程行为
 9*/

10
11
12#include "nvthread.h"
13
14class NVThreadObject:public NVThread{
15public:
16    void     Run(void *param){
17        _threadproc(param);    
18    }

19    bool    Create(THREAD_PROC proc,void *param=0){
20        _threadproc = proc;
21        _param = param;
22        return NVThread::Create();
23    }

24protected:    
25    THREAD_PROC    _threadproc;
26}
;
27#endif
posted on 2006-03-11 00:24 放屁阿狗 阅读(510) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理