Movatterモバイル変換


[0]ホーム

URL:


Wayback Machine
29 captures
13 Oct 2008 - 20 Nov 2023
OctDECNov
Previous capture05Next capture
200720082010
success
fail
COLLECTED BY
Collection:Common Crawl
Web crawl data from Common Crawl.
TIMESTAMPS
loading
The Wayback Machine - https://web.archive.org/web/20081205072023/http://www.ddj.com/cpp/210604448

Site Archive (Complete)
ABOUT US |CONTACT |ADVERTISE |SUBSCRIBE |SOURCE CODE |CURRENT PRINT ISSUE |NEWSLETTERS|RESOURCES|BLOGS|PODCASTS|CAREERS
C++
Email
Print
Reprint

add to:
Del.icio.us
Digg
Google
Furl
Slashdot
Y! MyWeb
Blink
September 29, 2008
Writing Lock-Free Code: A Corrected Queue

Think in transactions, know who owns what, and use ordered atomic variables

(Page1 of3)
Herb Sutter
Herb continues his exploration of lock-free code--this time focusing on creating a lock-free queue.
Herb is a software development consultant, a software architect at Microsoft, and chair of the ISO C++ Standards committee. He can be contacted atwww.gotw.ca.


As we saw last month [1], lock-free coding is hard even for experts. There, I dissected a published lock-free queue implementation [2] and examined why the code was quite broken. This month, let's see how to do it right.

Lock-Free Fundamentals

When writing lock-free code, always keep these essentials well in mind:

  • Key concepts. Think in transactions. Know who owns what data.
  • Key tool. The ordered atomic variable.

When writing a lock-free data structure, "to think in transactions" means to make sure that each operation on the data structure is atomic, all-or-nothing with respect to other concurrent operations on that same data. The typical coding pattern to use is to do work off to the side, then "publish" each change to the shared data with a single atomic write or compare-and-swap. [3] Be sure that concurrent writers don't interfere with each other or with concurrent readers, and pay special attention to any operations that delete or remove data that a concurrent operation might still be using.

Be highly aware of who owns what data at any given time; mistakes mean races where two threads think they can proceed with conflicting work. You know who owns a given piece of shared data right now by looking at the value of the ordered atomic variable that says who it is. To hand off ownership of some data to another thread, do it at the end of a transaction with a single atomic operation that means "now it's your's."

An ordered atomic variable is a "lock-free-safe" variable with the following properties that make it safe to read and write across threads without any explicit locking:

  • Atomicity. Each individual read and write is guaranteed to be atomic with respect to all other reads and writes of that variable. The variables typically fit into the machine's native word size, and so are usually pointers (C++), object references (Java, .NET), or integers.
  • Order. Each read and write is guaranteed to be executed in source code order. Compilers, CPUs, and caches will respect it and not try to optimize these operations the way they routinely distort reads and writes of ordinary variables.
  • Compare-and-swap (CAS) [4]. There is a special operation you can call using a syntax likevariable.compare_exchange( expectedValue, newValue )that does the following as an atomic operation: Ifvariable currently has the valueexpectedValue, it sets the value tonewValue and returns true; else returns false. A common use isif(variable.compare_exchange(x,y)), which you should get in the habit of reading as, "if I'm the one who gets to change variable fromx toy."

Ordered atomic variables are spelled in different ways on popular platforms and environments. For example:

  • volatile in C#/.NET, as involatile int.
  • volatile or* Atomic*in Java, as involatile int,AtomicInteger.
  • atomic<T> in C++0x, the forthcoming ISO C++ Standard, as inatomic<int>.

In the code that follows, I'm going to highlight the key reads and writes of such a variable; these variables should leap out of the screen at you, and you should get used to being very aware of every time you touch one.

If you don't yet have ordered atomic variables yet on your language and platform, you can emulate them by using ordinary but aligned variables whose reads and writes are guaranteed to be naturally atomic, and enforce ordering by using either platform-specific ordered API calls (such as Win32'sInterlockedCompareExchange for compare-and-swap) or platform-specific explicit memory fences/barriers (for example, Linuxmb).

1 Lock-Free Fundamentals|2 A Corrected One-Producer, One-Consumer Lock-Free Queue|3 Do Work, Then PublishNext Page
TOP 5 ARTICLES
No Top Articles.
DR. DOBB'S CAREER CENTER
Ready to take that job and shove it?open |close
Search jobs onDr. Dobb's TechCareers
Function:

Keyword(s):

State:  
  • Post Your Resume
  • Employers Area
  • News & Features
  • Blogs & Forums
  • Career Resources

    Browse By:
    Location |Employer |City
  • Most Recent Posts:



    MICROSITES
    FEATURED TOPIC

    ADDITIONAL TOPICS

    INFO-LINK



     



    |All Feeds
    © 2008 Think Services,Privacy Policy,Terms of Service,United Business Media LLC
    Comments about the web site:webmaster@ddj.com
    Related Sites:DotNetJunkies,SD Expo,SqlJunkies

    [8]ページ先頭

    ©2009-2025 Movatter.jp