A Confused problem with thread checker

A Confused problem with thread checker

Recently,I justused the Intel Thread checker toolto test some algorithms used by my lock-free structure liberary,Unfortunately found that a simple CAS operation could cause the tool to give me some write->read race feed back.
I'm totally confused. The codes below:

volatile LONG g_lock = 0;
DWORD g_value = 0;

VOID ThreadProc( VOID ){
while ( TRUE ){

if ( InterlockedCompareExchange(&g_lock,1,0) == 0){

if ( g_value != 0 ) //[Code 1]
g_value += 1; //[Code2]

_ASSERT( g_lock == 1 );

LONG dwBefore = InterlockedCompareExchange(&g_lock, 0 , 1);

_ASSERT( dwBefore == 1 );
}//endif

::Sleep(1)
}
}

The g_lock and g_value are two global variable, then I started 10 threads to call ThreadProc function and ran together.
when I used thread checker to launch the program , write-read race problem was gained around [Code1] and [Code2],
I'm begging some one could help me...

3 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.
vladimir-tsymbal (Intel)'s picture

Quoting - kukubrian
Recently,I justused the Intel Thread checker toolto test some algorithms used by my lock-free structure liberary,Unfortunately found that a simple CAS operation could cause the tool to give me some write->read race feed back.
I'm totally confused. The codes below:

volatile LONG g_lock = 0;
DWORD g_value = 0;

VOID ThreadProc( VOID ){
while ( TRUE ){

if ( InterlockedCompareExchange(&g_lock,1,0) == 0){

if ( g_value != 0 ) //[Code 1]
g_value += 1; //[Code2]

_ASSERT( g_lock == 1 );

LONG dwBefore = InterlockedCompareExchange(&g_lock, 0 , 1);

_ASSERT( dwBefore == 1 );
}//endif

::Sleep(1)
}
}

The g_lock and g_value are two global variable, then I started 10 threads to call ThreadProc function and ran together.
when I used thread checker to launch the program , write-read race problem was gained around [Code1] and [Code2],
I'm begging some one could help me...

Hi,

This "lock free" construction is a sort of user-level synchronization which is nottracked by Thread Checker. However, the Thread Checker includes the user-level synchronization API to help you gather information related to user-defined synchronization constructs. In order to make Thread Checker aware of the protected code region use _itt notifications:__itt_notify_sync_acquired and __itt_notify_sync_releasing.

You'd have to include libittnotify.h header and to link libittnotify.lib library to your project (The library is installed in AnalyzerLib by default.). Please, see Thread Checker Help documentation for more details ("User-Level Synchronization API" and"User-Level Synchronization API Usage Examples" topics).

-Vladimir

Quoting - Vladimir Tsymbal (Intel)

Hi,

This "lock free" construction is a sort of user-level synchronization which is nottracked by Thread Checker. However, the Thread Checker includes the user-level synchronization API to help you gather information related to user-defined synchronization constructs. In order to make Thread Checker aware of the protected code region use _itt notifications:__itt_notify_sync_acquired and __itt_notify_sync_releasing.

You'd have to include libittnotify.h header and to link libittnotify.lib library to your project (The library is installed in AnalyzerLib by default.). Please, see Thread Checker Help documentation for more details ("User-Level Synchronization API" and"User-Level Synchronization API Usage Examples" topics).

Thank u very much , It's greately helpful.

Login to leave a comment.