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
scattered resposibi;ity and authority -necessity of operation research

BEST FIT ALGORITHM Here scanning is for the finest fit one. Signifies it searches the entire list for the best fit position. For the process therefore in that the process will

explain any four types of operating system feature and structure(design)

Signal handling A signal is used in UNIX systems to inform a process that a particular event has occurred. 1. A signal is produced by the occurrence of a particular event.

Q. Consider the two-dimensional array A: int A[][] = new int[100][100]; Whereas A [0][0] is at location 200 in a paged memory system with pages of size 200. A little process

what is meant by lazzy swapper

The 5Z Company is selling pens to the local market. It is planning to maximize sales and profit by analyzing few conditions using the break-even analysis formula. Below is the data

What is scheduler? A process migrates among the various scheduling queues throughout its life time. The OS must choose processes from these queues in some fashion. This selecti

Explain the OS/2 Operating System Like NetWare, OS/2 manage resources at three levels, as shown in Figure 4.5 Resources may be maintained at the session, process, and thread le

Demand paging gives that pages could only be brought into memory if the running process acts them. This is usually referred to as lazy evaluation as only those pages operated by