Operating system:three Easy Pieces---Lock Concurrent Data structures (Note)

Source: Internet
Author: User

Before moving beyong locks, we'll first describe how to use locks in some common data

Structures. Adding locks to a data structure to make it usable by threads makes the structure

Thread safe. Of course, exactly how such locks is added determines both the correctness

and performance of the data structure. And thus, our challenge:

Crux:how to Add Locks to Data structures

When given a particular data structure, what shoule we add locks to it, in order to do it work

Correctly? Further, how does we add locks such, the data structure yields high performance,

Enabling Mang threads to access the structure at once, i.e., concurrently?

Of course, we'll be hard pressed to cover all data structures or all methods for adding

Concurrency, as this was a topic that had been studied for years, with literally thousands of

Papers published about it. Thus, we hope to provide a sufficient introduction to the

Type of thinking required, and refer you-some good resources of material for further inquiry

On your own. We found Moir and Shavit ' s survey to be a great source of information.

Concurrent Counters

One of the simplest data structures is a counter, it's a structure that's commonly used and has

A simple interface. We define a simple nonconcurrent counter in Figure 29.1.

Simple and not scale

As can see, the non-synchronized counter is a trivial data structure, requiring a tiny amount

of code to implement. We are now having our next challenge:how can we do this code thread safe

? Figure 29.2 shows.

This concurrent counter are simple and works correctly. In the face, it follows a design pattern

Common to the simplest and more basic concurrent data structures:it simply adds a single

Lock, which is acquired if calling a routne that manipulates the data structure, and is

Released when returning from the call. In this manner, it's similar to a data structure built

With monitors, Wherer Locks is acquired and released automatically as call and return

From Object methods.

At the this point, you have a working concurrent data structure. The problem you might has is

Performance. If your data structure is too slow and you'll have the to does more than just add a single

Lock Such optimization, if needed, is thus the topic of the rest of the chapter. Note that if the

Data structure isn't too slow, you are done! No need to does something fancy if something simple

would work.

To understand the performance costs of the simple approach, we run a benchmark in which

Each thread updates a single shared counter a fixed number of times; We then vary the number

of threads. Figure 29.3 shows the total time taken, with one or four threads active.

Operating system:three Easy Pieces---Lock Concurrent Data structures (Note)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.