Thread safety, Operating System

Assignment Help:

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()


Related Discussions:- Thread safety

Define the features to implement top down parsing, Define the features are ...

Define the features are needed to implement top down parsing                      Source string marker, Prediction making mechanism and Matching and Backtracking mechanism

Why is rotational optimization no longer consequently useful, UNIX systems ...

UNIX systems utilized to use disk-layout optimizations based on the rotation position of disk data however modern implementations including Linux simply optimize for sequential dat

System bus and device speeds as the cpu speed increases, Q. Why is it impor...

Q. Why is it important to scale up system bus and device speeds as the CPU speed increases? Answer: Consider a system which carries out 50% I/O and 50% computes. Doubling-up

Define i/o and cpu bursts, Define I/O and CPU Bursts   Applications typ...

Define I/O and CPU Bursts   Applications typically to through cycle of CPU bursts and Input/output bursts. Since the CPU sits idle during I/O operations, efficiency is improved

What are the reasons for process suspension, Q. What are the reasons for pr...

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

List post-installation procedures that need to be performed, Problem: (...

Problem: (a) Based on your experience in setting up Windows 2003 Server operating system, briefly describe the twelve different steps in which the installation is conducted du

Interprocess communication, Now we consider the following questions: how ca...

Now we consider the following questions: how can the parent process communicate with its child? Or how can children processes communicate with other children? The exact answer depe

Explain short term scheduling, Short term scheduling The short term sch...

Short term scheduling The short term scheduler as well known as the dispatcher executes most frequently and makes the fine grained decision of which process to execute next. Th

Scheduler that select process from secondary storage device, A scheduler th...

A scheduler that selects processes from secondary storage device is known as                    Medium term scheduler is a scheduler that selects processes from secondary stor

#network OS, what are the design issues of network OS

what are the design issues of network OS

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd