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

            八葉草

            學(xué)習(xí)資料記錄

            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的項(xiàng)目文件, 但是打不開的, 只能用nmake. 默認(rèn)的會(huì)告訴你一堆nmake參數(shù)的.
                我所要用的是編譯成static的library, 所以輸入"nmake clean VC-static", 編譯很快的. 不過默認(rèn)會(huì)鏈接到VC的crt, 我們需要修改它的makefile. 找到CFLAGS那一行, 把"/MD"改成"/MT".

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

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

            1. 下載pthreads win32源代碼:
                ftp://sourceware.org/pub/pthreads-win32/pthreads-w32-2-8-0-release.tar.gz
            2. 編譯靜態(tài)庫:
            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. 編譯時(shí)確保傳入-DPTW32_STATIC_LIB,鏈接時(shí)加入-lpthreadGC2, OK!


            http://hi.baidu.com/lff0305/blog/item/2a55e7366ebba6360b55a942.html
            pthread2提供了不同的.lib以及相對應(yīng)的.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 = 沒有使用異常機(jī)制, 而是使用setjump/longjmp

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

             

               詳細(xì)請參照pthread的readme.


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

            pthread 靜態(tài)編譯版本在Windows下使用時(shí)的注意事項(xiàng)
            作為通用的跨平臺(tái)高性能線程庫,在很多跨平臺(tái)的項(xiàng)目中都可以看見pthread的身影。pthread本身的實(shí)現(xiàn)比較優(yōu)雅,APIs使用起來也很方便。

            但在Windows下使用靜態(tài)編譯的pthread時(shí)要特別注意一下,必須顯式的調(diào)用如下四個(gè)函數(shù),否則pthread用到的一些全局變量會(huì)沒有被初始化,導(dǎo)致所有的pthread的APIs調(diào)用都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.

            通過函數(shù)的名字我們不難猜測出如下調(diào)用順序

            在程序開始的時(shí)候要調(diào)用:

            BOOL pthread_win32_process_attach_np (void);

            BOOL pthread_win32_thread_attach_np (void);

            在程序退出時(shí)要調(diào)用:

            BOOL pthread_win32_thread_detach_np (void);

            BOOL pthread_win32_process_detach_np (void);

            比較通用的做法是在模塊Load和UnLoad的時(shí)候做這個(gè)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靜態(tài)編譯的標(biāo)志,這個(gè)可以通過pthread.h的配置或者CFLAGS傳遞進(jìn)來。
            下面是pthread的官方的dll.c的實(shí)現(xiàn)
            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官方代碼在動(dòng)態(tài)編譯的版本中主動(dòng)做了這個(gè)attach和detach操作。
            而靜態(tài)編譯版本由于沒有一個(gè)合適的地方來做這件事,就將attach和detach的的操作扔給用戶來完成了。
            上面的代碼是針對調(diào)用方是Dll的情況做的初始化,如果調(diào)用方不是Dll呢?對此可以參照如下做法,雖然很暴力,但很簡單,可以工作
            1)定義如下函數(shù)
            #ifdef PTW32_STATIC_LIB
            static void detach_ptw32(void)
            {
                pthread_win32_thread_detach_np();
                pthread_win32_process_detach_np();
            }
            #endif
            2)在你的主程序的入口處,一般而言是main()中做如下調(diào)用即可
            #ifdef PTW32_STATIC_LIB
                pthread_win32_process_attach_np();
                pthread_win32_thread_attach_np();
                atexit(detach_ptw32);
            #endif
             
            也就是用atexit()將detach工作掛接到程序中去,使得程序在退出的時(shí)候可以對pthread進(jìn)行detach.

            posted on 2010-11-15 15:41 八葉草 閱讀(2277) 評論(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);

            寫了第一個(gè)pthread_win32的創(chuàng)建線程,進(jìn)程core了,搞了好久,
            后來我在一個(gè)開源軟件中,類似的做法,才得知這個(gè)規(guī)則。你總結(jié)的
            很好,向你學(xué)習(xí)~  回復(fù)  更多評論   

            久久精品国产第一区二区三区| 亚洲伊人久久精品影院| 国产精品久久久久影院色| 久久精品国产第一区二区三区| 久久er国产精品免费观看2| 久久天天躁狠狠躁夜夜不卡| 国产精品久久久久久久人人看 | 久久青草国产手机看片福利盒子| 久久久国产精品福利免费| 久久国产精品无| 久久精品国产亚洲一区二区| 婷婷久久综合| 久久精品国产半推半就| 伊人久久大香线蕉综合影院首页| 91精品国产综合久久精品| 香蕉久久久久久狠狠色| 亚洲国产二区三区久久| 性欧美丰满熟妇XXXX性久久久| 国产—久久香蕉国产线看观看| 亚洲中文久久精品无码| 亚洲国产精品无码久久久久久曰 | 久久亚洲精品视频| 久久精品国产亚洲av麻豆图片| 国产精品99久久久久久宅男 | 国产精品久久久久天天影视| 午夜精品久久久久久影视riav| 国产99久久久国产精品~~牛 | 婷婷久久综合| 久久一区二区免费播放| 久久精品亚洲精品国产欧美| 青青草原综合久久大伊人精品| 久久久久人妻精品一区二区三区| 亚洲国产精品嫩草影院久久| 久久久久亚洲AV成人网人人网站| 丁香五月综合久久激情| 久久99精品国产麻豆蜜芽| 久久国产精品波多野结衣AV| 人人狠狠综合久久亚洲88| 精品一区二区久久| 国产亚洲精久久久久久无码AV| 久久香蕉一级毛片|