From: David Bolen Date: 2000-06-30T20:51:02-04:00 Subject: [ruby-talk:03738] Re: Multithreaded/Embedded Ruby? "R. Hotaka" writes: > This is from Hotaka@TUT > > If a thread can read (but not write) the value of a variable defined > in (or possesed by) another thread, can we say that situation > "thread safe"? One thing to be cautious of is that thread-safety is often not just a read-only versus read-write issue. Data access in general can be risky depending on how that access is performed and how thread scheduling takes place. For example, in a pre-emptive threading system, if you have a variable that requires several machine instructions to fetch from memory and/or decode, then it is possible for one thread to be in the process of changing the variable while another is reading it (or vice versa), and for the reading thread to obtain a corrupted version of the variable. Even if the reading thread accesses the variable as a read-only object, without proper exclusion protection on the access, it is still possible to not be a thread-safe system. Such corruption can occur at higher levels as well - let's say you have an object that you can request state from and that state consists of several pieces of information. Even if you are treating the object as read-only, if someone else can change that state, and interrupts your thread that is querying it, you may obtain state with a mixture of information from before and after the change. That is, unless the object itself ensures the integrity of any state changes and access to the state information. In general, to really consider a system thread-safe, it must be a system that was designed to expect multiple accesses to common data and resources, and thus to always protect any accesses to such resources with some protection (semaphores, critical sections, etc...). The resources being protected may be individual data elements, or they may be higher level structures which must be self-consistent at all points in time. -- -- David -- /-----------------------------------------------------------------------\ \ David Bolen \ E-mail: db3l@fitlinxx.com / | FitLinxx, Inc. \ Phone: (203) 708-5192 | / 860 Canal Street, Stamford, CT 06902 \ Fax: (203) 316-5150 \ \-----------------------------------------------------------------------/