16
Image: NASA artima Sample Click here to purchase the complete PDF document.

C++ & Beyond 2010 - Snoqualmie, WA USA · C++ and Beyond Snoqualmie, Washington, USA December 13-16, 2010 Schedule 8:45 - 9:00 Welcome Announcements Announcements Break Break Break

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: C++ & Beyond 2010 - Snoqualmie, WA USA · C++ and Beyond Snoqualmie, Washington, USA December 13-16, 2010 Schedule 8:45 - 9:00 Welcome Announcements Announcements Break Break Break

Image: NASA artima

SampleClick here to purchase the complete PDF document.

Page 2: C++ & Beyond 2010 - Snoqualmie, WA USA · C++ and Beyond Snoqualmie, Washington, USA December 13-16, 2010 Schedule 8:45 - 9:00 Welcome Announcements Announcements Break Break Break

C++ and Beyond 2010 Sample

Thank you for downloading this sample from the official presentation materials for the C++ and Beyond 2010 event, presented by Scott Meyers, Herb Sutter, and An-drei Alexandrescu. If you’d like to purchase the complete copy of these materials, please visit:

http://www.artima.com/shop/cpp_and_beyond_2010

Artima Press is an imprint of Artima, Inc.P.O. Box 305, Walnut Creek, California 94597

Copyright © 2011 Scott Meyers, Herb Sutter, Andrei Alexandrescu.All rights reserved.

All information and materials in this document are provided “as is” and without war-ranty of any kind.

The term “Artima” and the Artima logo are trademarks or registered trademarks of Artima, Inc. All other company and/or product names may be trademarks or regis-tered trademarks of their owners.

Page 3: C++ & Beyond 2010 - Snoqualmie, WA USA · C++ and Beyond Snoqualmie, Washington, USA December 13-16, 2010 Schedule 8:45 - 9:00 Welcome Announcements Announcements Break Break Break

C++ and BeyondSnoqualmie, Washington, USA

October 24-27, 2010

Schedule

Welcome [Andrei]

Break Break Break

Break BreakBreak

Sunday Monday Tuesday Wednesday

Group Breakfast[Attic]

Ask us anything...live![Panel]

Group Lunchand

Mid-Day Activity

Group Lunchand

Mid-Day Activity

Group Lunchand

Mid-Day Activity

Elements of Design, Part 1[Herb]

CAS-Based Concurrency[Andrei]

Ask us anything...in advance[Panel]

Group Breakfast[Attic]

Group Breakfast[Attic]

Points and Counterpoints[Panel]

Lambdas, Lambdas, Everywhere

[Herb]

A Fresh Look at Containers and Iterators

[Andrei]

Move Semantics, Rvalue References, and Perfect

Forwarding, Part 2[Scott]

Move Semantics, Rvalue References, and Perfect

Forwarding, Part 1[Scott]

CPU Caches andWhy You Care

[Scott]

Elements of Design, Part 2[Herb]

Reception[Ballroom]

Informal Discussions[Falls Terrace & Ballroom]

Informal Discussions[Falls Terrace & Ballroom]

Super Size Me: Lessons Learned Working at a Web

Company[Andrei]

Free Time(No Official C&B-Related Activities)

7:30 - 9:30

8:00 - 9:00

9:00 - 10:30

10:45 - 12:00

12:00 - 2:30

2:30 - 3:45

4:00 - 5:00

5:00 - 7:30

Click here to purchase the complete PDF document.

Page 4: C++ & Beyond 2010 - Snoqualmie, WA USA · C++ and Beyond Snoqualmie, Washington, USA December 13-16, 2010 Schedule 8:45 - 9:00 Welcome Announcements Announcements Break Break Break

C++ and BeyondSnoqualmie, Washington, USA

December 13-16, 2010

Schedule

8:45 - 9:00 Welcome Announcements Announcements

Break Break Break

Break Break Break

Scalable Use of the STL[Andrei]

Group Lunchand

Mid-Day Activity

Super Size Me: Lessons Learned Working at a Web

Company[Andrei]

Monday Tuesday Wednesday Thursday

Group Breakfast[Attic]

Group Breakfast[Attic]

Group Breakfast[Attic]

Move Semantics, Rvalue References, and Perfect

Forwarding, Part 1[Scott]

Elements of Design, Part 1[Herb]

