Thread safety, Operating System

What does it mean for something to be thread-safe? By saying that X is thread-safe, we mean that if multiple threads use X at the same time, we don't have to worry about concurrency problems. The STL, for instance, is not thread-safe; if we were to create an STL queue and have two threads to operate on it simultaneously, we would have to manually perform all locking operations. The cout instruction is also not thread-safe.

Suppose now we want to build a thread-safe queue; the methods we want the queue to have are insert(item), remove() and empty(). The ?rst question to be asked is what should remove() do when the queue is empty. One solution would be for it to return a special value (such as NULL or -1, etc) or to throw an exception. It would be much more elegant and useful, however, to make that function call wait until something actually appears in the queue. By implementing this type of blocking system, we are in fact implementing part of a producer-consumer system.

Now let us think of how to make the function wait. We can spin, i.e. write something like while (empty()) ;. This, however, obviously doesn't work, since the test of emptiness needs to read shared data; we need to put locks somewhere! And if we lock around the while(empty()); line, the program will hang forever. The conclusion is that we need some way of going to sleep and at the same time having someone to wake us up when there's something interesting to do. Let us now present several possible implementations for this system and discuss why they do not work. The ?rst possibility is:

dequeue()
lock() // needs to lock before checking if it's empty
if (queue empty)
sleep()
remove_item()
unlock()
enqueue()
lock()
insert_item()
if (thread waiting)
wake up dequeuer
unlock()

Posted Date: 3/12/2013 5:16:55 AM | Location : United States







Related Discussions:- Thread safety, Assignment Help, Ask Question on Thread safety, Get Answer, Expert's Help, Thread safety Discussions

Write discussion on Thread safety
Your posts are moderated
Related Questions
Deadlock Detection and Recovery It's a method of permitting the system to enter a deadlock state, detect it and then recover. Deadlock detection : Is the process of

Q. a number of systems automatically delete all user files when a user logs off or a job terminates unless the user explicitly requests that they be kept other systems keep all fil

Q. Presume that the following processes arrive for execution at the times indicated. Every process will run the listed amount of time. In responding the questions use non pre-empti

Explain the TlsAlloc (VOID) This function is used to allocate a TLS index.Any subsequent TLS function calls will require  the handle returned  from TlsAlloc to be passed. The i

whta is an operating system ? what sorts services are provided by an operating system ?

Question : a) What do you understand by a Directory Structure? Support your answer with a diagram. b) Give an example how Access Lists and Groups are arranged to mode of

What is Belady's anomaly When plotting on a graph the page faults versus the number of available frames. We notice that the number of faults for four frames is greater than th

Write a script that uses this idea to print output similar to the following: Files in this directory that match *~ : bash-scripts.html~ chimpanzee~ countdown~ countd

The physical memory acts as a cache backed by the disk. When the physical memory is full, and we want to read in another page from disk, we have to evict a page from physical memor

Explain Deadlock Prevention-Resource allocation graph allocation Resource allocation graph algorithm :  Using this algorithm we are able to actually know if there exists in th