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

            八葉草

            學習資料記錄

            pthreads-w32

            http://shenan1984.blog.163.com/blog/static/2530851020098231001787/
              official site: http://sourceware.org/pthreads-win32/
              source code: ftp://sourceware.org/pub/pthreads-win32/pthreads-w32-2-8-0-release.tar.gz

            1. 編譯:
                雖然源碼包里提供了vc6的項目文件, 但是打不開的, 只能用nmake. 默認的會告訴你一堆nmake參數的.
                我所要用的是編譯成static的library, 所以輸入"nmake clean VC-static", 編譯很快的. 不過默認會鏈接到VC的crt, 我們需要修改它的makefile. 找到CFLAGS那一行, 把"/MD"改成"/MT".

            2. 項目:
                誒.. 有好多地方要改的.
                a) 當然是vs路徑的include啊, lib啊.. 自己加.
                b) 項目的crt設置成"/MT"和"/MTd". 額外的lib加: pthreadVC2(d).lib ws2_32.lib
                c) preprocesser定義的地方, 加一個“PTW32_STATIC_LIB”宏, 不然link的時候會找不到symbol的.
                d) 好了, 你可以coding了, 隨便pthread_create()一把吧.

            3. 編碼:
                嗯嗯.. 如果真的直接pthread_create()的話可是會access violation的呀. win32下的線程很詭異的, 像winsock一樣, 調用任何其它函數之前必須調用pthread_win32_process_attach_np(), 結束后必須調用pthread_win32_process_detach_np(). 代碼大概就是這樣的:

            1. 下載pthreads win32源代碼:
                ftp://sourceware.org/pub/pthreads-win32/pthreads-w32-2-8-0-release.tar.gz
            2. 編譯靜態庫:
            make clean GC-static
            在根目錄下面生成libpthreadGC2.a
            3. 將生成的libpthreadGC2.a拷貝到mingw庫目錄下,將pthread.h, sched.h, semaphore.h拷貝到INCLUDE目錄下
            4. 使用libpthread庫,
            在程序起始處對libpthread作初始化:
            #if defined(PTW32_STATIC_LIB)
                ptw32_processInitialize();
            #endif
            5. 編譯時確保傳入-DPTW32_STATIC_LIB,鏈接時加入-lpthreadGC2, OK!


            http://hi.baidu.com/lff0305/blog/item/2a55e7366ebba6360b55a942.html
            pthread2提供了不同的.lib以及相對應的.dll,

            pthread[VG]{SE,CE,C}c.dll
            pthread[VG]{SE,CE,C}c.lib

             

            含義為
            [VG] 編譯器種類
            V    - MS VC, or
            G    - GNU C

             

            {SE,CE,C} 異常處理模式
            SE    - Structured EH, or
            CE    - C++ EH, or
            C    - no exceptions - uses setjmp/longjmp

            c    - DLL compatibility number indicating ABI and API
            compatibility with applications built against
            any snapshot with the same compatibility number.
            See 'Version numbering' below.

               比如上面用的pthreadvc2.dll, 含義為:

               v = MSVC

               c = 沒有使用異常機制, 而是使用setjump/longjmp

               2 = 兼用性 - 和pthread2兼容, 不和舊版本pthread1兼容.

             

               詳細請參照pthread的readme.


            http://blog.csdn.net/psusong/archive/2010/01/14/5189659.aspx

            pthread 靜態編譯版本在Windows下使用時的注意事項
            作為通用的跨平臺高性能線程庫,在很多跨平臺的項目中都可以看見pthread的身影。pthread本身的實現比較優雅,APIs使用起來也很方便。

            但在Windows下使用靜態編譯的pthread時要特別注意一下,必須顯式的調用如下四個函數,否則pthread用到的一些全局變量會沒有被初始化,導致所有的pthread的APIs調用都crash.

            BOOL pthread_win32_process_attach_np (void);

            BOOL pthread_win32_process_detach_np (void);

            BOOL pthread_win32_thread_attach_np (void);

            BOOL pthread_win32_thread_detach_np (void);

            pthread官方文檔對此有如下的明確說明:

            These functions contain the code normally run via dllMain

            when the library is used as a dll but which need to be

            called explicitly by an application when the library

            is statically linked.

            You will need to call pthread_win32_process_attach_np() before

            you can call any pthread routines when statically linking.

            You should call pthread_win32_process_detach_np() before

            exiting your application to clean up.

            pthread_win32_thread_attach_np() is currently a no-op, but

            pthread_win32_thread_detach_np() is needed to clean up

            the implicit pthread handle that is allocated to a Win32 thread if

            it calls certain pthreads routines. Call this routine when the

            Win32 thread exits.

            These functions invariably return TRUE except for

            pthread_win32_process_attach_np() which will return FALSE

            if pthreads-win32 initialisation fails.

            通過函數的名字我們不難猜測出如下調用順序

            在程序開始的時候要調用:

            BOOL pthread_win32_process_attach_np (void);

            BOOL pthread_win32_thread_attach_np (void);

            在程序退出時要調用:

            BOOL pthread_win32_thread_detach_np (void);

            BOOL pthread_win32_process_detach_np (void);

            比較通用的做法是在模塊Load和UnLoad的時候做這個attach和detach操作,如下面所示:

            /* Callback for our DLL so we can initialize pthread */

            BOOL WINAPI DllMain( HANDLE hinstDLL, DWORD fdwReason, LPVOID lpvReserved )

            {

            #ifdef PTW32_STATIC_LIB

                switch( fdwReason )

                {

                    case DLL_PROCESS_ATTACH:

                        pthread_win32_process_attach_np();

                    case DLL_THREAD_ATTACH:

                        pthread_win32_thread_attach_np();

                        break;

                    case DLL_THREAD_DETACH:

                        pthread_win32_thread_detach_np();

                        break;

                    case DLL_PROCESS_DETACH:

                        pthread_win32_thread_detach_np();

                        pthread_win32_process_detach_np();

                        break;

                }

            #endif

                return TRUE;

            }

            注意: PTW32_STATIC_LIB 宏為pthread靜態編譯的標志,這個可以通過pthread.h的配置或者CFLAGS傳遞進來。
            下面是pthread的官方的dll.c的實現
            BOOL WINAPI
            DllMain (HINSTANCE hinstDll, DWORD fdwReason, LPVOID lpvReserved)
            {
              BOOL result = PTW32_TRUE;
              switch (fdwReason)
                {
                case DLL_PROCESS_ATTACH:
                  result = pthread_win32_process_attach_np ();
                  break;
                case DLL_THREAD_ATTACH:
                  /*
                   * A thread is being created
                   */
                  result = pthread_win32_thread_attach_np ();
                  break;
                case DLL_THREAD_DETACH:
                  /*
                   * A thread is exiting cleanly
                   */
                  result = pthread_win32_thread_detach_np ();
                  break;
                case DLL_PROCESS_DETACH:
                  (void) pthread_win32_thread_detach_np ();
                  result = pthread_win32_process_detach_np ();
                  break;
                }
              return (result);
            } /* DllMain */
            也就是說pthread官方代碼在動態編譯的版本中主動做了這個attach和detach操作。
            而靜態編譯版本由于沒有一個合適的地方來做這件事,就將attach和detach的的操作扔給用戶來完成了。
            上面的代碼是針對調用方是Dll的情況做的初始化,如果調用方不是Dll呢?對此可以參照如下做法,雖然很暴力,但很簡單,可以工作
            1)定義如下函數
            #ifdef PTW32_STATIC_LIB
            static void detach_ptw32(void)
            {
                pthread_win32_thread_detach_np();
                pthread_win32_process_detach_np();
            }
            #endif
            2)在你的主程序的入口處,一般而言是main()中做如下調用即可
            #ifdef PTW32_STATIC_LIB
                pthread_win32_process_attach_np();
                pthread_win32_thread_attach_np();
                atexit(detach_ptw32);
            #endif
             
            也就是用atexit()將detach工作掛接到程序中去,使得程序在退出的時候可以對pthread進行detach.

            posted on 2010-11-15 15:41 八葉草 閱讀(2261) 評論(1)  編輯 收藏 引用 所屬分類: pthreads-w32

            評論

            # re: pthreads-w32 2012-09-05 00:41 方斌

            很好呀,
            BOOL pthread_win32_thread_attach_np (void);
            BOOL pthread_win32_thread_detach_np (void);

            寫了第一個pthread_win32的創建線程,進程core了,搞了好久,
            后來我在一個開源軟件中,類似的做法,才得知這個規則。你總結的
            很好,向你學習~  回復  更多評論   

            国产亚州精品女人久久久久久 | 天天综合久久久网| 99久久精品费精品国产一区二区 | 久久久久久精品久久久久| 久久免费的精品国产V∧| 99久久精品免费看国产一区二区三区| 久久国产成人午夜AV影院| 精品久久人人爽天天玩人人妻 | 久久久久国产视频电影| 亚洲欧美成人综合久久久| 色综合久久最新中文字幕| 伊人久久亚洲综合影院| 精品久久久久久久久午夜福利| 精品久久人人做人人爽综合| 性做久久久久久久| 欧美午夜A∨大片久久 | 久久高潮一级毛片免费| 久久久久亚洲Av无码专| 久久久久亚洲精品天堂久久久久久| 色综合久久无码中文字幕| 亚洲国产精品嫩草影院久久| 97久久精品午夜一区二区| 亚洲国产另类久久久精品| 欧美色综合久久久久久| 99久久国产热无码精品免费久久久久| 中文字幕人妻色偷偷久久| 日日狠狠久久偷偷色综合0| 国产亚州精品女人久久久久久 | 亚洲国产天堂久久综合| 国产女人aaa级久久久级| 精品久久香蕉国产线看观看亚洲| 久久中文骚妇内射| 亚洲va中文字幕无码久久不卡| 亚洲国产小视频精品久久久三级 | 久久精品中文字幕久久| 国内精品久久久久影院一蜜桃| 久久99热这里只有精品66| 国内精品伊人久久久久妇| 天天影视色香欲综合久久| 亚洲精品美女久久久久99小说| 亚洲v国产v天堂a无码久久|