あなたの代わりに-lpthread
の-pthread
(つまり、glibcのに対して)、おそらくいくつかの十年半で古くなっGCCの特別なオプションを使用する必要がありますアイデア。現代のglibcでは、pthreadsライブラリがリンクされているかどうかに基づいて、スレッド化への切り替えは完全に動的です。 glibcのヘッダには何も_REENTRANT
が定義されているかどうかに基づいてその動作を変更しません。動的切り替えの例として
、FILE *
ストリームを考えます。ストリーム上の特定の操作は、putc
のようなロックです。シングルスレッドのプログラムをコンパイルしているかどうかにかかわらず、同じputc
関数を呼び出します。それは、「pthreadの対応」putc
にプリプロセッサによって再ルーティングされません。何が起こるかは、何もしないスタブ関数がロックとロック解除の動作を行うために使用されることです。スレッドライブラリがリンクされている場合、これらの機能は、実際のものに上書きされます。
私はglibcのインストールのインクルードファイルツリー全体をザッgrep
をしました。 features.h
では、_REENTRANT
__USE_REENTRANT
が定義されます。次に、正確に1つのことは、__USE_REENTRANT
が存在するかどうかに依存するように見えるが、それを可能にする並列条件を有する。すなわち、<unistd.h>
にこれがあります:
#if defined __USE_REENTRANT || defined __USE_POSIX199506
/* Return at most NAME_LEN characters of the login name of the user in NAME.
If it cannot be determined or some other error occurred, return the error
code. Otherwise return 0.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern int getlogin_r (char *__name, size_t __name_len) __nonnull ((1));
#endif
これは怪しげに見えるし、廃止されています。私はglibc git repoのマスターブランチでそれを見つけることができません。
そして、ああ、見て単なる日前(12月6日)Aこのトピックで行われたコミット:変化の中で
https://sourceware.org/git/?p=glibc.git;a=commit;h=c03073774f915fe7841c2b551fe304544143470f
Make _REENTRANT and _THREAD_SAFE aliases for _POSIX_C_SOURCE=199506L.
For many years, the only effect of these macros has been to make
unistd.h declare getlogin_r. _POSIX_C_SOURCE >= 199506L also causes
this function to be declared. However, people who don't carefully
read all the headers might be confused into thinking they need to
define _REENTRANT for any threaded code (as was indeed the case a long
time ago).
:
--- a/posix/unistd.h
+++ b/posix/unistd.h
@@ -849,7 +849,7 @@ extern int tcsetpgrp (int __fd, __pid_t __pgrp_id) __THROW;
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern char *getlogin (void);
-#if defined __USE_REENTRANT || defined __USE_POSIX199506
+#ifdef __USE_POSIX199506
/* Return at most NAME_LEN characters of the login name of the user in NAME.
If it cannot be determined or some other error occurred, return the error
code. Otherwise return 0.
を参照してください。 :)
出典
2016-12-21 16:20:07
Kaz
実際にはpthreadを使用しないプログラムに対して、なぜ '-pthread'(または' -lpthread')を使用するのですか?あなたのプログラムが実際にpthreadを使用しているとしたら、どうして問題があるのでしょうか? –
私が理解しておいてほしいのは、あなたがこれをしたいと思う唯一の理由は、決して良い考えではない*ユニバーサルビルドコマンド*を持つことです。つまり、実行可能ファイルがマルチスレッドであるかどうかです。 gccのスマートリンケージに頼る必要はありません。 –
@JohnBollinger一般的なプロジェクトがビルドされています。 – PSkocik