Boost logo

Boost :

From: Alexander Terekhov (terekhov_at_[hidden])
Date: 2001-09-25 03:16:47


> > > As for errorcheck mutex
> > > implementations... you don't need full collation you only need
> > > equality comparison, which boost::thread has already got.
> >
> > But I need something to compare to. Are you suggesting that I store
> a
> > boost::thread object inside the mutex, just for comparison
> purposes? Is this
> > the 'official' Boost.Threads way?
>
> Well, yes, that would be the current "official" Boost.Threads way.

IMHO a "better" way (which does not require extra synchronization
and thread id comparison) to track lock ownership status is to
use TSD (note that the problem of limited number of "native"
TSD slots could be solved quite easily and with almost no
overhead with respect to speed of "second level" TSD set/get
operations). e.g. recursive lock with error checking,
pseudo-code:

given:

key - tsd key (or sub-key)
lock - mutex/bin.semaphore (non-recursive lock)
nCount - int

init {

  tsd::init( &key );
  lock::init( &lock );
  nCount = 0;

}

destroy {

  lock::destroy( &lock );
  tsd::destroy( &key );

}

lock {

  if ( 0 == tsd::get( &key ) ) {

    tsd::set( &key,&key ); // ownership pending..

    lock.acquire();

    nCount = 1;

  }
  else {

    ++nCount;

  }
}

unlock {

  // optional EPERM check
  if ( 0 == tsd::get( &key ) )
    return EPERM;

  if ( 0 == --nCount ) {

    lock.release();

    tsd::set( &key,0 ); // retract ownership status

  }
}

regards,
alexander.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk