• <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>

            S.l.e!ep.¢%

            像打了激速一樣,以四倍的速度運(yùn)轉(zhuǎn),開心的工作
            簡(jiǎn)單、開放、平等的公司文化;尊重個(gè)性、自由與個(gè)人價(jià)值;
            posts - 1098, comments - 335, trackbacks - 0, articles - 1
              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            JetByteTools(二)

            Posted on 2009-09-20 16:53 S.l.e!ep.¢% 閱讀(1305) 評(píng)論(1)  編輯 收藏 引用 所屬分類: IOCP

            IOCP 跟線程池脫不了關(guān)系

            以前寫過一個(gè)線程類
            http://www.shnenglu.com/sleepwom/archive/2009/02/07/73161.html

            JetByteTools里的線程類是這樣的,作了一些小改動(dòng)

            今天才知道,原來C++的虛函數(shù)可以寫成 private: ......

            ??? private?:

            ??????
            virtual?unsigned?int?Run()?=?0

            #if ?defined?(_MSC_VER)?&&?(_MSC_VER?>=?1020)
            #pragma?once
            #endif

            #ifndef?JETBYTE_TOOLS_WIN32_THREAD_INCLUDED__
            #define ?JETBYTE_TOOLS_WIN32_THREAD_INCLUDED__
            ////////////////////////////////////////////////////////////////////////////// /
            //
            // ?File???????????:?$Workfile:?Thread.h?$
            // ?Version????????:?$Revision:?3?$
            // ?Function???????:?
            //
            // ?Author?????????:?$Author:?Len?$
            // ?Date???????????:?$Date:?20/05/02?23:17?$
            //
            // ?Notes??????????:?
            //
            // ?Modifications??:
            //
            // ?$Log:?/Clients/PayPoint/e-Voucher/JetByteTools/Win32Tools/Thread.h?$
            // ?
            // ?3?????20/05/02?23:17?Len
            // ?Updated?copyright?and?disclaimers.
            // ?
            // ?2?????10/05/02?19:25?Len
            // ?Lint?options?and?code?cleaning.
            // ?
            // ?1?????9/05/02?18:47?Len
            // ?
            ////////////////////////////////////////////////////////////////////////////// /
            //
            // ?Copyright?1997?-?2002?JetByte?Limited.
            //
            // ?JetByte?Limited?grants?you?("Licensee")?a?non-exclusive,?royalty?free,?
            // ?licence?to?use,?modify?and?redistribute?this?software?in?source?and?binary?
            // ?code?form,?provided?that?i)?this?copyright?notice?and?licence?appear?on?all?
            // ?copies?of?the?software;?and?ii)?Licensee?does?not?utilize?the?software?in?a?
            // ?manner?which?is?disparaging?to?JetByte?Limited.
            //
            // ?This?software?is?provided?"as?is"?without?a?warranty?of?any?kind.?All?
            // ?express?or?implied?conditions,?representations?and?warranties,?including
            // ?any?implied?warranty?of?merchantability,?fitness?for?a?particular?purpose
            // ?or?non-infringement,?are?hereby?excluded.?JetByte?Limited?and?its?licensors?
            // ?shall?not?be?liable?for?any?damages?suffered?by?licensee?as?a?result?of?
            // ?using,?modifying?or?distributing?the?software?or?its?derivatives.?In?no
            // ?event?will?JetByte?Limited?be?liable?for?any?lost?revenue,?profit?or?data,
            // ?or?for?direct,?indirect,?special,?consequential,?incidental?or?punitive
            // ?damages,?however?caused?and?regardless?of?the?theory?of?liability,?arising?
            // ?out?of?the?use?of?or?inability?to?use?software,?even?if?JetByte?Limited?
            // ?has?been?advised?of?the?possibility?of?such?damages.
            //
            // ?This?software?is?not?designed?or?intended?for?use?in?on-line?control?of?
            // ?aircraft,?air?traffic,?aircraft?navigation?or?aircraft?communications;?or?in?
            // ?the?design,?construction,?operation?or?maintenance?of?any?nuclear?
            // ?facility.?Licensee?represents?and?warrants?that?it?will?not?use?or?
            // ?redistribute?the?Software?for?such?purposes.?
            //
            ////////////////////////////////////////////////////////////////////////////// /

            ////////////////////////////////////////////////////////////////////////////// /

            // ?Lint?options
            //
            // lint?-save
            // lint?-esym(1704,?CThread::CThread)???private?(copy)?constructor?
            //
            ////////////////////////////////////////////////////////////////////////////// /

            #ifndef?_WINDOWS_
            #define ?WIN32_LEAN_AND_MEAN
            #include?
            < windows.h >
            #undef ?WIN32_LEAN_AND_MEAN
            #endif



            ////////////////////////////////////////////////////////////////////////////// /
            // ?Namespace:?JetByteTools::Win32
            ////////////////////////////////////////////////////////////////////////////// /

            namespace ?JetByteTools? {
            namespace ?Win32? {

            ////////////////////////////////////////////////////////////////////////////// /
            // ?CThread
            ////////////////////////////////////////////////////////////////////////////// /

            class ?CThread?
            {
            ???
            public ?:
            ???
            ??????CThread();
            ??????
            ??????
            virtual ? ~ CThread();

            ??????HANDLE?GetHandle();

            ??????
            void ?Wait()? const ;

            ??????
            bool ?Wait(DWORD?timeoutMillis)? const ;

            ??????
            void ?Start();

            ??????
            void ?Terminate(DWORD?exitCode? = ? 0 );

            ???
            private ?:

            ??????
            virtual ?unsigned? int ?Run()? = ? 0 ;

            ??????
            static ?unsigned? int ?__stdcall?ThreadFunction( void ? * pV);

            ??????HANDLE?m_hThread;

            ??????
            // ?No?copies?do?not?implement
            ??????CThread( const ?CThread? & rhs);
            ??????CThread?
            & operator = ( const ?CThread? & rhs);
            }
            ;

            ////////////////////////////////////////////////////////////////////////////// /
            // ?Namespace:?JetByteTools::Win32
            ////////////////////////////////////////////////////////////////////////////// /

            }
            ? // ?End?of?namespace?Win32
            }
            ? // ?End?of?namespace?JetByteTools?

            ////////////////////////////////////////////////////////////////////////////// /
            // ?Lint?options
            //
            // lint?-restore
            //
            ////////////////////////////////////////////////////////////////////////////// /

            #endif ? // ?JETBYTE_TOOLS_WIN32_THREAD_INCLUDED__

            ////////////////////////////////////////////////////////////////////////////// /
            // ?End?of?file
            ////////////////////////////////////////////////////////////////////////////// /


            ////////////////////////////////////////////////////////////////////////////// /
            //
            // ?File???????????:?$Workfile:?Thread.cpp?$
            // ?Version????????:?$Revision:?3?$
            // ?Function???????:?
            //
            // ?Author?????????:?$Author:?Len?$
            // ?Date???????????:?$Date:?20/05/02?23:17?$
            //
            // ?Notes??????????:?
            //
            // ?Modifications??:
            //
            // ?$Log:?/Clients/PayPoint/e-Voucher/JetByteTools/Win32Tools/Thread.cpp?$
            // ?
            // ?3?????20/05/02?23:17?Len
            // ?Updated?copyright?and?disclaimers.
            // ?
            // ?2?????10/05/02?19:25?Len
            // ?Lint?options?and?code?cleaning.
            // ?
            // ?1?????9/05/02?18:47?Len
            // ?
            ////////////////////////////////////////////////////////////////////////////// /
            //
            // ?Copyright?1997?-?2002?JetByte?Limited.
            //
            // ?JetByte?Limited?grants?you?("Licensee")?a?non-exclusive,?royalty?free,?
            // ?licence?to?use,?modify?and?redistribute?this?software?in?source?and?binary?
            // ?code?form,?provided?that?i)?this?copyright?notice?and?licence?appear?on?all?
            // ?copies?of?the?software;?and?ii)?Licensee?does?not?utilize?the?software?in?a?
            // ?manner?which?is?disparaging?to?JetByte?Limited.
            //
            // ?This?software?is?provided?"as?is"?without?a?warranty?of?any?kind.?All?
            // ?express?or?implied?conditions,?representations?and?warranties,?including
            // ?any?implied?warranty?of?merchantability,?fitness?for?a?particular?purpose
            // ?or?non-infringement,?are?hereby?excluded.?JetByte?Limited?and?its?licensors?
            // ?shall?not?be?liable?for?any?damages?suffered?by?licensee?as?a?result?of?
            // ?using,?modifying?or?distributing?the?software?or?its?derivatives.?In?no
            // ?event?will?JetByte?Limited?be?liable?for?any?lost?revenue,?profit?or?data,
            // ?or?for?direct,?indirect,?special,?consequential,?incidental?or?punitive
            // ?damages,?however?caused?and?regardless?of?the?theory?of?liability,?arising?
            // ?out?of?the?use?of?or?inability?to?use?software,?even?if?JetByte?Limited?
            // ?has?been?advised?of?the?possibility?of?such?damages.
            //
            // ?This?software?is?not?designed?or?intended?for?use?in?on-line?control?of?
            // ?aircraft,?air?traffic,?aircraft?navigation?or?aircraft?communications;?or?in?
            // ?the?design,?construction,?operation?or?maintenance?of?any?nuclear?
            // ?facility.?Licensee?represents?and?warrants?that?it?will?not?use?or?
            // ?redistribute?the?Software?for?such?purposes.?
            //
            ////////////////////////////////////////////////////////////////////////////// /

            #include?
            " Thread.h "

            #include?
            < process.h >

            #include?
            " Win32Exception.h "
            #include?
            " Utils.h "

            ////////////////////////////////////////////////////////////////////////////// /
            // ?Lint?options
            //
            // lint?-save
            // lint?-esym(1763,?CThread::GetHandle)?const?member?indirectly?modifies?obj
            //
            // ?Member?not?defined
            // lint?-esym(1526,?CThread::CThread)
            // lint?-esym(1526,?CThread::operator=)
            //
            ////////////////////////////////////////////////////////////////////////////// /

            ////////////////////////////////////////////////////////////////////////////// /

            // ?Namespace:?JetByteTools::Win32
            ////////////////////////////////////////////////////////////////////////////// /

            namespace ?JetByteTools? {
            namespace ?Win32? {

            ////////////////////////////////////////////////////////////////////////////// /
            // ?CThread
            ////////////////////////////////////////////////////////////////////////////// /

            CThread::CThread()
            ???:??m_hThread(INVALID_HANDLE_VALUE)
            {

            }

            ??????
            CThread::
            ~ CThread()
            {
            ???
            if ?(m_hThread? != ?INVALID_HANDLE_VALUE)
            ???
            {
            ??????(
            void )?::CloseHandle(m_hThread);
            ???}


            ???m_hThread?
            = ?NULL;

            ???
            // lint?-e{1540}?m_hThread?neither?freed?nor?zeroed?in?destructor
            }


            HANDLE?CThread::GetHandle()
            {
            ???
            return ?m_hThread;
            }


            void ?CThread::Start()
            {
            ???
            if ?(m_hThread? == ?INVALID_HANDLE_VALUE)
            ???
            {
            ??????unsigned?
            int ?threadID? = ? 0 ;

            ??????m_hThread?
            = ?(HANDLE)::_beginthreadex( 0 ,? 0 ,?ThreadFunction,?( void * ) this ,? 0 ,? & threadID);

            ??????
            if ?(m_hThread? == ?INVALID_HANDLE_VALUE)
            ??????
            {
            ?????????
            throw ?CWin32Exception(_T( " CThread::Start()?-?_beginthreadex " ),?GetLastError());
            ??????}

            ???}

            ???
            else
            ???
            {
            ??????
            throw ?CException(_T( " CThread::Start() " ),?_T( " Thread?already?running?-?you?can?only?call?Start()?once! " ));
            ???}

            }


            void ?CThread::Wait()? const
            {
            ???
            if ?( ! Wait(INFINITE))
            ???
            {
            ??????
            throw ?CException(_T( " CThread::Wait() " ),?_T( " Unexpected?timeout?on?infinite?wait " ));
            ???}

            }


            bool ?CThread::Wait(DWORD?timeoutMillis)? const
            {
            ???
            // ?TODO?base?class??Waitable?
            ??? bool ?ok;

            ???DWORD?result?
            = ?::WaitForSingleObject(m_hThread,?timeoutMillis);

            ???
            if ?(result? == ?WAIT_TIMEOUT)
            ???
            {
            ??????ok?
            = ? false ;
            ???}

            ???
            else ? if ?(result? == ?WAIT_OBJECT_0)
            ???
            {
            ??????ok?
            = ? true ;
            ???}

            ???
            else
            ???
            {
            ??????
            throw ?CWin32Exception(_T( " CThread::Wait()?-?WaitForSingleObject " ),?::GetLastError());
            ???}

            ????
            ???
            return ?ok;
            }


            unsigned?
            int ?__stdcall?CThread::ThreadFunction( void ? * pV)
            {
            ???unsigned?
            int ?result? = ? 0 ;

            ???CThread
            * ?pThis? = ?(CThread * )pV;
            ???
            ???
            if ?(pThis)
            ???
            {
            ??????
            try
            ??????
            {
            ?????????result?
            = ?pThis -> Run();
            ??????}

            ??????
            catch ()
            ??????
            {
            ??????????DEBUG_ONLY(Output(_T(
            " CThread::ThreadFunction()?-?Unknown?Exception " ))?);
            ??????}

            ???}


            ???
            return ?result;
            }


            void ?CThread::Terminate(
            ???DWORD?exitCode?
            /* ?=?0? */ )
            {
            ???
            if ?( ! ::TerminateThread(m_hThread,?exitCode))
            ???
            {
            ??????
            // ?TODO?we?could?throw?an?exception?here
            ???}

            }


            ////////////////////////////////////////////////////////////////////////////// /
            // ?Namespace:?JetByteTools::Win32
            ////////////////////////////////////////////////////////////////////////////// /

            }
            ? // ?End?of?namespace?Win32
            }
            ? // ?End?of?namespace?JetByteTools?

            ////////////////////////////////////////////////////////////////////////////// /
            // ?Lint?options
            //
            // lint?-restore
            //
            ////////////////////////////////////////////////////////////////////////////// /

            ////////////////////////////////////////////////////////////////////////////// /

            // ?End?of?file
            ////////////////////////////////////////////////////////////////////////////// /


            使用也很簡(jiǎn)單
            #include?<stdio.h>
            #include?
            "Thread.h"

            class?CThreadTest?:?public?JetByteTools::Win32::CThread
            {
            public:

            private:
            ????
            virtual?unsigned?int?Run()
            ????
            {
            ????????printf(
            "Run?Thread\n");
            ????????
            ????????
            while(true)
            ????????
            {
            ????????}


            ????????
            return?0;
            ????}

            }
            ;

            int?main()
            {
            ????CThreadTest?test;
            ????test.Start();
            ????
            ????(
            void)test.Wait(1000);

            ????test.Terminate();

            ????printf(
            "success\n");
            ????
            return?0;
            }


            調(diào)用 start() 線程就開始,退出程序時(shí),如果允許就 wait(), 如果很急,就直接 Terminate(),

            Feedback

            # re: JetByteTools(二)   回復(fù)  更多評(píng)論   

            2009-09-21 15:18 by 一豬
            JetByteTools代碼可以提供一份嗎?寫了,好了好久沒找到免費(fèi)下載的。多謝了!
            郵箱: zhlong.1988@163.com
            久久亚洲中文字幕精品一区四| 久久人妻AV中文字幕| 热久久视久久精品18| 少妇久久久久久被弄高潮| 亚洲精品高清久久| 久久91精品国产91久| 97精品久久天干天天天按摩| 久久久久这里只有精品| 色婷婷综合久久久久中文一区二区 | 99久久精品国产高清一区二区 | 久久精品中文无码资源站| 99国产欧美久久久精品蜜芽 | 区久久AAA片69亚洲| 久久久久久免费一区二区三区| 久久精品国产男包| 91久久成人免费| 久久久久人妻一区精品色| 久久久久亚洲AV综合波多野结衣 | 伊人久久大香线蕉成人| 欧美777精品久久久久网| 一本一本久久A久久综合精品| 国内精品久久久久久久久| 久久精品欧美日韩精品| 欧美日韩精品久久久久 | 久久久久综合网久久| 亚洲欧美日韩久久精品第一区| 久久国产精品免费一区| 国产美女久久久| 无码国内精品久久人妻蜜桃| 亚洲国产精品综合久久网络| 精品久久久无码中文字幕天天| 99久久99这里只有免费的精品| 久久精品国产男包| 亚洲国产天堂久久综合| 国产亚洲精午夜久久久久久| 精品久久久久香蕉网| 午夜精品久久久久久毛片| 伊人久久大香线蕉综合网站| 久久久久久一区国产精品| 久久综合久久久| 久久99国产精品99久久|