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:

lock() // needs to lock before checking if it's empty
if (queue empty)
if (thread waiting)
wake up dequeuer

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
Explain the Advantage of Assembly Language The advantages of assembly language program is: a)    Reduced errors  b)    Faster translation times  c)    Changes could be ma

Define busy waiting and spinlock. When a process is in its significant section, any other process that tries to enter its critical section must loop continuously in the entry c

Refer a program with a linked origin of 5000. Suppose the memory area allocated to it have the start address of 70000. Determine the value to be loaded in relocation register?

Briefly discuss on real-time scheduling? It is divided into two types.  Hard real-time systems are needed to complete a critical task within a guaranteed amount of time. T

Explain the Proper Distribution of Functionality and Code It is important to note that the bandwidth of the server operating system and server processor availability is fixed.

Consider a scenario of post mailbox, there are two processes named sender S and receiver R. Both processes can communicate with each other by name along with message. You need to I

Give an overview of UNIX based file systems. UNIX and UNIX-like operating systems allocate a device name to each device, other than this isn't how the files on that device are

Define the Global vs. local variables It is also important to discuss the scope of resources being granted to the application program.  Variable scoping is defined as the acces

Q. What are the major advantages of the microkernel approach to system design? Answer: Benefits usually include the following (a) Adding a new service doesn't require modify