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
what do obelia eat and how do they feed

Q. In following Section we mentioned that disabling interrupts frequently could affect the system's clock. Describe why it could and how such effects could be minimized.

Q. Likewise some systems support many types of structures for a file's data while others simply support a stream of bytes. What are the merits in addition to demerits? Answer:

What disadvantages are there to this two-level directory? Without other provisions, two users who require cooperating with each other are hampered in reaching every other's fi

Calculate the Average Waiting Time CPU burst time points out the time, the process needs the CPU. The subsequent are the set of processes with their respective CPU burst time (

How is memory management done using linked lists?   Each one node of the l list will have 4 fields as follows 1-this tells whether it s a hole or a process 2-starting ad

i need the job to be done within 3days

Utility Programs: those programs which are not the essential part of operating system. But they provide different utilities / features to the users e.g. MS-DOS : Fdisk, FORMAT, AT

Q. What are the reasons for process suspension? Reasons for process suspension 1.  Swapping 2.  Timing 3.  Interactive user request 4.  Parent process request

Networks and operating systems have a lot of overlap. In this course, however,we are not going to discuss networking in detail, but rather focus on the networking abstractions prov