Difference between revisions of "Ns critsec"
Jump to navigation
Jump to search
(Added an example) |
(Added multiple sequential calls to ns_critsec enter to the example) |
||
| Line 31: | Line 31: | ||
Is this the right way to use this command? The example is contrived, but I wanted to call ns_critsec multiple times. | Is this the right way to use this command? The example is contrived, but I wanted to call ns_critsec multiple times. | ||
| − | # If a thread is executing | + | # If a thread is executing any of these procs, no other |
| − | # thread can execute any of these procs until | + | # thread can execute any of these procs until the first |
# thread has completely finished. | # thread has completely finished. | ||
| Line 38: | Line 38: | ||
proc write_special_file {data} { | proc write_special_file {data} { | ||
| − | + | set critsec [nsv_get . special_file_critsec] | |
| + | ns_critsec enter $critsec | ||
set handle [open special_file w] | set handle [open special_file w] | ||
puts $handle $data | puts $handle $data | ||
close $handle | close $handle | ||
| − | ns_critsec leave | + | ns_critsec leave $critsec |
} | } | ||
proc read_special_file {} { | proc read_special_file {} { | ||
| − | + | set critsec [nsv_get . special_file_critsec] | |
| + | ns_critsec enter $critsec | ||
set handle [open special_file r] | set handle [open special_file r] | ||
set result [read $handle] | set result [read $handle] | ||
close $handle | close $handle | ||
| − | ns_critsec leave [nsv_get . special_file_critsec] | + | ns_critsec leave $critsec |
| + | return $result | ||
| + | } | ||
| + | |||
| + | proc change_special_file {data} { | ||
| + | set critsec [nsv_get . special_file_critsec] | ||
| + | ns_critsec enter $critsec | ||
| + | set result [read_special_file] | ||
| + | write_special_file $data | ||
| + | ns_critsec leave $critsec | ||
return $result | return $result | ||
} | } | ||
Revision as of 20:09, 21 June 2006
Man page: http://aolserver.com/docs/tcl/ns_critsec.html
NAME
- ns_critsec - Operate on critical section objects
SYNOPSIS
- ns_critsec option ?arg arg ...?
DESCRIPTION
- This command provides a mechanism to manipulate critical section objects. The legal options (which may be abbreviated) are:
- ns_critsec create
- Initializes a new critical section object and returns a handle to it.
- ns_critsec destroy object
- Destroys the critical section object and frees any resources it was using.
- ns_critsec enter object
- Enters the critical section. The thread will block if another thread is already in the critical section.
- ns_critsec leaves object
- Leaves the critical section. When the thread leaves the critical section as many times as it has entered, a notification will be sent to other threads that are waiting on the critical section.
EXAMPLES
Is this the right way to use this command? The example is contrived, but I wanted to call ns_critsec multiple times.
# If a thread is executing any of these procs, no other
# thread can execute any of these procs until the first
# thread has completely finished.
nsv_set . special_file_critsec [ns_critsec create]
proc write_special_file {data} {
set critsec [nsv_get . special_file_critsec]
ns_critsec enter $critsec
set handle [open special_file w]
puts $handle $data
close $handle
ns_critsec leave $critsec
}
proc read_special_file {} {
set critsec [nsv_get . special_file_critsec]
ns_critsec enter $critsec
set handle [open special_file r]
set result [read $handle]
close $handle
ns_critsec leave $critsec
return $result
}
proc change_special_file {data} {
set critsec [nsv_get . special_file_critsec]
ns_critsec enter $critsec
set result [read_special_file]
write_special_file $data
ns_critsec leave $critsec
return $result
}
SEE ALSO