windows下靜態編譯pthread

來源:互聯網
上載者:User

標籤:

1. Building the library as a statically linkable library
-----------------------------------------------------

General: PTW32_STATIC_LIB must be defined for both the library build and the
application build. The makefiles supplied and used by the following ‘make‘
command lines will define this for you.

MSVC (creates pthreadVCn.lib as a static link lib):

nmake clean VC-static


MinGW32 (creates libpthreadGCn.a as a static link lib):

make clean GC-static


Define PTW32_STATIC_LIB when building your application. Also, your
application must call a two non-portable routines to initialise the
some state on startup and cleanup before exit. One other routine needs
to be called to cleanup after any Win32 threads have called POSIX API
routines. See README.NONPORTABLE or the html reference manual pages for
details on these routines:

BOOL pthread_win32_process_attach_np (void);
BOOL pthread_win32_process_detach_np (void);
BOOL pthread_win32_thread_attach_np (void); // Currently a no-op
BOOL pthread_win32_thread_detach_np (void);


The tests makefiles have the same targets but only check that the
static library is statically linkable. They don‘t run the full
testsuite. To run the full testsuite, build the dlls and run the
dll test targets.

=============================================

2. 使用pthread-win32靜態庫
使用pthread-win32靜態庫要注意:
1) 在程式中要定義宏PTW32_STATIC_LIB
2) pthread-win32靜態庫中沒有做attach和detach操作,要程式員來完成該任務,
否則線程有能成功建立。具體如下:
在程式入口(通常是main函數)處調用如下兩個函數
pthread_win32_process_attach_np();
pthread_win32_thread_attach_np();
程式結束時調用
pthread_win32_thread_detach_np();
pthread_win32_process_detach_np();

沒辦法,windows系統有很多標準它都不支援,寫起來就要麻煩點。
可以簡單地這樣處理:

a) 定義程式結束時的處理函數
#ifdef PTW32_STATIC_LIB
void pthread_win32_detach(void)
{
pthread_win32_thread_detach_np();
pthread_win32_process_detach_np();
}
#endif

b) 在程式入口處調用如下
#ifdef PTW32_STATIC_LIB
pthread_win32_process_attach_np();
pthread_win32_thread_attach_np();
atexit(pthread_win32_detach);
#endif

windows下靜態編譯pthread

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.