2015-12-03 32 views
0

我試圖給here線程消毒不顯示數據爭

small_race.c

#include <pthread.h> 
int Global; 
void *Thread1(void *x) { 
    Global = 42; 
    return x; 
} 
int main() { 
    pthread_t t; 
    pthread_create(&t, NULL, Thread1, NULL); 
    Global = 43; 
    pthread_join(t, NULL); 
    return Global; 
} 

編譯程序

$ clang -fsanitize=thread -g -pthread -O1 small_race.c 
$./a.out ==> No error it's passing successfully 

我試圖創建2個線程,也試睡員在其中一個線程中,它也正在傳遞。我正在使用Debian操作系統

回答

0

您的平臺或安裝有問題。用您的確切代碼,我得到:

================== 
WARNING: ThreadSanitizer: data race (pid=20087) 
    Write of size 4 at 0x000000601080 by thread T1: 
    #0 Thread1(void*) /tmp/a.cpp:4 (a2+0x000000400a7f) 
    #1 <null> <null> (libtsan.so.0+0x0000000235b9) 

    Previous write of size 4 at 0x000000601080 by main thread: 
    #0 main /tmp/a.cpp:10 (a2+0x000000400ac5) 

    Location is global '<null>' of size 0 at 0x000000000000 (a2+0x000000601080) 

    Thread T1 (tid=20089, running) created by main thread at: 
    #0 pthread_create <null> (libtsan.so.0+0x000000027a67) 
    #1 main /tmp/a.cpp:9 (a2+0x000000400abb) 

SUMMARY: ThreadSanitizer: data race /tmp/a.cpp:4 Thread1(void*) 
==================