• <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 閱讀(2317) 評(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ù)雜的東西放到頭文件里,用戶(hù)不用關(guān)心。 用戶(hù)只要調(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è)用戶(hù)登錄后才能發(fā)表評(píng)論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問(wèn)   Chat2DB   管理


            亚洲精品99久久久久中文字幕 | 久久无码中文字幕东京热| 99久久免费国产特黄| 国产精品久久成人影院| 91亚洲国产成人久久精品网址| 人人狠狠综合久久亚洲| 久久综合色老色| 99久久精品午夜一区二区| 欧美久久亚洲精品| 日日躁夜夜躁狠狠久久AV| 少妇久久久久久被弄到高潮| 久久天天躁狠狠躁夜夜avapp| 99久久精品免费观看国产| 亚洲国产精品无码久久九九| 久久精品无码午夜福利理论片| 一本色道久久88综合日韩精品| 亚洲AV无码一区东京热久久| 狠狠色丁香久久婷婷综合图片 | 久久国产福利免费| 国产精品久久精品| 2021国产精品久久精品| 色综合久久综精品| 久久99国产精品久久久 | 久久本道伊人久久| 久久久久久国产精品无码下载| 国产精品熟女福利久久AV| 国产99精品久久| 亚洲国产精品无码久久久不卡 | 久久国产高清字幕中文| 99久久国产综合精品女同图片| 狠狠色丁香婷婷久久综合| 狠狠色伊人久久精品综合网| 9久久9久久精品| 日韩乱码人妻无码中文字幕久久 | 国产精品久久久久影院色| 久久久噜噜噜久久中文字幕色伊伊| 久久精品成人| 久久国产三级无码一区二区| 久久香蕉国产线看观看99| 久久这里只有精品久久| 亚洲国产精品一区二区久久|