A multithreaded web server wishes to keep track of the numberof requests it services

Chapter 5, Problem 5.19

(choose chapter or problem)

Get Unlimited Answers
QUESTION:

A multithreaded web server wishes to keep track of the number of requests it services (known as hits). Consider the two following strategies to prevent a race condition on the variable hits. The first strategy is to use a basic mutex lock when updating hits:

int hits;

mutex_lock hit_lock;

hit_lock.acquire();

hits++;

hit_lock.release();

A second strategy is to use an atomic integer:

atomic_t hits;

atomic_inc(&hits);

Explain which of these two strategies is more efficient.

Questions & Answers

QUESTION:

A multithreaded web server wishes to keep track of the number of requests it services (known as hits). Consider the two following strategies to prevent a race condition on the variable hits. The first strategy is to use a basic mutex lock when updating hits:

int hits;

mutex_lock hit_lock;

hit_lock.acquire();

hits++;

hit_lock.release();

A second strategy is to use an atomic integer:

atomic_t hits;

atomic_inc(&hits);

Explain which of these two strategies is more efficient.

ANSWER:

Step 1 of 3

A race condition in an operating system is a situation when multiple processes access and manipulate the same data at the same time. The output in such a case depends upon the order of access that takes place. So, to avoid a race condition, mutual exclusion is used using either mutex locks or atomic integer.

Add to cart


Study Tools You Might Need

Not The Solution You Need? Search for Your Answer Here:

×

Login

Login or Sign up for access to all of our study tools and educational content!

Forgot password?
Register Now

×

Register

Sign up for access to all content on our site!

Or login if you already have an account

×

Reset password

If you have an active account we’ll send you an e-mail for password recovery

Or login if you have your password back