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

What are privileged instructions, What are privileged instructions? Som...

What are privileged instructions? Some of the machine instructions that may cause harm to a system are designated as privileged instructions. The hardware permits the privilege

Explain processing predefined application protocols, Explain Processing Pre...

Explain Processing Predefined Application Protocols Implementing and swift processing of predefined application protocols is sure to improve performance of server - side applic

Difference between threads and processes, Difference between threads and pr...

Difference between threads and processes 1. Thread is flow of implementation. Process is group of instruction which is alike to that of a program except which may be stopped an

Socket, socket based fortune teller sever.your program should create a serv...

socket based fortune teller sever.your program should create a server that listens to a specific port when a client receives a connection the server should respond with a random fo

Define unixware, Define UnixWare UnixWare manages resources at the proc...

Define UnixWare UnixWare manages resources at the process level. Each resource allocated to the application is actually allocated to the process representing the application. C

Multiprogramming, ? FREE ASSOCIATION ASSIGNMENTS of multiprograming

? FREE ASSOCIATION ASSIGNMENTS of multiprograming

What is the purpose of system calls, What is the purpose of system calls? ...

What is the purpose of system calls? System calls permit user-level processes to request services of the operating system.

What are the different accessing methods of a file, What are the different ...

What are the different accessing methods of a file? The different types of accessing a file are: Sequential access: Information in the file is accessed sequentially

Microsoft, Discuss the high barriers to entry in the market for PL operatin...

Discuss the high barriers to entry in the market for PL operating systems

Networking protocols, Modern networks are not implemented as a single piece...

Modern networks are not implemented as a single piece of software; that would render the task of dealing with multiple technologies and manufacturers virtually impossible. The solu

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