Tcl API Reference -- ns_rwlock

### ns_rwlock

#### Overview

Create, destroy, and manipulate read/write locks

#### Syntax

ns_rwlock create

ns_rwlock destroy rwlockid

ns_rwlock readlock rwlockid

ns_rwlock readunlock rwlockid

ns_rwlock writelock rwlockid

ns_rwlock writeunlock rwlockid

#### Description

ns_rwlock create initializes a read/write lock and returns an ID for it.

ns_rwlock destroy frees the resources associated with the specified read/write lock. The rwlockid argument is the read/write lock ID returned by ns_rwlock create when the lock was created.

ns_rwlock readlock acquires a read lock. Any number of read locks can be pending. If there's a write lock active, the read lock acquisition blocks until the write lock is released.

ns_rwlock readunlock releases a read lock.

ns_rwlock writelock acquires a write lock. Only one write lock can be in effect. If there are pending read locks active, the write lock acquisition blocks until all of the read locks drain. If a subsequent read lock acquisition attempt is made, the write lock has priority.

ns_rwlock writeunlock releases a write lock

#### About Read/Write Locks

Read/write locks are a serialization mechanism for using data structures where multiple reads can happen simultaneously, but where writes must happen singly. For example, suppose you have a hash table that is heavily used but doesn't change very often. You'd like to have multiple threads be able to read from the table without blocking on each other, but when you need to update the table, you can do so safely without having to worry about other threads reading incorrect data.

The principal feature of read/write locks is the mechanism of which locks have priority and which locks must wait. Any number of read locks can be pending. If there's a write lock active, the read lock acquisition blocks until the write lock is released. Also, only one write lock can be in effect. If there are pending read locks active, the write lock acquisition blocks until all of the read locks drain. If a subsequent read lock acquisition attempt is made while a write lock is waiting to acquire, the write lock has priority.

Top of Page
Copyright © 1998-99 America Online, Inc.