Move Semantics, Rvalue References, and Perfect

Forwarding, Part 2[Scott]

CPU Caches andWhy You Care

[Scott]

Group Lunchand

Mid-Day Activity

Group Lunchand

Mid-Day Activity

Elements of Design, Part 2[Herb]

Reception[Falls Terrace]

Informal Discussions[Falls Terrace]

Informal Discussions[Falls Terrace]

Free Time(No Official C&B-Related Activities)

Lambdas, Lambdas, Everywhere

[Herb]

Q&A[Andrei, Scott, Herb]

Q&A[Herb, Andrei, Scott]

CAS-Based Concurrency[Andrei]

Informal C++0x Feature Overview

[Scott, Herb, Andrei]

8:00 - 8:45

2:30 - 4:00

7:30 - 9:30

9:00 - 10:30

10:45 - 12:00

12:00 - 2:30

5:00 - 7:30

4:15 - 5:00

Click here to purchase the complete PDF document.

Page 5: C++ & Beyond 2010 - Snoqualmie, WA USA · C++ and Beyond Snoqualmie, Washington, USA December 13-16, 2010 Schedule 8:45 - 9:00 Welcome Announcements Announcements Break Break Break

Scott Meyers, Software Development Consultant © 2010 Scott Meyers, all rights reserved.http://www.aristeia.com/

Move Semantics, Rvalue References, Perfect Forwarding

Scott�Meyers,�Software�Development�Consultanthttp://www.aristeia.com/

©�2010�Scott�Meyers,�all�rights�reserved.Last�Revised:�12/21/10

Scott�Meyers,�Ph.D.Software�Development�Consultant

[email protected] Voice:�503/638�6028http://www.aristeia.com/ Fax:�503/974�1887

Move�Semantics,�Rvalue�References,�and�Perfect�Forwarding

Scott�Meyers,�Software�Development�Consultanthttp://www.aristeia.com/

©�2010�Scott�Meyers,�all�rights�reserved.Slide�2

C++0x�Warning

Some�examples�show�C++0x�features�unrelated�to�move�semantics.

I’m�sorry�about�that.

But�not�that�sorry�:�)

Click here to purchase the complete PDF document.

Page 6: C++ & Beyond 2010 - Snoqualmie, WA USA · C++ and Beyond Snoqualmie, Washington, USA December 13-16, 2010 Schedule 8:45 - 9:00 Welcome Announcements Announcements Break Break Break

Scott Meyers, Software Development Consultant © 2010 Scott Meyers, all rights reserved.http://www.aristeia.com/

Move Semantics, Rvalue References, Perfect Forwarding

Scott�Meyers,�Software�Development�Consultanthttp://www.aristeia.com/

©�2010�Scott�Meyers,�all�rights�reserved.Slide�3

Move�SupportC++�sometimes�performs�unnecessary�copying:

typedef std::vector<T> TVec;TVec createTVec(); // factory functionTVec vt;…vt = createTVec(); // copy return value object to vt,

// then destroy return value object

createTVecTVec

T T T … T T T

vt

T T T … T T T

Scott�Meyers,�Software�Development�Consultanthttp://www.aristeia.com/

©�2010�Scott�Meyers,�all�rights�reserved.Slide�4

Move�SupportMoving values�would�be�cheaper:

TVec vt;…vt = createTVec(); // move data in return value object

// to vt, then destroy return value // object

createTVecTVec

T T T … T T T

vt

Click here to purchase the complete PDF document.

Page 7: C++ & Beyond 2010 - Snoqualmie, WA USA · C++ and Beyond Snoqualmie, Washington, USA December 13-16, 2010 Schedule 8:45 - 9:00 Welcome Announcements Announcements Break Break Break

Scott Meyers, Software Development Consultant © 2010 Scott Meyers, all rights reserved.http://www.aristeia.com/

Move Semantics, Rvalue References, Perfect Forwarding

Scott�Meyers,�Software�Development�Consultanthttp://www.aristeia.com/

©�2010�Scott�Meyers,�all�rights�reserved.Slide�5

Move�SupportAppending�to�a�full�vector causes�much�copying�before�the�append:

std::vector<T> vt;...vt.push_back(T object); // assume vt lacks

// unused capacity

vt T T T … T T T

T S

tate

T S

tate

T S

tate

T S

tate

T S

tate

T S

tate

