• <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++博客 :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

            thread wrapper

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

             最近做了一套thread wrapper, 可以以線程的形式運(yùn)行任意函數(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);        // 產(chǎn)生一個(gè)線程
            slaunch3(foo)(a, b, (int)c); // 產(chǎn)生一個(gè)線程
            }

            不知道這種發(fā)射多線程的方法會(huì)給大家?guī)?lái)方便嗎?請(qǐng)各位大俠給點(diǎn)意見(jiàn)。

            Feedback

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

            2010-06-08 12:19 by 天堂的隔壁
            有一些簡(jiǎn)單問(wèn)題復(fù)雜化
            跨平臺(tái)問(wèn)題平臺(tái)綁定的意思
            而且還有多線程問(wèn)題

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

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

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

            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);
            --------------------
            這樣就好用了,因?yàn)镃++編譯器會(huì)替你檢查錯(cuò)誤。譬如說(shuō)穿進(jìn)去的囧函數(shù)第二個(gè)參數(shù)是float,編譯器就會(huì)抱怨你參數(shù)"vczh"不對(duì)
            --------------------
            一般來(lái)說(shuō)你只需要支持0到10個(gè)參數(shù),還有返回void或者有返回值的函數(shù),特化22個(gè)就行了。

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

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

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

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


            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)建線程時(shí),其返回值可能是一個(gè)無(wú)效句柄(太快而被關(guān)閉了),千萬(wàn)不要對(duì)它作操
            // 作,如 WaitForSingleObject 等,具體見(jiàn)幫助
            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("啟動(dòng)線程[") + tstring(nm) + _T("]失敗"));
            else
            ThrowException1(RuntimeException, _T("啟動(dòng)線程失敗"));
            }

            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)建線程時(shí),其返回值可能是一個(gè)無(wú)效句柄(太快而被關(guān)閉了),千萬(wàn)不要對(duì)它作操
            // 作,如 WaitForSingleObject 等,具體見(jiàn)幫助
            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("啟動(dòng)線程[") + tstring(nm) + _T("]失敗"));
            else
            ThrowException1(RuntimeException, _T("啟動(dòng)線程失敗"));
            }

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

            2010-06-09 19:08 by djx_zh
            @梅發(fā)坤
            Cool, 這樣就清晰多了,還容易調(diào)試。

            只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            yy6080久久| 亚洲天堂久久久| 无码人妻久久一区二区三区蜜桃| 成人精品一区二区久久久| 国产午夜免费高清久久影院| 18禁黄久久久AAA片| 久久天天躁夜夜躁狠狠躁2022| 亚洲Av无码国产情品久久| 久久狠狠爱亚洲综合影院| 久久人人爽人人爽人人爽 | 久久精品18| 久久亚洲中文字幕精品一区四| 欧美久久天天综合香蕉伊| 一极黄色视频久久网站| 国产毛片欧美毛片久久久| 亚洲午夜久久久久久久久电影网| 久久天天躁狠狠躁夜夜躁2O2O| 久久久久久久久无码精品亚洲日韩| 久久99精品国产麻豆| 麻豆精品久久久一区二区| 久久国产精品二国产精品| 国产精品一区二区久久精品涩爱 | 色青青草原桃花久久综合| 久久久久久久久久久精品尤物| 午夜精品久久久久久99热| 久久免费小视频| 亚洲精品第一综合99久久 | 久久亚洲精品无码aⅴ大香| 久久精品99久久香蕉国产色戒| 国产成人香蕉久久久久 | 亚洲国产精品无码久久98| 久久国产精品一区二区| 久久精品无码一区二区WWW| 狠狠88综合久久久久综合网 | 国产一级做a爰片久久毛片| 久久久久97国产精华液好用吗| 亚洲人成网亚洲欧洲无码久久| 久久天堂电影网| 色综合久久中文字幕无码| 久久久久久国产精品美女| www.久久热.com|