S.l.e!ep.¢%

像打了激速一样,以四倍的速度运转,开心的工作
简单、开放、平等的公司文化;尊重个性、自由与个人价值;
posts - 1098, comments - 335, trackbacks - 0, articles - 1
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Thread Class

Posted on 2009-02-08 21:29 S.l.e!ep.¢% 阅读(1944) 评论(7)  编辑 收藏 引用 所属分类: VC
上一篇  代码有问题,经过两次重构, 88 line 代码

测试代码::
 1
 2#include "thread.h"
 3
 4class testThread
 5{
 6public:
 7    testThread()
 8    {
 9        m_Obj.setObj(test);
10        
11    }

12
13    virtual ~testThread()
14    {
15        m_Obj.stop();
16    }

17
18    static unsigned int test(void* pVoid)
19    {
20        testThread* pThis = (testThread*)pVoid;
21
22        ::EnterCriticalSection(&pThis->m_cs);
23        cout << "test" << endl;
24        ::LeaveCriticalSection(&pThis->m_cs);
25        return 0;
26    }

27
28
29    void go()
30    {
31        m_Obj.start(this);
32    }

33
34
35    CThread m_Obj;
36    static CRITICAL_SECTION m_cs;
37}
;
38
39CRITICAL_SECTION testThread::m_cs;
40
41int main()
42{    
43    ::InitializeCriticalSection(&testThread.m_cs);
44
45    {
46        testThread B[100000];
47        
48        forint i = 0; i < 100000; i++ )
49        {
50            B[i].go();
51        }

52    }

53
54    ::DeleteCriticalSection(&testThread.m_cs);
55
56    return 0;
57}


实现代码::
 1#include <windows.h>
 2#include <process.h>
 3
 4class CThread
 5{
 6public:
 7    CThread(unsigned int (*pfnCall)(void* pVoid))
 8    {
 9        m_pfnCall   = pfnCall;
10        m_hThread   = INVALID_HANDLE_VALUE;
11        m_nThreadID = 0;
12        m_pObj      = NULL;
13    }

14
15    virtual ~CThread()
16    {
17        stop();
18    }

19
20    static unsigned __stdcall _ThreadProc(void* pVoid)
21    {
22        CThread* pThis = (CThread*)pVoid;
23
24        if ( NULL != pThis->m_pfnCall )
25            pThis->m_pfnCall(pThis->m_pObj);
26
27         _endthread();
28
29        return 0;
30    }

31
32    bool start(void* pVoid)
33    {
34        m_pObj = pVoid;
35        m_hThread = (HANDLE)_beginthreadex(NULL, 0, _ThreadProc, (void *)this0&m_nThreadID);
36        
37        if ( 0 == m_hThread )
38            return false;
39        else
40            return true;
41    }

42
43    bool stop()
44    {
45        #define ONE_SECOND 1000L
46
47        if ( INVALID_HANDLE_VALUE == m_hThread )
48            return true;
49
50        BOOL bRet   = FALSE;
51        DWORD dwRet = 0;
52
53        DWORD dwExitCode = 0;
54        if( TRUE == ::GetExitCodeThread(m_hThread, &dwExitCode) )
55        {
56            if( STILL_ACTIVE == dwExitCode )            
57                dwRet = ::WaitForSingleObject(m_hThread, ONE_SECOND); // INFINITE
58        }

59                
60        if ( dwRet == WAIT_TIMEOUT || dwRet == WAIT_FAILED ) 
61        {
62            bRet = ::TerminateThread(m_hThread, 1);
63        }

64        else
65        {
66            bRet = TRUE;
67        }

68        
69        if ( TRUE == bRet )
70        {
71            return true;
72        }

73        else
74        {
75            return false;
76        }

77    }
    
78
79private:
80    CThread()
81    {
82    }

83
84    HANDLE       m_hThread;
85    unsigned int m_nThreadID;
86    unsigned int (*m_pfnCall)(void* pVoid);
87    void*        m_pObj;
88}
;

Feedback

# re: 查了MSDN,发现之前写的类有问题  回复  更多评论   

2009-02-08 13:58 by IT公司面试手册
还没写完吧
怎么文不对题呢

# re: Thread Class  回复  更多评论   

2009-02-08 17:27 by lwan
void setObj(unsigned int (*pfnCall)(void* pVoid))
建议放在构造函数里面,因为没有这个回调这个线程就是一个dummy线程。

# re: Thread Class[未登录]  回复  更多评论   

2009-02-08 22:17 by 关中刀客
说真的,这个封装的很不好

# re: Thread Class  回复  更多评论   

2009-02-08 22:24 by 放屁阿狗
的确不好

# re: Thread Class  回复  更多评论   

2009-02-08 22:53 by Dancefire
封装这个作甚?是自己玩么?如果真用起来,还是建议你考虑一下很多已经作好的封装。许多出色的线程库都充分利用了C++特性,并且是跨平台的,比这个要好的多。比如,

[boost::thread]

http://www.boost.org/doc/libs/1_37_0/doc/html/thread.html
http://www.stlchina.org/twiki/bin/view.pl/Main/BoostThread

如果需要小巧,也有ting,也是跨平台的:
[ting]
http://code.google.com/p/ting/

也有含在glibmm里面的Glib::Thread
[glibmm/threads]
http://www.gtkmm.org/docs/glibmm-2.4/docs/reference/html/group__Threads.html

如果不是coding 4 fun,而是有任何使用价值,不妨停止重复造轮子,看看已有实现先。毕竟那些充分利用了C++特性,而且是跨平台的。另外需要注意的是,C++并不追求代码行数最少,而是追求效率和结构,不要陷入用最短的代码就是最好的误区。

# re: Thread Class  回复  更多评论   

2009-02-09 13:26 by 路人
重新发明轮子,看看这个实现。
http://www.viksoe.dk/code/thread.htm

# re: Thread Class  回复  更多评论   

2009-02-09 15:24 by true
@Dancefire
从你的本贴中的回复,以及以前对我的一个帖子的回复看,兄弟知识面比较广,全面,值得学习。不知道你从事什么工作,支持使用各种库,反正我工作过的几个公司,都是从头开始开发,除非是压缩等算法相关性强的领域,才会考虑使用开源的库。

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