Difference between revisions of "Ns mutex"
Jump to navigation
Jump to search
(One intermediate revision by one other user not shown) | |||
Line 1: | Line 1: | ||
− | Man page: http://aolserver.com/docs/tcl/ | + | Man page: http://www.aolserver.com/docs/devel/tcl/api/thread.html#ns_mutex |
---- | ---- | ||
Line 30: | Line 30: | ||
'''EXAMPLES''' | '''EXAMPLES''' | ||
+ | |||
+ | It seems like the only difference between ns_mutex lock and ns_critsec enter is that a thread can block itself by calling ns_mutex lock twice on one lock without calling ns_mutex unlock on that lock. Is there some reason to use ns_mutex instead of ns_critsec? | ||
+ | |||
+ | nsv_set . special_file_mutex [ns_mutex create] | ||
+ | |||
+ | proc write_special_file {data} { | ||
+ | set mutex [nsv_get . special_file_mutex] | ||
+ | ns_mutex lock $mutex | ||
+ | set handle [open special_file w] | ||
+ | puts $handle $data | ||
+ | ns_mutex unlock $mutex | ||
+ | } | ||
+ | |||
+ | proc read_special_file {data} { | ||
+ | set mutex [nsv_get . special_file_mutex] | ||
+ | ns_mutex lock $mutex | ||
+ | set handle [open special_file r] | ||
+ | set result [read $handle] | ||
+ | ns_mutex unlock $mutex | ||
+ | return $result | ||
+ | } | ||
+ | |||
+ | proc change_special_file {data} { | ||
+ | set mutex [nsv_get . special_file_mutex] | ||
+ | # PROC WAITS FOREVER HERE | ||
+ | ns_mutex lock $mutex | ||
+ | set result [read_special_file] | ||
+ | write_special_file $data | ||
+ | ns_mutex unlock $mutex | ||
+ | return $result | ||
+ | } | ||
'''SEE ALSO''' | '''SEE ALSO''' |
Latest revision as of 20:12, 21 June 2006
Man page: http://www.aolserver.com/docs/devel/tcl/api/thread.html#ns_mutex
NAME
- ns_mutex - Operate on mutexes
SYNOPSIS
- ns_mutex option ?arg arg ...?
DESCRIPTION
- This command provides a mechanism to manipulate mutexes. The legal options (which may be abbreviated) are:
- ns_mutex create ?name?
- Initializes a new mutual exclusion (mutex) lock and returns a handle to it. If name is provided the mutex name will be set to this value.
- ns_mutex destroy object
- Destroys the mutex and frees any resources it was using.
- NOTE: The mutex must be unlocked, or else the behavior is undefined and will likely crash the server. Before using this, you should probably look at Implementing a Mutex Pool instead.
- ns_mutex lock object
- Attempt to acquire the mutex lock and block if it is already locked.
- ns_mutex unlock object
- Release a previously acquired mutex lock.
EXAMPLES
It seems like the only difference between ns_mutex lock and ns_critsec enter is that a thread can block itself by calling ns_mutex lock twice on one lock without calling ns_mutex unlock on that lock. Is there some reason to use ns_mutex instead of ns_critsec?
nsv_set . special_file_mutex [ns_mutex create] proc write_special_file {data} { set mutex [nsv_get . special_file_mutex] ns_mutex lock $mutex set handle [open special_file w] puts $handle $data ns_mutex unlock $mutex } proc read_special_file {data} { set mutex [nsv_get . special_file_mutex] ns_mutex lock $mutex set handle [open special_file r] set result [read $handle] ns_mutex unlock $mutex return $result } proc change_special_file {data} { set mutex [nsv_get . special_file_mutex] # PROC WAITS FOREVER HERE ns_mutex lock $mutex set result [read_special_file] write_special_file $data ns_mutex unlock $mutex return $result }
SEE ALSO