Rok*_*ady 18 c++ race-condition visual-studio-2008 conditional-statements
我有一些带有竞争条件的代码......我知道它是竞争条件,因为它不会一直发生,而且似乎更常发生在双核机器上.
当我追踪时,它永远不会发生.虽然,它也可能是一个僵局.通过分析完成和不发生的日志的完成阶段,我已经能够将此错误指向单个函数.但是,我不知道在这个功能的范围内发生了什么.它不在顶级.
如果是竞争条件,添加日志语句或断点将改变时间,并防止这种情况发生.
除了获得竞争条件分析器之外,我还能使用哪种技术来确定这种情况的发生方向吗?
这是在Visual Studio 9中,使用C++(非管理类型).
CLang和gcc 4.8+中包含一个名为ThreadSanitizer的工具.
您使用-fsanitize=thread标志编译代码
例:
$ cat simple_race.cc
#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);
}
Run Code Online (Sandbox Code Playgroud)
和输出
$ clang++ simple_race.cc -fsanitize=thread -fPIE -pie -g
$ ./a.out
==================
WARNING: ThreadSanitizer: data race (pid=26327)
Write of size 4 at 0x7f89554701d0 by thread T1:
#0 Thread1(void*) simple_race.cc:8 (exe+0x000000006e66)
Previous write of size 4 at 0x7f89554701d0 by thread T2:
#0 Thread2(void*) simple_race.cc:13 (exe+0x000000006ed6)
Thread T1 (tid=26328, running) created at:
#0 pthread_create tsan_interceptors.cc:683 (exe+0x00000001108b)
#1 main simple_race.cc:19 (exe+0x000000006f39)
Thread T2 (tid=26329, running) created at:
#0 pthread_create tsan_interceptors.cc:683 (exe+0x00000001108b)
#1 main simple_race.cc:20 (exe+0x000000006f63)
==================
ThreadSanitizer: reported 1 warnings
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10221 次 |
| 最近记录: |