全局变量竞争
jing@jing-Satellite-L510:~/tsan$ cat simple_race.cpp
#include <pthread.h>
#include <stdlib.h>
int Global;
void *Thread1(void *x)
{
Global++;
return NULL;
}
void *Thread2(void *x)
{
Global--;
return NULL;
}
int main()
{
pthread_t t[2];
pthread_create(&t[0], NULL, Thread1, NULL);
pthread_create(&t[1], NULL, Thread2, NULL);
Global++;
pthread_join(t[0], NULL);
pthread_join(t[1], NULL);
return 0;
}jing@jing-Satellite-L510:~/tsan$
jing@jing-Satellite-L510:~/tsan$ g++ -fsanitize=thread -fPIE -pie -g simple_race.cpp
jing@jing-Satellite-L510:~/tsan$ ./a.out
==================
WARNING: ThreadSanitizer: data race (pid=4062)
Read of size 4 at 0x55fc0e7e701c by main thread:
#0 main /home/jing/tsan/simple_race.cpp:24 (a.out+0x138a)
Previous write of size 4 at 0x55fc0e7e701c by thread T1:
#0 Thread1(void*) /home/jing/tsan/simple_race.cpp:8 (a.out+0x12a6)
Location is global 'Global' of size 4 at 0x55fc0e7e701c (a.out+0x00000000401c)
Thread T1 (tid=4064, finished) created by main thread at:
#0 pthread_create ../../../../src/libsanitizer/tsan/tsan_interceptors_posix.cpp:962 (libtsan.so.0+0x5ea79)
#1 main /home/jing/tsan/simple_race.cpp:21 (a.out+0x135d)
SUMMARY: ThreadSanitizer: data race /home/jing/tsan/simple_race.cpp:24 in main
==================
==================
WARNING: ThreadSanitizer: data race (pid=4062)
Read of size 4 at 0x55fc0e7e701c by thread T2:
#0 Thread2(void*) /home/jing/tsan/simple_race.cpp:14 (a.out+0x12e9)
Previous write of size 4 at 0x55fc0e7e701c by thread T1:
#0 Thread1(void*) /home/jing/tsan/simple_race.cpp:8 (a.out+0x12a6)
Location is global 'Global' of size 4 at 0x55fc0e7e701c (a.out+0x00000000401c)
Thread T2 (tid=4065, running) created by main thread at:
#0 pthread_create ../../../../src/libsanitizer/tsan/tsan_interceptors_posix.cpp:962 (libtsan.so.0+0x5ea79)
#1 main /home/jing/tsan/simple_race.cpp:22 (a.out+0x137e)
Thread T1 (tid=4064, finished) created by main thread at:
#0 pthread_create ../../../../src/libsanitizer/tsan/tsan_interceptors_posix.cpp:962 (libtsan.so.0+0x5ea79)
#1 main /home/jing/tsan/simple_race.cpp:21 (a.out+0x135d)
SUMMARY: ThreadSanitizer: data race /home/jing/tsan/simple_race.cpp:14 in Thread2(void*)
==================
ThreadSanitizer: reported 2 warnings
jing@jing-Satellite-L510:~/tsan$
全局变量解决竞争
jing@jing-Satellite-L510:~/tsan$ cat simple_race_fix.cpp
#include <pthread.h>
#include <stdlib.h>
int Global;
pthread_mutex_t global_lock;
void *Thread1(void *x)
{
pthread_mutex_lock(&global_lock);
Global++;
pthread_mutex_unlock(&global_lock);
return NULL;
}
void *Thread2(void *x)
{
pthread_mutex_lock(&global_lock);
Global--;
pthread_mutex_unlock(&global_lock);
return NULL;
}
int main()
{
pthread_mutex_init(&global_lock, NULL);
pthread_t t[2];
pthread_create(&t[0], NULL, Thread1, NULL);
pthread_create(&t[1], NULL, Thread2, NULL);
pthread_mutex_lock(&global_lock);
Global++;
pthread_mutex_unlock(&global_lock);
pthread_join(t[0], NULL);
pthread_join(t[1], NULL);
pthread_mutex_destroy(&global_lock);
return 0;
}jing@jing-Satellite-L510:~/tsan$
jing@jing-Satellite-L510:~/tsan$ g++ -fsanitize=thread -fPIE -pie -g simple_race_fix.cpp
jing@jing-Satellite-L510:~/tsan$ ./a.out
线程间共享变量竞争
#include <pthread.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
void *Thread2(void *x);
void *Thread1(void *x)
{
uint8_t *buffer = (uint8_t *)malloc(10 * sizeof(uint8_t));
pthread_t t2;
pthread_create(&t2, NULL, Thread2, buffer);
printf("buffer:%u\n", *buffer);
pthread_join(t2, NULL);
free(buffer);
return NULL;
}
void *Thread2(void *x)
{
uint8_t *buffer = (uint8_t *)x;
uint8_t *tmp = buffer;
for (size_t index = 0; index < 10; index++) {
*tmp = index;
tmp++;
}
return NULL;
}
int main()
{
pthread_t t1;
pthread_create(&t1, NULL, Thread1, NULL);
pthread_join(t1, NULL);
return 0;
}
jing@jing-Satellite-L510:~/tsan$ g++ -fsanitize=thread -fPIE -pie -g simple_race_2.cpp
jing@jing-Satellite-L510:~/tsan$ ./a.out
==================
WARNING: ThreadSanitizer: data race (pid=4384)
Read of size 1 at 0x7b0400000000 by thread T1:
#0 Thread1(void*) /home/jing/tsan/simple_race_2.cpp:13 (a.out+0x132d)
Previous write of size 1 at 0x7b0400000000 by thread T2:
#0 Thread2(void*) /home/jing/tsan/simple_race_2.cpp:24 (a.out+0x1402)
Location is heap block of size 10 at 0x7b0400000000 allocated by thread T1:
#0 malloc ../../../../src/libsanitizer/tsan/tsan_interceptors_posix.cpp:651 (libtsan.so.0+0x30323)
#1 Thread1(void*) /home/jing/tsan/simple_race_2.cpp:10 (a.out+0x12fe)
Thread T1 (tid=4386, running) created by main thread at:
#0 pthread_create ../../../../src/libsanitizer/tsan/tsan_interceptors_posix.cpp:962 (libtsan.so.0+0x5ea79)
#1 main /home/jing/tsan/simple_race_2.cpp:33 (a.out+0x146d)
Thread T2 (tid=4387, finished) created by thread T1 at:
#0 pthread_create ../../../../src/libsanitizer/tsan/tsan_interceptors_posix.cpp:962 (libtsan.so.0+0x5ea79)
#1 Thread1(void*) /home/jing/tsan/simple_race_2.cpp:12 (a.out+0x1321)
SUMMARY: ThreadSanitizer: data race /home/jing/tsan/simple_race_2.cpp:13 in Thread1(void*)
==================
buffer:0
ThreadSanitizer: reported 1 warnings
jing@jing-Satellite-L510:~/tsan$