私は C で pthreads を使用していgcc ... -fsanitize=thread -g
ます。この投稿が氾濫するのを避けるために、実行可能ファイルを実行したときに生成される出力のごく一部のみを含めます。
WARNING: ThreadSanitizer: data race (pid=18858)
Write of size 4 at 0x000102ef7d80 by thread T2:
#0 eval_positioning 8q.c:178 (8q:x86_64+0x100003416)
Previous write of size 4 at 0x000102ef7d80 by thread T3:
[failed to restore the stack]
Location is global 'printouts' at 0x000102ef7100 (8q+0x000100008d80)
Thread T2 (tid=3263043, running) created by main thread at:
#0 pthread_create <null>:3 (libclang_rt.tsan_osx_dynamic.dylib:x86_64h+0x2cd8d)
#1 main 8q.c:249 (8q:x86_64+0x1000039c6)
Thread T3 (tid=3263063, running) created by main thread at:
#0 pthread_create <null>:3 (libclang_rt.tsan_osx_dynamic.dylib:x86_64h+0x2cd8d)
#1 main 8q.c:249 (8q:x86_64+0x1000039c6)
SUMMARY: ThreadSanitizer: data race 8q.c:178 in eval_positioning
行 178 は、次のコード セグメントを参照しています。
pthread_mutex_lock(&print_mutex);
memcpy(printouts.qpositions[++printouts.top], qpositions, N*sizeof(int));
pthread_mutex_unlock(&print_mutex);
このコード行は複数のスレッドからアクセスできるため、同期の問題を回避するためにロックを実装しました。また、この特定の共有メモリへの書き込みがファイル全体で発生するのはこれだけです。
typedef struct print_buf {
int qpositions[100][8];
int top;
} print_buf;
print_buf printouts = { {{0}}, -1 };
print_buf
結論として、構造体をロックで「正しく」保護していますが、サニタイザーは文句を言います。これは実際にどのようにデータ競合になるのでしょうか? 私は何が欠けていますか?