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

            string

            string
            posts - 27, comments - 177, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            thread wrapper

            Posted on 2010-06-08 09:43 djx_zh 閱讀(2317) 評論(6)  編輯 收藏 引用

             最近做了一套thread wrapper, 可以以線程的形式運行任意函數(shù), 例子如下:

            #ifdef  _PTHREAD
            #include <pthread.h>
            void* thread_func_g(void*p){
             funcInfo_t* ft = (funcInfo_t*)p;
             __asm__(
             "subl %%ecx, %%esp\n"
             "movl %%esp, %%edi\n"
             "rep movsb\n"
             "call *%%eax\n"
             :
             :"a"(ft->f),"c"(ft->argSize), "S"(ft->esp)
             :"%edi");
            }
            #else
            #endif

            typedef struct{
             void* f;
             int argSize;
             char esp[128];
            }funcInfo_t;

            funcInfo_t global_funcInfo;
            #define launchTemplateThread() \
            create_thread(thread_func_g, &global_funcInfo);// /*thread_func_g(&global_funcInfo);*/

            ///////////////////// 3 args ////////////////////////////////////////////
            #define slaunchArg3( a00, a01, a02) {char* pcur= global_funcInfo.esp; \
             (*(__typeof__(a00)*)pcur)=a00; pcur += _INTSIZEOF(__typeof__(a00));\
             (*(__typeof__(a01)*)pcur)=a01; pcur += _INTSIZEOF(__typeof__(a01));\
             (*(__typeof__(a02)*)pcur)=a02; pcur += _INTSIZEOF(__typeof__(a02));\
             global_funcInfo.argSize = pcur - global_funcInfo.esp;\
            }\
            launchTemplateThread();

            #define slaunch3(sfunc) global_funcInfo.f = (void*) sfunc; slaunchArg3

            #define kernel_ret  void* __attribute__((stdcall))

            kernel_ret foo(int a, int b, int c){
            printf("%d %d %d\n", a, b,c);
            return 0;


            int main(){
            int a, b, c;
            char cc;
            slaunch3(foo)(a, b, c);        // 產生一個線程
            slaunch3(foo)(a, b, (int)c); // 產生一個線程
            }

            不知道這種發(fā)射多線程的方法會給大家?guī)矸奖銌幔空埜魑淮髠b給點意見。

            Feedback

            # re: thread wrapper  回復  更多評論   

            2010-06-08 12:19 by 天堂的隔壁
            有一些簡單問題復雜化
            跨平臺問題平臺綁定的意思
            而且還有多線程問題

            # re: thread wrapper  回復  更多評論   

            2010-06-08 13:59 by djx_zh
            @天堂的隔壁
            多謝賜教。 是搞的挺復雜的。但可以把這套復雜的東西放到頭文件里,用戶不用關心。 用戶只要調用slaunchX就可以了, 不過這種簡單的線程好像不大實用。

            # re: thread wrapper  回復  更多評論   

            2010-06-08 15:28 by 陳梓瀚(vczh)
            @djx_zh

            int 囧(int a, string b, void* c)
            {
            throw "囧";
            }

            class 囧Getter
            {
            public:
            void Get囧(int result);
            }

            --------------------
            囧Getter 囧getter;
            RunAsync(囧, Func<void(int)>(&囧getter, &囧Getter::Get囧), 1, "vczh", 0);
            RunAsync(囧, 1, "vczh", 0);
            --------------------
            這樣就好用了,因為C++編譯器會替你檢查錯誤。譬如說穿進去的囧函數(shù)第二個參數(shù)是float,編譯器就會抱怨你參數(shù)"vczh"不對
            --------------------
            一般來說你只需要支持0到10個參數(shù),還有返回void或者有返回值的函數(shù),特化22個就行了。

            # re: thread wrapper  回復  更多評論   

            2010-06-08 15:35 by djx_zh
            @陳梓瀚(vczh)
            對,模板比宏好用。

            # re: thread wrapper  回復  更多評論   

            2010-06-09 17:57 by 梅發(fā)坤
            呵呵,用到了匯編,看的頭大,我模板做了一個如何


            template< typename R, typename T>
            R _cast(T t)
            {
            return t;
            }

            template< typename R>
            R _cast(std::tr1::shared_ptr< typename std::tr1::remove_pointer<R>::type > t)
            {
            return t.get();
            }

            template<typename F>
            class thread_closure_0
            {
            public:

            thread_closure_0(const F& f, const tchar* nm = 0)
            : _function(f)
            , _name((0 == nm) ? _T("") : nm)
            {
            }

            static void start_routine(void* c)
            {
            thread_closure_0 *self = static_cast<thread_closure_0*>(c);
            try
            {
            self->_function();
            }
            catch (...)
            {
            delete self;
            throw;
            }

            delete self;
            }

            const tstring& name() const
            {
            return _name;
            }
            private:
            F _function;
            tstring _name;
            };

            template<typename F, typename P>
            class thread_closure_1
            {
            public:

            thread_closure_1(const F& f, const P& x, const tchar* nm = 0)
            : _function(f)
            , _name((0 == nm) ? _T("") : nm)
            , arg1(x)
            {
            }

            static void start_routine(void* c)
            {
            thread_closure_1 *self = static_cast<thread_closure_1*>(c);
            try
            {
            self->_function(self->arg1);
            }
            catch (...)
            {
            delete self;
            throw;
            }
            delete self;
            }

            const tstring& name() const
            {
            return _name;
            }
            private:
            F _function;
            tstring _name;
            P arg1;
            };

            template<typename F>
            inline void create_thread(const F& f, const tchar* nm = null_ptr)
            {
            typedef thread_closure_0<F> closure_type;

            // _beginthread創(chuàng)建線程時,其返回值可能是一個無效句柄(太快而被關閉了),千萬不要對它作操
            // 作,如 WaitForSingleObject 等,具體見幫助
            uintptr_t handle = ::_beginthread(closure_type::start_routine, 0, new closure_type(f, nm));
            if (-1L != handle)
            return;

            if (null_ptr != nm)
            ThrowException1(RuntimeException, _T("啟動線程[") + tstring(nm) + _T("]失敗"));
            else
            ThrowException1(RuntimeException, _T("啟動線程失敗"));
            }

            template<typename F, typename P>
            inline void create_thread(const F& f, const P& x, const tchar* nm = null_ptr)
            {
            typedef thread_closure_1<F, P> closure_type;

            // _beginthread創(chuàng)建線程時,其返回值可能是一個無效句柄(太快而被關閉了),千萬不要對它作操
            // 作,如 WaitForSingleObject 等,具體見幫助
            uintptr_t handle = ::_beginthread(closure_type::start_routine, 0, new closure_type(f, x, nm));
            if (-1L != handle)
            return;

            if (null_ptr != nm)
            ThrowException1(RuntimeException, _T("啟動線程[") + tstring(nm) + _T("]失敗"));
            else
            ThrowException1(RuntimeException, _T("啟動線程失敗"));
            }

            # re: thread wrapper  回復  更多評論   

            2010-06-09 19:08 by djx_zh
            @梅發(fā)坤
            Cool, 這樣就清晰多了,還容易調試。
            久久er国产精品免费观看8| 无码人妻精品一区二区三区久久久| 久久99久久99精品免视看动漫| 五月丁香综合激情六月久久 | www亚洲欲色成人久久精品| 免费一级欧美大片久久网 | 久久亚洲精品中文字幕| 久久久WWW成人免费精品| 久久免费看黄a级毛片| 蜜臀久久99精品久久久久久| 2020久久精品亚洲热综合一本| 久久综合综合久久97色| 午夜不卡久久精品无码免费 | 亚洲伊人久久综合中文成人网| 久久国产精品久久精品国产| 久久亚洲精精品中文字幕| 狠狠色丁香婷婷综合久久来来去| 婷婷久久综合九色综合九七| 久久免费视频一区| 国产精品一久久香蕉国产线看 | 久久久久亚洲av无码专区导航| 九九精品久久久久久噜噜| 久久这里只精品国产99热| 久久精品无码一区二区WWW| 国内精品久久久久久久久| 欧美喷潮久久久XXXXx| 亚洲国产成人久久精品99| 国产精品无码久久久久| 91精品国产乱码久久久久久| 国产精品99久久久精品无码| 久久久久亚洲av成人网人人软件| 2020最新久久久视精品爱 | 2021国内久久精品| 色婷婷综合久久久久中文字幕 | 精品国产VA久久久久久久冰 | 四虎影视久久久免费| 久久国产精品二国产精品| 九九久久精品国产| 国产精品欧美久久久久无广告 | 99久久精品免费| 中文字幕成人精品久久不卡 |