T T T … T T T

T S

tate

T S

tate

T S

tate

T S

tate

T S

tate

T S

tate

T

T S

tate

Scott�Meyers,�Software�Development�Consultanthttp://www.aristeia.com/

©�2010�Scott�Meyers,�all�rights�reserved.Slide�6

Move�SupportAgain,�moving�would�be�more�efficient:

std::vector<T> vt;...vt.push_back(T object); // assume vt lacks

// unused capacity

Other�vector and�deque operations�could�similarly�benefit.

� insert,�emplace,�resize,�erase,�etc.

vt T T T … T T T

T S

tate

T S

tate

T S

tate

T S

tate

T S

tate

T S

tate

T T T … T T T T

T S

tate

Click here to purchase the complete PDF document.

Page 8: C++ & Beyond 2010 - Snoqualmie, WA USA · C++ and Beyond Snoqualmie, Washington, USA December 13-16, 2010 Schedule 8:45 - 9:00 Welcome Announcements Announcements Break Break Break

Scott Meyers, Software Development Consultant © 2010 Scott Meyers, all rights reserved.http://www.aristeia.com/

Move Semantics, Rvalue References, Perfect Forwarding

Scott�Meyers,�Software�Development�Consultanthttp://www.aristeia.com/

©�2010�Scott�Meyers,�all�rights�reserved.Slide�7

Move�SupportStill�another�example:

template<typename T> // straightforward std::swap impl. void swap(T& a, T& b){

T tmp(a); // copy a to tmp ( 2 copies of a)a = b; // copy b to a ( 2 copies of b)b = tmp; // copy tmp to b ( 2 copies of tmp)

} // destroy tmp

a

b

tmp copy of a’s state

a’s state

b’s state

copy of b’s state

copy of a’s state

Scott�Meyers,�Software�Development�Consultanthttp://www.aristeia.com/

©�2010�Scott�Meyers,�all�rights�reserved.Slide�8

Move�Supporttemplate<typename T> // straightforward std::swap impl. void swap(T& a, T& b){

T tmp(std::move(a)); // move a’s data to tmpa = std::move(b); // move b’s data to ab = std::move(tmp); // move tmp’s data to b

} // destroy (eviscerated) tmp

a

b

tmpa’s state

b’s state

Click here to purchase the complete PDF document.

Page 9: C++ & Beyond 2010 - Snoqualmie, WA USA · C++ and Beyond Snoqualmie, Washington, USA December 13-16, 2010 Schedule 8:45 - 9:00 Welcome Announcements Announcements Break Break Break

Elements of Design Herb Sutter Software Development Consultant

www.gotw.ca/training

© 2010 by Herb Sutter except material otherwise referenced.

Date updated: December 17, 2010 Page: 1

Herb Sutter

industrial design applies to industrial software

Click here to purchase the complete PDF document.

Page 10: C++ & Beyond 2010 - Snoqualmie, WA USA · C++ and Beyond Snoqualmie, Washington, USA December 13-16, 2010 Schedule 8:45 - 9:00 Welcome Announcements Announcements Break Break Break

Elements of Design Herb Sutter Software Development Consultant

www.gotw.ca/training

© 2010 by Herb Sutter except material otherwise referenced.

Date updated: December 17, 2010 Page: 2

1. Process

2. Principles

3. Elements

What do you think of this code?

CustomContainer<T> c;

for( auto i = src.begin(); i != src.end(); ++i ) { c.insert( *i ); }

CustomMap<string,string> phone;

phone*“John”+ = “212-555-1212”; // inserts into map

phone.Insert( “John”, “212-555-1212” ); // inserts into map

Click here to purchase the complete PDF document.

Page 11: C++ & Beyond 2010 - Snoqualmie, WA USA · C++ and Beyond Snoqualmie, Washington, USA December 13-16, 2010 Schedule 8:45 - 9:00 Welcome Announcements Announcements Break Break Break

Elements of Design Herb Sutter Software Development Consultant

www.gotw.ca/training

© 2010 by Herb Sutter except material otherwise referenced.

Date updated: December 17, 2010 Page: 3

Design for the user

Design For the User

Guiding star: What the consuming code looks like.

Start of design: First, write some of the calling code.

During design: Rinse and repeat.

Key goals:

Applicability: Solving an actual user’s problem.

Usability: Being understandable, discoverable.

