volatile int m = 0 ;
#define HELD_VALUE 1
int glbint = 0 ;
int main (int argc, char *argv[])
{
    int i ;
    for(i=0;i<10;i++)
    {
    while ( true )
    {
    int wasHeld = __sync_lock_test_and_set( &m, HELD_VALUE ) ;
    if ( !wasHeld )
    {
    printf("wasHeld=(%d)\n",wasHeld) ;
    break ;
    }
    // sleep and other stuff if you didn't get it
    }
    // Got the mutex, access the protected data.
    printf("1m=(%d)\n",m) ;
    glbint = glbint + 1 ;
    __sync_lock_release( &m ) ;
    printf("2m=(%d)\n",m) ;
    printf(".......(%d)\n",glbint) ;
    }//for
}

hedgezzz 發表在 痞客邦 留言(0) 人氣()