• <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 閱讀(2313) 評論(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ā)射多線程的方法會給大家?guī)矸奖銌幔空埜魑淮髠b給點(diǎn)意見。

            Feedback

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

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

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

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

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

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

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

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

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

            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è)無效句柄(太快而被關(guān)閉了),千萬不要對它作操
            // 作,如 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)建線程時(shí),其返回值可能是一個(gè)無效句柄(太快而被關(guān)閉了),千萬不要對它作操
            // 作,如 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  回復(fù)  更多評論   

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

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


            激情五月综合综合久久69| 亚洲一级Av无码毛片久久精品| 狠狠干狠狠久久| 婷婷久久综合九色综合98| 国产精品久久毛片完整版| 日韩乱码人妻无码中文字幕久久| 人妻系列无码专区久久五月天| 亚洲欧洲久久久精品| 国产69精品久久久久9999APGF| 欧美一区二区三区久久综| 国产精品gz久久久| 很黄很污的网站久久mimi色| 亚洲国产小视频精品久久久三级 | 九九99精品久久久久久| 久久精品亚洲精品国产欧美| 久久91综合国产91久久精品| 久久本道伊人久久| 精品国产91久久久久久久| 精品人妻久久久久久888| 青青久久精品国产免费看| 99久久成人18免费网站| 亚洲精品视频久久久| 欧洲性大片xxxxx久久久| 久久精品国产亚洲av高清漫画| 亚洲国产成人精品女人久久久 | 亚洲?V乱码久久精品蜜桃 | 亚洲AV日韩精品久久久久| 97精品伊人久久大香线蕉| 日韩欧美亚洲综合久久影院d3| 久久免费的精品国产V∧| 久久国产视频网| 欧美午夜精品久久久久久浪潮| 久久99国产精品99久久| 久久精品国产亚洲av麻豆小说| 久久99国产乱子伦精品免费| 亚洲人AV永久一区二区三区久久 | 99999久久久久久亚洲| 国产69精品久久久久久人妻精品 | 国产成人无码久久久精品一| 国产成人精品久久| 日韩乱码人妻无码中文字幕久久 |