Mutex stands for "mutual exclusion", and is designed to let only one
process run in a critical section at a time.

During server initialization, you say:

ns_share lockname
set lockname [ns_mutex create]

To protect a section of code from multiple processes, you use:

ns_mutex lock $lockname
if {[catch {
   your stuff
} err]} {
  ns_mutex unlock $lockname
  error "my code is hosed with error $err"
} else {
  ns_mutex unlock $lockname
}
(other ways too of course; the important thing is to use a catch around
locked code)


A rwlock is a read/write lock.  This is used in a situation where many
readers can access a data structure but you want to single-thread write
access to avoid races.  Take the lock for reading when you are planning
to read, take it for writing when you need to change the critical
data structure.  Use the catch like above.  I have read that the overhead
of a rwlock is not worth it for short code sections, like where some
variables are being read/changed, but it might be useful for longer-running
code.

Jim

>
> hi!
> Could anybody explain what is a difference between ns_mutex and
> ns_rwlock (both are some kind of blocking mechanism). In documentation
> very little about it :-(
> thanks in advance
>
> --
>
> -------------------------------------------------------------------/\------
> Remigiusz Sokolowski      e-mail: [EMAIL PROTECTED]            /  \
> -----------------------------------------------------------------/____\-----
>

Reply via email to