I want to using multi thread to modify the global variable , so i need to create a critical section.
In the criaical section, i use atomicCAS to modify the g_odata , and i can control only one thread enter critical section.
The first thread enter the critical section when g_odata[0] = 1,and immediately set g_odata[0] = 0 to avoid other threads
enter the critical section.
The thread exits the critical section,and it will set g_odata[0] = 1 to allow other threads have the right enter the critical.
In the following code, i use the method to implement the critical section, but i don’t know why my computer always break
down when i execute the program.
Does anybody know what happend on my program??
Thank you very much.
[codebox]testKernel(int* g_odata)
{
////////// critical section start ////////
while(1)
{
//g_odata[0] is initialized to 1
if(atomicCAS(&g_odata[0],1,0) == 1)
break;
}
//modify any global variable or shared variable in the critical section
g_odata[0] = 1;
///////// critical section end //////
}[/codebox]