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

Structures for a file''s data, Q. Likewise some systems support many types ...

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:

Principles of informtiona tecnologu, i need to complete project on lan arch...

i need to complete project on lan architceture RAIDs etc

Multiprogramming, explain how response time are reduced by allowing time sh...

explain how response time are reduced by allowing time sharing on top of multiprogramming

Determine the minimum number of units of resource, An operating system invo...

An operating system involves 3 user processes each one requiring 2 units of resource R .The minimum number of units of R like no deadlocks will ever take place is The minimum

What is indexed allocation, What is indexed allocation? Every file has ...

What is indexed allocation? Every file has its own block of pointers to the sectors of the file.

Explain gui based operating system, GUI - Short for Graphical User Interfa...

GUI - Short for Graphical User Interface a GUI Operating System contains icons and graphics and is commonly navigated by using a computer mouse. Below are some instances of GUI Op

Enforce memory protection at compile time?, Why is it not possible to enfor...

Why is it not possible to enforce memory protection at compile time?   Every process should be protected against unwanted interference by other process, whether intentional or

Define a state which is not a fundamental process state, Define a state whi...

Define a state which is not a fundamental process state Answer: Blocked state is not a fundamental process state

Kernel mode and user mode function, Q. How does the distinction among kerne...

Q. How does the distinction among kernel mode and user mode function as a rudimentary form of protection (security) system? Answer: The distinction among kernel mode and user m

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