1、介绍
ThreadSanitizer(TSAN)是一种C/C++数据竞争检测工具。数据竞争是多线程系统中最常见和最难调试的问题。数据竞争发生在多个线程访问相同数据,并且至少一个为写线程。在数据库系统中,涉及大量读写数据的并发操作,引入TSAN有利于发现线程调配间的不合理性,从而增强数据库的稳定性。
2、一个数据竞争的例子
#include <pthread.h> #include <stdio.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); pthread_join(t[0], NULL); pthread_join(t[1], NULL); }在这段程序中,线程t[0]和t[1]同时对全局变量进行写操作,导致数据冲突