<pre name="code" class="cpp">#include "stdafx.h"
#include <boost/thread/thread.hpp>
#include <iostream>
#pragma pack(2)
struct Data{
char padding[62];
int i;
};
#pragma pack()
int kk = 0x12345678;
bool stop = false;
Data g_data;
void g(){
while(!stop){
g_data.i=0;
g_data.i=kk;
}
}
void f(){
int tmp;
while(!stop){
tmp = g_data.i;
if ( tmp != kk && tmp != 0){
return ; //这里可以加断点,因为发生torn-read现象时,kk可能是 0x00005678或0x12340000
}
}
}
int main(int argc, char* argv[])
{
boost::thread trd1(&g);
boost::thread trd2(&f);
while (true){
std::cin >> stop;
if (stop) break;
}
trd1.join();
trd2.join();
return 0;
}