Why it’s difficult:

You’re not him/her (nearly always).

Click here to purchase the complete PDF document.

Page 12: C++ & Beyond 2010 - Snoqualmie, WA USA · C++ and Beyond Snoqualmie, Washington, USA December 13-16, 2010 Schedule 8:45 - 9:00 Welcome Announcements Announcements Break Break Break

Elements of Design Herb Sutter Software Development Consultant

www.gotw.ca/training

© 2010 by Herb Sutter except material otherwise referenced.

Date updated: December 17, 2010 Page: 4

Pitfall: Avoid “Expert-Friendly” Design

Easy to say.

(Blush.)

C++0x example: Metaprogramming extensions vs. “auto.”

Which would you be most interested in spending time designing?

Which would most programmers be most interested in using (applicability) and able to understand (usability)?

Why it’s difficult:

You’re an expert.

Aim to enable “what,” not “how”

Click here to purchase the complete PDF document.

Page 13: C++ & Beyond 2010 - Snoqualmie, WA USA · C++ and Beyond Snoqualmie, Washington, USA December 13-16, 2010 Schedule 8:45 - 9:00 Welcome Announcements Announcements Break Break Break

© 2010- Andrei Alexandrescu. Do not redistribute. 1 / 33

CAS-Based ConcurrencyPrepared for C++ and Beyond 2010

Andrei Alexandrescu, PhD

[email protected]

This Talk

© 2010- Andrei Alexandrescu. Do not redistribute. 2 / 33

• Lock-free programming: Brief history

and introduction

• CAS-based code

• A Singly-Linked Lock-Free List

Click here to purchase the complete PDF document.

Page 14: C++ & Beyond 2010 - Snoqualmie, WA USA · C++ and Beyond Snoqualmie, Washington, USA December 13-16, 2010 Schedule 8:45 - 9:00 Welcome Announcements Announcements Break Break Break

Motto

© 2010- Andrei Alexandrescu. Do not redistribute. 3 / 33

“Multithreading is just one darn thing

after, before, or simultaneously with

another”.

Lock-free Programming:

Brief History and

Introduction

© 2010- Andrei Alexandrescu. Do not redistribute. 4 / 33

Click here to purchase the complete PDF document.

Page 15: C++ & Beyond 2010 - Snoqualmie, WA USA · C++ and Beyond Snoqualmie, Washington, USA December 13-16, 2010 Schedule 8:45 - 9:00 Welcome Announcements Announcements Break Break Break

Defining Terms

© 2010- Andrei Alexandrescu. Do not redistribute. 5 / 33

• Wait-free procedure: completes in a bounded

number of steps regardless of the relative

speeds of other threads

• Lock-free procedure: at any time, at least one

thread is guaranteed to make progress

◦ Probabilistically, all threads will finish

timely

• Mutex-based procedures

◦ Not wait-free

◦ Not lock-free

A Different Angle

© 2010- Andrei Alexandrescu. Do not redistribute. 6 / 33

• Lock-based: ask for synchronization device

prior to operation

• Pessimistically assumes contention

• Wait/Lock-free: Perform operation, attempt to

commit

• Optimistically assumes no contention

• “Better ask for forgiveness than permission”

Click here to purchase the complete PDF document.

Page 16: C++ & Beyond 2010 - Snoqualmie, WA USA · C++ and Beyond Snoqualmie, Washington, USA December 13-16, 2010 Schedule 8:45 - 9:00 Welcome Announcements Announcements Break Break Break

Brief History

© 2010- Andrei Alexandrescu. Do not redistribute. 7 / 33

• Lock-based threading theory established in

the 1960s

◦ Still the dominant model today

• By 1972—efforts to avoid mutex-based

pessimistic concurrency control

◦ Atomic assignment

◦ Use of atomic instructions: increment,

test-and-set

• By 1990—search for universal atomic

primitive that would enable all others

• 1991: “Wait-free synchronization” by Herlihy

settles the matter

Impossibility/Universality

© 2010- Andrei Alexandrescu. Do not redistribute. 8 / 33

• Some primitives cannot synchronize any

shared data structure for >2 threads

◦ test-and-set

◦ fetch-and-add

◦ atomic queues!

• Some other primitives are enough to

implement any shared data structure

◦ e.g., CAS

Click here to purchase the complete PDF document.