• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            stlf

            uti.thread

                                                                                UTI.THREAD

                我寫的線程類, 用GP技術和OOP思想對OS的線程創建進行了封裝, 使用起來靈活方便(后附example),力求簡單實用。

                這個線程類需要boost庫的頭文件支持, 但無需對boost庫進行編譯, 要知道編譯boost庫是一個漫長的過程... 而且boost庫里面有很多我們平常用不上的一些“高級”玩意:-).


                此外, 我還寫了一系列使用靈活、方便,且在編程中經常需要使用的類(如同步鎖, 線程池, 同步隊列, 常用I/O對象, 常用算法等), 我其中的大部分放入了uti namespace之中, 所以稱之為uti庫。我盡量保持這些類的相對獨立,一般只需要注釋掉某行就可以單獨使用。總之, 我寫此類庫所奉行的原則是 --- 穩定,靈活,簡單(在example中應該可以看得到:-)).其他類庫將出現在后續文章中, 希望大家喜歡, 也希望能和大家交流:-) stlflingfeitang@gmail.com


            /*******************************************************************

            created:   2008/06/05

            created:   5:6:2008  

                       Vision     0.8

                       file base: uti::thread

                       file ext:  h

            author:       lingfei.tang

             

                       copyright(c) 2008.9

             

                       stlflingfeitang@gmail.com

             

                       歡迎使用本輕量級的線程類庫---uti::thread

             

                       目前版本只支持Win32操作系統,且需要boost庫的支持,

                       但只需引入頭文件, 可免去編譯boost的漫長等待過程

                       請注意boost::thread是需要編譯才能使用的, 而且已經比較龐大復雜。

             

                       本線程庫支持兩種形式的函數簽名:

                       1. 若是自由函數(全局函數):

                       RT func_name(PT param);

                       RT, PT 均已泛化. 即帶一個參數的自由函數都滿足條件

             

                       2. 若是成員函數:

                       RT memb_func_name(void);

                       父類, 和返回型別已經被泛化.

                       以上兩種形式的函數其實已經可以滿足大多數需求, 且形式簡潔.

             

                       ***你可以自由使用本庫, 但請保留本說明和作者名及聯系方式***

                      

                       0.8版本已經過單元測試.

             

                       若有問題或建議請與我聯系:-)stlflingfeitang@gmail.com

             

            This code may be used in compiled form in any way you desire. This

            file may be redistributed unmodified by any means PROVIDING it is

            not sold for profit without the authors written consent, and

            providing that this notice and the authors name and all copyright

            notices remains intact.

             

            An email letting me know how you are using it would be nice as well.

             

            This file is provided "as is" with no expressed or implied warranty.

            The author accepts no liability for any damage/loss of business that

            this product may cause.

             

            *******************************************************************/

            /*--------------------------使用說明-------------------------------

             

            本線程類的使用時簡單且靈活的:

            1. 在線程對象的構造函數中傳入你的線程函數指針, 以及相關參數

                eg:

                void freefunc(void*p){}//自由函數

                uti::thread thread_1(&freefunc, NULL);

             

                class aclass{public: threadfunc(){}};

                aclass ainstan;//對象

                uit::thread thread_2(&aclass::threadfunc, &ainstan);

                  

            2. 調用start方法即可以啟動線程

                eg:

                thread_1->start();

                thread_2->start();

             

            3. 調用join方法可以等待線程的退出,在線程對象析構時可以保證該線程的退出

                eg:

                thread_1->join();

                thread_2->join();

             

            另外,在后面還提供了兩個用于繼承復用的包裝類, 繼承復用可能時最簡單的,但是耦合也是較高的:-)

                eg:

                class mythreadclass :public ThreadObjBase

                {

                   void Run

                   {

                       //...重寫該線程函數

                   }

                }

             

            ***需要特別說明的是:請確保你的線程函數是可以解棧的,否則利用該函數創建的線程將無法退出:(

             

            ------------------------------------------------------------------*/

            /*modification record

            08-08-29 by tlf :為了支持友元方式的訪問, 將Thread_MemFun中的原對象的動態創建

                           移到父類thread 中創建.

            */

             

            #if !defined(_UTI_THREAD__)

            #define _UTI_THREAD__

             

            //#include "utidef.h"http://如果你想只使用本線程類, 請注釋該行

            #include <Windows.h>

            #include <process.h>

            #include <boost\function.hpp>

            #include <boost\shared_ptr.hpp>

            #include <vector>

             

            #if !defined(_UTI_DEF__)

            #define  BEGIN_UTI

            #define  END_UTI

            #else

            #include <iostream>

            #endif

             

            BEGIN_UTI

             

             

            class ThreadFunbase {public: virtual void run() = 0;};

            template<typename RT, typename CT>

            class Thread_MemFun: public ThreadFunbase

            {

            public:

                typedef RT(CT::*FT)();

                explicit Thread_MemFun( RT(CT::*f)(), CT *pcobj):m_fp(f), m_p(pcobj)

                {}

             

                virtual void run()

                {

                   if (m_p)

                       (m_p->*m_fp)();

                   else

                       throw std::logic_error("the thread function have no owner!");

                }

             

            private:

                CT *m_p;

                FT m_fp;

            };

             

            template<typename RT, typename PT>

            class Thread_Fun: public ThreadFunbase

            {

            public:

                explicit Thread_Fun(RT(*f)(PT), PT param){

                   m_funptor = f;

                   m_param = param;

                }

             

                virtual void run()

                {

                   m_funptor(m_param);

                }

            private:

                boost::function1<RT, PT> m_funptor;

                PT m_param;

            };

             

            class thread

            {

            public:

                template<typename RT, typename CT>

                   explicit thread(RT(CT::*f)(), CT *pcobj = NULL, std::string thread_name= ""):

                m_thread_name(thread_name),m_id(0), m_threadhandle(0), m_bisrunning(false), m_bisout(true)

                {

                   CT *po = pcobj;

                   if (NULL == po)

                   {

                       get_static_obj_ptr(&po);

                   }

                   m_pfunptor.swap(boost::shared_ptr<ThreadFunbase>(new Thread_MemFun<RT, CT>(f, po)));

             

                }

                template<typename RT, typename PT>

                   explicit thread( RT(*f)(PT), PT param, std::string thread_name= ""):

                m_thread_name(thread_name),m_id(0), m_threadhandle(0), m_bisrunning(false)

                {  

                   m_pfunptor.swap(boost::shared_ptr<ThreadFunbase>(new Thread_Fun<RT,PT>(f, param)));

                }

             

                virtual ~thread()

                {

                   join();

                }

             

                inline unsigned int getid() const

                {

                   return m_id;

                }

             

                inline void start()

                {

                    

                   if (!m_bisrunning){

                       m_threadhandle = reinterpret_cast<HANDLE>

                          (_beginthreadex(0, 0, threadfun, this, 0, &m_id));     

                       m_bisout = false;//we must used it in two thread for sync

             

                       while(!m_bisrunning && !m_bisout){ //blocking to waiting thread running

            #ifdef ___UTI_DEBUG

                          std::cout<<"###the thread is waiting for start! "<< std::endl;

            #endif

                          Sleep(2);

                       }

                       //Sleep(8);

                   }

                }

             

                inline bool IsRunning() const

                {

                   return m_bisrunning;

                }

             

                inline void join()

                {

                   if (m_threadhandle)

                       ::WaitForSingleObject(m_threadhandle, INFINITE);

                   ::CloseHandle(m_threadhandle);

                   m_threadhandle = 0;

                }

             

             

            private:

                std::string m_thread_name;

             

                boost::shared_ptr<ThreadFunbase> m_pfunptor;

             

                HANDLE m_threadhandle;

                unsigned int m_id;

                bool m_bisrunning;

                bool m_bisout;

             

                virtual void run()

                {

                   try{

                       m_bisrunning = true;

                       m_pfunptor->run();

                       m_bisout = true;

                       m_bisrunning = false;

                   }

                   catch(...)

                   {

                       throw std::logic_error("\r\n exception found in thread func! \r\n");

                   }

                }

             

                //now we are in another thread :-)

                static unsigned __stdcall threadfun(void *threadenti)

                {

                   thread * th = reinterpret_cast<thread*>(threadenti);

                   th->run();

                   return 0;

                }

             

                template<typename CT> CT *get_static_obj_ptr(CT **po = NULL)

                {

                   static std::vector<boost::shared_ptr<CT> > v_ct;

                   boost::shared_ptr<CT> sp_ct(new CT);

                   v_ct.push_back(sp_ct);

                   return sp_ct.get();

                }

            };

             

            class ThreadObjBase

            {

            public:

                enum ATACH_DETACH{ATTCH = 0, DETACH};

                ThreadObjBase(ATACH_DETACH a_or_d = ATTCH): m_bNeedStop(false), m_a_or_d(a_or_d)

                {}

                virtual ~ThreadObjBase()

                {

                   m_pth->join();

                }

                void Start()

                {

                   if (ATTCH == m_a_or_d)

                   {

                       m_pth = boost::shared_ptr<thread>(new thread(&ThreadObjBase::Thread_Start,this));

                       m_pth->start();

                   }

                   if (DETACH == m_a_or_d) 

                   {

                       m_pth = boost::shared_ptr<thread>(new thread(&ThreadObjBase::Thread_Start));

                       m_pth->start();

                      

                   }

                }

                inline void NotifyStop()

                {

                   m_bNeedStop = true;

                }

             

                void Join()

                {

                   m_pth->join();

                }

             

            public:   

                virtual void Run(){}

             

            private:

                void Thread_Start()

                {

                   Run();

                }

             

                boost::shared_ptr<thread> m_pth;

                bool m_bNeedStop;

                ATACH_DETACH m_a_or_d;

            };

             

            template <typename DT>

            class ThreadObjBase_T

            {

            public:

                enum ATACH_DETACH{ATTCH = 0, DETACH};

                ThreadObjBase_T(ATACH_DETACH a_or_d = ATTCH):m_bNeedStop(FALSE), m_a_or_d(a_or_d)

                {}

                virtual ~ThreadObjBase_T()

                {

                   m_pth->join();

                }

                void start()

                {

                   if (ATTCH == m_a_or_d){

                       m_pth = boost::shared_ptr<thread>(new thread(&DT::Run, static_cast<DT*>(this)));

                       m_pth->start();

                   }

                   if (DETACH == m_a_or_d){

                       m_pth = boost::shared_ptr<thread>(new thread(&DT::Run));

                       m_pth->start();

                   }

                }

                inline void notify_stop()

                {

                   m_bNeedStop = TRUE;

                }

                void join()

                {

                   m_pth->join();

                }

            private:

                boost::shared_ptr<thread> m_pth;

                bool m_bNeedStop;

                ATACH_DETACH m_a_or_d;

            };

             

             

            END_UTI

             

            #endif

             

            //===================================================================

            //example

            #include "thread.h"

            #include <iostream>

            #include <stdlib.h>

             

            bool g_needstop = false;

            int g_count = 0;

             

            void globle_func(int np)

            {

                std::cout << "------- globle_func start--------" <<std::endl;

                while(1)

            {

                   if (g_needstop)

                       break;

                   std::cout << ">>in globle_func countint:" <<g_count++ <<" param:" << np << std::endl;

                   Sleep(200);

                }

                std::cout << "------- globle_func exit--------" <<std::endl;

            }

             

            class uti_thread_test_cls

            {

            public:

                void mem_func()

                {

                   std::cout << "------- uti_thread_test_cls::mem_func start--------" <<std::endl;

                   while(1)

                   {

                       if (g_needstop)

                          break;

                       std::cout <<">>in uti_thread_test_cls::mem_func count:"<< g_count++ << std::endl;

                       Sleep(200);

                   }  

                   std::cout << "------- uti_thread_test_cls::mem_func exit--------" <<std::endl;

                }

            };

             

             

            //線程類的簡單使用。

            int main(int argc, char* argv[])

            {

                {

                thread thread_1(globle_func, 100);

                thread_1.start();

             

                uti_thread_test_cls cls_mem;

                thread thread_2(&uti_thread_test_cls::mem_func, &cls_mem);

                thread_2.start();

             

                ::system("pause\r\n");

                g_needstop = true;

                }

             

                ::system("pause\r\n");

             

                return 0;

            }

                  昨天我通過zoundry將此文發表在本BLOG之中, 后來我通過網頁才看到排版很糟糕(至今我還沒有發現完美的BLOG桌面軟件:-(,  如果大家有什么好的blog桌面軟件也希望可以介紹一下, 在這里先謝謝了 ^_^), 故今天稍作調整重新發表。



            posted on 2008-10-19 03:23 stlf 閱讀(170) 評論(0)  編輯 收藏 引用

            <2025年6月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            導航

            統計

            常用鏈接

            留言簿

            文章檔案

            搜索

            最新評論

            一本久久a久久精品综合香蕉| 久久久久久久波多野结衣高潮| 亚洲AV无码久久| 日韩AV无码久久一区二区| 亚洲中文字幕无码一久久区| 丁香色欲久久久久久综合网| 久久国产高清字幕中文| 色8激情欧美成人久久综合电| 久久人人添人人爽添人人片牛牛| 久久精品夜夜夜夜夜久久| 精品久久国产一区二区三区香蕉| 久久久久久久97| 久久精品无码一区二区三区日韩| 亚洲va中文字幕无码久久不卡| 精品久久久无码中文字幕| 亚洲国产精品高清久久久| 久久99精品免费一区二区| 人妻无码中文久久久久专区| 久久久久亚洲精品无码网址| 国内精品伊人久久久久av一坑 | 中文字幕久久波多野结衣av| 久久久久久综合一区中文字幕| 久久精品中文字幕一区| 久久国产成人精品麻豆| 久久婷婷成人综合色综合| 久久只有这里有精品4| 久久99精品久久久久久9蜜桃| 国产成人精品久久一区二区三区| 精品久久亚洲中文无码| 一本综合久久国产二区| 国内精品久久久久久麻豆| 青青草原综合久久| 久久精品男人影院| 久久99国产精一区二区三区| 久久久婷婷五月亚洲97号色 | 久久综合九色综合久99| 女人香蕉久久**毛片精品| 国产精品一区二区久久不卡| 亚洲狠狠婷婷综合久久久久| 久久亚洲AV成人出白浆无码国产| 久久国产色av免费看|