• <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++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            thread wrapper

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

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

            #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); // 產生一個線程
            }

            不知道這種發射多線程的方法會給大家帶來方便嗎?請各位大俠給點意見。

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

            # re: thread wrapper  回復  更多評論   

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

            # re: thread wrapper  回復  更多評論   

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


            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創建線程時,其返回值可能是一個無效句柄(太快而被關閉了),千萬不要對它作操
            // 作,如 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創建線程時,其返回值可能是一個無效句柄(太快而被關閉了),千萬不要對它作操
            // 作,如 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
            @梅發坤
            Cool, 這樣就清晰多了,還容易調試。
            亚洲av成人无码久久精品 | 久久狠狠一本精品综合网| 天天躁日日躁狠狠久久| 无码人妻精品一区二区三区久久| 久久无码人妻一区二区三区 | 久久久国产乱子伦精品作者| 狠狠色丁香婷婷久久综合不卡| 伊人久久综合热线大杳蕉下载| 久久无码AV中文出轨人妻| 香蕉久久夜色精品升级完成| 99久久综合狠狠综合久久| 77777亚洲午夜久久多喷| 91久久九九无码成人网站| 久久久国产视频| 天天爽天天爽天天片a久久网| 热久久最新网站获取| 久久w5ww成w人免费| 亚洲精品视频久久久| 亚洲乱亚洲乱淫久久| 精产国品久久一二三产区区别| 色综合久久久久| 99久久人妻无码精品系列蜜桃 | 国产精品免费福利久久| 久久久精品久久久久特色影视| 日本强好片久久久久久AAA| 香蕉aa三级久久毛片| 久久综合中文字幕| 色婷婷综合久久久久中文 | 欧美大香线蕉线伊人久久| 国内精品伊人久久久久影院对白 | 久久亚洲熟女cc98cm| 精品综合久久久久久88小说| 国产精品久久久久久久久免费| 国产99久久久国产精品小说| 精品久久人人做人人爽综合| 69国产成人综合久久精品| 久久丫精品国产亚洲av不卡| 久久综合狠狠综合久久| 人人狠狠综合久久88成人| 久久精品亚洲精品国产色婷| 无码人妻久久一区二区三区免费丨|