SlideShare ist ein Scribd-Unternehmen logo
1 von 21
∵ FRECKLE ∴
Lock-Free Usage of Standard STL
          Containers*



                                  * (almost)
Time Constrains
• Very advanced talk – limited time
  – Must assume prior knowledge and skim a lot!
• Assume:
  – Familiarity with reference counting smart-pointers
  – Basic understanding of threading concepts.


• No need for C++11 compiler
  – Boost provides all missing functionality (v1.48 and up)
Time Constrains
• No time to go deep into:
  – C++11
     • std::shared_ptr<>
     • Lock-based facilities – e.g. std::mutex
     • New memory model
     • Move semantics – r-values
  – Concurrent programming
     • Lock-based programming
        – Pitfalls: dead-locks, data-races, resource release problems
     • Lock-free programming
        – Pitfalls: hard, tricky, subtle, platform-specific hardware support (e.g.
          CAS), ABA etc.
A Problem I Had…
• A highly concurrent server system
• Must update a shared vector of in-memory
  objects while these objects are being used by
  other threads.
• Cannot afford locking the whole vector

• Problem: How to update this shared vector
  without:
  – Delaying other threads
  – Causing multithreaded havoc such as data-
    races, deadlocks, memory leaks or premature release.
The Ecosystem
• Concurrent hardware becoming pervasive:
  – Hyper-Threading
  – Multiple cores
  – [GPGPUs, cloud…]
• True parallelism – not just time-sharing
• Demand for high performance and fast response
  is growing – think web-servers

• One way to maximize the power of parallelism is:
           Lock-Free Data-Structures!
Lock-free Programming

                     WARNING:
       Lock-free and wait-free data-structures
                 and algorithms are
                   HARD to write!

• Some wise advice: “Use Locks!”
        Tony Van Eerd, BoostCon 2011 presentation: “The Basics of Lock-free Programming”




• Consider lock-free algorithms as a last resort.
Lock-Free Programming
• Non-blocking algorithms
• Guaranteed system-wide progress at all times.
• No locks used
   – No dead-locks
   – Reduced contention = improved hardware utilization
• Generally, use atomic hardware read-modify-write primitives
   – These are like single HW instruction “critical sections”

• A lot of research in recent years = many new lock-free data-
  structures. However, implementations are:
   –   Often designed for particular use-cases
   –   Not always portable (platform-dep. atomic support)
   –   Not always well-tested, more proof-of-concept
   –   Not always suitably licensed for commercial use.
FRECKLE*
 • A general scheme for providing portable lock-free access
   to, and modification of, standard STL containers.
 • Can be easily adapted to non-STL containers.
 • Leverages new standard atomic reference-counting
   guarantees to provide automatic garbage collection.
 • Uses only standard C++11 components.

 But:
 • It is not a replacement for more specialized lock-free
   containers, but in many cases may suffice.
 • There is no free-lunch, and FRECKLE entails various trade-
   offs and certain usage conditions that will be detailed.

* Poor-man’s ‘LoCKFREE’ acronym, a distant cousin of the Pimpl.
The FRECKLE Setup
• Provide shared access to a container of shared
  objects.
• Useful for
  – Frequent map lookups with infrequent map updates.
  – Optimize greedy vector-traversal algorithms
• Use only a single object that is shared between
  the multiple threads.
• This shared object is a std::shared_ptr to an STL
  container of shared objects.
What does “shared” mean?
Two meanings:
1. A shared variable:
   A variable that can be seen concurrently by multiple
   threads, this ‘shared’ applies to the instance of the
   shared_ptr itself


2. In the shared_ptr type name:
   Refers to the fact that multiple objects may be
   “pointing” to the same data (from one or more
   threads) – this is the pointer abstraction.
   This refers to the data pointed to by the shared_ptr
   instance.
The FRECKLE Setup
• Use a single shared shared_ptr to an STL container of
  shared objects.
• The container is a container of shared_ptrs to the desired
  data type.
• The data-type can be:
    – Arbitrarily large
    – Does not even need to be
      CopyConstructible, CopyAssignable, and LessThanComparable
      and thus not even generally allowed for direct use within
      standard containers, it may also be an incomplete type.
    – A polymorphic type like an abstract interface

using namespace std;
shared_ptr< vector< shared_ptr< MyAbstractInterface const>>> sharedObjects;
shared_ptr< vector< shared_ptr< atomic<int>>>>               atomicCounters;
shared_ptr< map   < size_t, shared_ptr< int const>>>         sharedMap;
When/Where Can FRECKLE Help?
1.   Multiple concurrent readers that use only const access to the container.
     – Readers do not modify the shared shared_ptr to the container;
     – Readers do not modify the container itself;
     – Readers should not, generally, modify the container data, unless this data is
       safe (read: designed for) for concurrent modification (e.g. using std::atomic<>).

2.   One or more writers such that:
     – Writer(s) may modify the shared shared_ptr to the container;
     – Writer(s) may modify the container itself (e.g. insert, remove, sort, resize);
     – Writer(s) should not, generally, modify the container data, unless this data is
       safe for concurrent modification (e.g. std::atomic<>) or by using the approach
       mentioned below.

• Intuitively, FRECKLE provides shared access to a container of shared
  objects via “virtual snapshots”.
• These “virtual snapshots” do not in-fact incur any access runtime
  overheads.
C++11 Facilities – shared_ptr
• std::shared_ptr<>
  – A replacement for raw pointers for dynamic data
    management
  – Reference counted smart-pointer
  – Automatic deleting of data upon ref-count == 0 in dtor
    (RAII)
  – Use std::make_shared() instead of new.
  – Supports:
     • Deleters, weak_ptr, use in containers …
const Access to shared_ptr is Atomic
•   From the Boost shared_ptr documentation about thread-safety:

     –   shared_ptr objects offer the same level of thread safety as built-in types.
     –   A shared_ptr instance can be "read" (accessed using only const operations) simultaneously by multiple threads.
     –   Different shared_ptr instances can be "written to" (accessed using mutable operations such as operator= or reset)
         simultaneously by multiple threads (even when these instances are copies, and share the same reference count
         underneath.)
     –   Any other simultaneous accesses result in undefined behavior.

           •   shared_ptr<int> p(make_shared<int>(42)); // the shared shared_ptr
               //--- Example 1 ---
               // thread A
               shared_ptr<int> p2(p); // reads p
               // thread B
               shared_ptr<int> p3(p); // OK, multiple reads are safe

           •   p is in shared memory and visible to both threads A and B.
           •   Objects p2 and p3 are local variables and are not (necessarily) shared or visible to the other thread.


•   In legalese, section §20.7.2.2 of the C++11 ISO standard states:
     –   For purposes of determining the presence of a data race, member functions shall access and modify only the
         shared_ptr [...] objects themselves and not objects they refer to. Changes in use_count() do not reflect modifications
         that can introduce data races.


• TAKE HOME MESSAGE: as long as there are no concurrent non-use_count()
  changes, the single reference-count of p, p2 and p3, will always be properly, i.e.
  atomically, adjusted, with no data-races.
Atomic Access to a shared_ptr
• To modify a shared_ptr instance (item 4), we must use special atomic
  functions provided by the standard. Section §20.7.2.5 of the standard
  states:
    – Concurrent access to a shared_ptr object from multiple threads does not introduce
      a data race if the access is done exclusively via the functions in this section and the
      instance is passed as their first argument.
• Of particular interest in this case are:
    – atomic_store(), atomic_compare_exchange(), atomic_load()

• atomic_store allows the atomic update of a shared shared_ptr object
  to share the same ref-count and ref-object as another (e.g. thread-local)
  shared_ptr instance.
• atomic_compare_exchange is a more generalized form of
  atomic_store.
• atomic_load allows the atomic creation of a (e.g. thread-local)
  shared_ptr instance which shares the same ref-count and ref-object of
  another shared shared_ptr instance even in the presence of possible
  concurrent modification of this shared shared_ptr instance.
FRECKLE – Single Writer
class MyDataClass;
typedef vector<shared_ptr<MyDataClass const>> Container;
shared_ptr<Container> freckle;

// The reader thread(s)
void reader()
{   shared_ptr<Container const> p = atomic_load( &freckle );
    // use *p;
}

// The single writer case

void writer()
{   // copy container
    shared_ptr<Container> p(make_shared<Container>(*freckle));
    // update *p reflecting new information;
    atomic_store( &freckle, move( p ) );
}
FRECKLE – Multiple Writers
class MyDataClass;
typedef vector<shared_ptr<MyDataClass const>> Container;
shared_ptr<Container> freckle;

// The reader thread(s)
void reader()
{   shared_ptr<Container const> p = atomic_load( &freckle );
    // use *p;
}

// The multiple writers case
void writer()
{   shared_ptr<Container> p = atomic_load( &freckle );
    shared_ptr<Container> q;
    do
    {   // copy container
        q = make_shared<Container>( *p );
        // update *q reflecting new information;
    }
    while( !atomic_compare_exchange( &freckle, &p, move(q )));
}
atomic_compare_exchange()
• The function atomic_compare_exchange() does all of the
  following atomically:
   – If p and freckle are equivalent – i.e. pointing to the same data,
     then:
       • freckle is updated to point at q’s data - the newly created copy.
         Just like with the single reader case.
       • Else: p is updated to point at the updated data pointed to by freckle.
         This update is equivalent to an internal atomic_load like on the first line.

// The multiple writers case
void writer()
{   shared_ptr<Container> p = atomic_load( &freckle );
    shared_ptr<Container> q;
    do
    {   // copy container
        q = make_shared<Container>( *p );
        // update *q reflecting new information;
    }
    while( !atomic_compare_exchange( &freckle, &p, move(q )));
}
Exception Safety
Provides the Strong Exception Safety Guarantee:
  The operation has either completed successfully or thrown an
  exception, leaving the program state exactly as it was before
  the operation started.

All operations are in one of the following categories:
1. const access to shared data - no change in the
    program state.
2. Modifications only of stack-local non-shared
    variables and data - no change to the rest of the
    program state.
3. Only atomic non-throwing operations actually alter
    shared data.
Additional Notes
• Why (*almost)?
• Move semantics
• Primitive Data Types

• Further reading:
  – C++ Concurrency in Action
    Anthony Williams
∵          Questions                       ∴
Adi Shavit ∵ adishavit@gmail.com ∴ 050-7637223

Weitere ähnliche Inhalte

Was ist angesagt?

7 streams and error handling in java
7 streams and error handling in java7 streams and error handling in java
7 streams and error handling in javaJyoti Verma
 
Posix threads(asha)
Posix threads(asha)Posix threads(asha)
Posix threads(asha)Nagarajan
 
Threads - Why Can't You Just Play Nicely With Your Memory?
Threads - Why Can't You Just Play Nicely With Your Memory?Threads - Why Can't You Just Play Nicely With Your Memory?
Threads - Why Can't You Just Play Nicely With Your Memory?Robert Burrell Donkin
 
Multithreading Presentation
Multithreading PresentationMultithreading Presentation
Multithreading PresentationNeeraj Kaushik
 
Intro To .Net Threads
Intro To .Net ThreadsIntro To .Net Threads
Intro To .Net Threadsrchakra
 
MULTITHREADING CONCEPT
MULTITHREADING CONCEPTMULTITHREADING CONCEPT
MULTITHREADING CONCEPTRAVI MAURYA
 
Correct and efficient synchronization of java thread
Correct and efficient synchronization of java threadCorrect and efficient synchronization of java thread
Correct and efficient synchronization of java threadoutofmemoryerror
 
Threads - Why Can't You Just Play Nicely With Your Memory_
Threads - Why Can't You Just Play Nicely With Your Memory_Threads - Why Can't You Just Play Nicely With Your Memory_
Threads - Why Can't You Just Play Nicely With Your Memory_Robert Burrell Donkin
 
2 second lesson- attributes
2 second lesson- attributes2 second lesson- attributes
2 second lesson- attributesMohammad Alyan
 
.NET: Thread Synchronization Constructs
.NET: Thread Synchronization Constructs.NET: Thread Synchronization Constructs
.NET: Thread Synchronization ConstructsSasha Kravchuk
 
Hpc Visualization with X3D (Michail Karpov)
Hpc Visualization with X3D (Michail Karpov)Hpc Visualization with X3D (Michail Karpov)
Hpc Visualization with X3D (Michail Karpov)Michael Karpov
 

Was ist angesagt? (20)

7 streams and error handling in java
7 streams and error handling in java7 streams and error handling in java
7 streams and error handling in java
 
P threads
P threadsP threads
P threads
 
Multi-Threading
Multi-ThreadingMulti-Threading
Multi-Threading
 
Posix threads(asha)
Posix threads(asha)Posix threads(asha)
Posix threads(asha)
 
Threads - Why Can't You Just Play Nicely With Your Memory?
Threads - Why Can't You Just Play Nicely With Your Memory?Threads - Why Can't You Just Play Nicely With Your Memory?
Threads - Why Can't You Just Play Nicely With Your Memory?
 
Multithreading Presentation
Multithreading PresentationMultithreading Presentation
Multithreading Presentation
 
Intro To .Net Threads
Intro To .Net ThreadsIntro To .Net Threads
Intro To .Net Threads
 
MULTITHREADING CONCEPT
MULTITHREADING CONCEPTMULTITHREADING CONCEPT
MULTITHREADING CONCEPT
 
Correct and efficient synchronization of java thread
Correct and efficient synchronization of java threadCorrect and efficient synchronization of java thread
Correct and efficient synchronization of java thread
 
Threads - Why Can't You Just Play Nicely With Your Memory_
Threads - Why Can't You Just Play Nicely With Your Memory_Threads - Why Can't You Just Play Nicely With Your Memory_
Threads - Why Can't You Just Play Nicely With Your Memory_
 
Multithreading Concepts
Multithreading ConceptsMultithreading Concepts
Multithreading Concepts
 
2 second lesson- attributes
2 second lesson- attributes2 second lesson- attributes
2 second lesson- attributes
 
Handling I/O in Java
Handling I/O in JavaHandling I/O in Java
Handling I/O in Java
 
Java file
Java fileJava file
Java file
 
.NET: Thread Synchronization Constructs
.NET: Thread Synchronization Constructs.NET: Thread Synchronization Constructs
.NET: Thread Synchronization Constructs
 
Hpc Visualization with X3D (Michail Karpov)
Hpc Visualization with X3D (Michail Karpov)Hpc Visualization with X3D (Michail Karpov)
Hpc Visualization with X3D (Michail Karpov)
 
Io streams
Io streamsIo streams
Io streams
 
General Purpose GPU Computing
General Purpose GPU ComputingGeneral Purpose GPU Computing
General Purpose GPU Computing
 
Thread
ThreadThread
Thread
 
Hazelcast sunum
Hazelcast sunumHazelcast sunum
Hazelcast sunum
 

Ähnlich wie Freckle

Pune-Cocoa: Blocks and GCD
Pune-Cocoa: Blocks and GCDPune-Cocoa: Blocks and GCD
Pune-Cocoa: Blocks and GCDPrashant Rane
 
Processes and Threads in Windows Vista
Processes and Threads in Windows VistaProcesses and Threads in Windows Vista
Processes and Threads in Windows VistaTrinh Phuc Tho
 
Embedded Recipes 2018 - Shared memory / telemetry - Yves-Marie Morgan
Embedded Recipes 2018 - Shared memory / telemetry - Yves-Marie MorganEmbedded Recipes 2018 - Shared memory / telemetry - Yves-Marie Morgan
Embedded Recipes 2018 - Shared memory / telemetry - Yves-Marie MorganAnne Nicolas
 
Kubernetes Internals
Kubernetes InternalsKubernetes Internals
Kubernetes InternalsShimi Bandiel
 
A Domain-Specific Embedded Language for Programming Parallel Architectures.
A Domain-Specific Embedded Language for Programming Parallel Architectures.A Domain-Specific Embedded Language for Programming Parallel Architectures.
A Domain-Specific Embedded Language for Programming Parallel Architectures.Jason Hearne-McGuiness
 
AEM - Binary less replication
AEM - Binary less replicationAEM - Binary less replication
AEM - Binary less replicationAshokkumar T A
 
Aleksandr_Butenko_Mobile_Development
Aleksandr_Butenko_Mobile_DevelopmentAleksandr_Butenko_Mobile_Development
Aleksandr_Butenko_Mobile_DevelopmentCiklum
 
Search at Twitter: Presented by Michael Busch, Twitter
Search at Twitter: Presented by Michael Busch, TwitterSearch at Twitter: Presented by Michael Busch, Twitter
Search at Twitter: Presented by Michael Busch, TwitterLucidworks
 
Introduciton to Apache Cassandra for Java Developers (JavaOne)
Introduciton to Apache Cassandra for Java Developers (JavaOne)Introduciton to Apache Cassandra for Java Developers (JavaOne)
Introduciton to Apache Cassandra for Java Developers (JavaOne)zznate
 
Coding For Cores - C# Way
Coding For Cores - C# WayCoding For Cores - C# Way
Coding For Cores - C# WayBishnu Rawal
 
Containers in depth – Understanding how containers work to better work with c...
Containers in depth – Understanding how containers work to better work with c...Containers in depth – Understanding how containers work to better work with c...
Containers in depth – Understanding how containers work to better work with c...All Things Open
 
Real Time Operating System
Real Time Operating SystemReal Time Operating System
Real Time Operating SystemSharad Pandey
 

Ähnlich wie Freckle (20)

Pune-Cocoa: Blocks and GCD
Pune-Cocoa: Blocks and GCDPune-Cocoa: Blocks and GCD
Pune-Cocoa: Blocks and GCD
 
gcdtmp
gcdtmpgcdtmp
gcdtmp
 
Processes and Threads in Windows Vista
Processes and Threads in Windows VistaProcesses and Threads in Windows Vista
Processes and Threads in Windows Vista
 
Embedded Recipes 2018 - Shared memory / telemetry - Yves-Marie Morgan
Embedded Recipes 2018 - Shared memory / telemetry - Yves-Marie MorganEmbedded Recipes 2018 - Shared memory / telemetry - Yves-Marie Morgan
Embedded Recipes 2018 - Shared memory / telemetry - Yves-Marie Morgan
 
Kubernetes Internals
Kubernetes InternalsKubernetes Internals
Kubernetes Internals
 
Pthread
PthreadPthread
Pthread
 
Memory model
Memory modelMemory model
Memory model
 
Basics of C
Basics of CBasics of C
Basics of C
 
A Domain-Specific Embedded Language for Programming Parallel Architectures.
A Domain-Specific Embedded Language for Programming Parallel Architectures.A Domain-Specific Embedded Language for Programming Parallel Architectures.
A Domain-Specific Embedded Language for Programming Parallel Architectures.
 
Os
OsOs
Os
 
Design_Patterns_Dr.CM.ppt
Design_Patterns_Dr.CM.pptDesign_Patterns_Dr.CM.ppt
Design_Patterns_Dr.CM.ppt
 
Chapter 6 os
Chapter 6 osChapter 6 os
Chapter 6 os
 
AEM - Binary less replication
AEM - Binary less replicationAEM - Binary less replication
AEM - Binary less replication
 
Aleksandr_Butenko_Mobile_Development
Aleksandr_Butenko_Mobile_DevelopmentAleksandr_Butenko_Mobile_Development
Aleksandr_Butenko_Mobile_Development
 
Search at Twitter: Presented by Michael Busch, Twitter
Search at Twitter: Presented by Michael Busch, TwitterSearch at Twitter: Presented by Michael Busch, Twitter
Search at Twitter: Presented by Michael Busch, Twitter
 
Introduciton to Apache Cassandra for Java Developers (JavaOne)
Introduciton to Apache Cassandra for Java Developers (JavaOne)Introduciton to Apache Cassandra for Java Developers (JavaOne)
Introduciton to Apache Cassandra for Java Developers (JavaOne)
 
Coding For Cores - C# Way
Coding For Cores - C# WayCoding For Cores - C# Way
Coding For Cores - C# Way
 
Containers in depth – Understanding how containers work to better work with c...
Containers in depth – Understanding how containers work to better work with c...Containers in depth – Understanding how containers work to better work with c...
Containers in depth – Understanding how containers work to better work with c...
 
Scheduling Thread
Scheduling  ThreadScheduling  Thread
Scheduling Thread
 
Real Time Operating System
Real Time Operating SystemReal Time Operating System
Real Time Operating System
 

Mehr von Sasha Goldshtein

Modern Linux Tracing Landscape
Modern Linux Tracing LandscapeModern Linux Tracing Landscape
Modern Linux Tracing LandscapeSasha Goldshtein
 
The Next Linux Superpower: eBPF Primer
The Next Linux Superpower: eBPF PrimerThe Next Linux Superpower: eBPF Primer
The Next Linux Superpower: eBPF PrimerSasha Goldshtein
 
Staring into the eBPF Abyss
Staring into the eBPF AbyssStaring into the eBPF Abyss
Staring into the eBPF AbyssSasha Goldshtein
 
Visual Studio 2015 and the Next .NET Framework
Visual Studio 2015 and the Next .NET FrameworkVisual Studio 2015 and the Next .NET Framework
Visual Studio 2015 and the Next .NET FrameworkSasha Goldshtein
 
Swift: Apple's New Programming Language for iOS and OS X
Swift: Apple's New Programming Language for iOS and OS XSwift: Apple's New Programming Language for iOS and OS X
Swift: Apple's New Programming Language for iOS and OS XSasha Goldshtein
 
C# Everywhere: Cross-Platform Mobile Apps with Xamarin
C# Everywhere: Cross-Platform Mobile Apps with XamarinC# Everywhere: Cross-Platform Mobile Apps with Xamarin
C# Everywhere: Cross-Platform Mobile Apps with XamarinSasha Goldshtein
 
Modern Backends for Mobile Apps
Modern Backends for Mobile AppsModern Backends for Mobile Apps
Modern Backends for Mobile AppsSasha Goldshtein
 
Performance and Debugging with the Diagnostics Hub in Visual Studio 2013
Performance and Debugging with the Diagnostics Hub in Visual Studio 2013Performance and Debugging with the Diagnostics Hub in Visual Studio 2013
Performance and Debugging with the Diagnostics Hub in Visual Studio 2013Sasha Goldshtein
 
Mastering IntelliTrace in Development and Production
Mastering IntelliTrace in Development and ProductionMastering IntelliTrace in Development and Production
Mastering IntelliTrace in Development and ProductionSasha Goldshtein
 
Delivering Millions of Push Notifications in Minutes
Delivering Millions of Push Notifications in MinutesDelivering Millions of Push Notifications in Minutes
Delivering Millions of Push Notifications in MinutesSasha Goldshtein
 
Building Mobile Apps with a Mobile Services .NET Backend
Building Mobile Apps with a Mobile Services .NET BackendBuilding Mobile Apps with a Mobile Services .NET Backend
Building Mobile Apps with a Mobile Services .NET BackendSasha Goldshtein
 
Building iOS and Android Apps with Mobile Services
Building iOS and Android Apps with Mobile ServicesBuilding iOS and Android Apps with Mobile Services
Building iOS and Android Apps with Mobile ServicesSasha Goldshtein
 
Attacking Web Applications
Attacking Web ApplicationsAttacking Web Applications
Attacking Web ApplicationsSasha Goldshtein
 
Windows Azure Mobile Services
Windows Azure Mobile ServicesWindows Azure Mobile Services
Windows Azure Mobile ServicesSasha Goldshtein
 
First Steps in Android Development
First Steps in Android DevelopmentFirst Steps in Android Development
First Steps in Android DevelopmentSasha Goldshtein
 

Mehr von Sasha Goldshtein (20)

Modern Linux Tracing Landscape
Modern Linux Tracing LandscapeModern Linux Tracing Landscape
Modern Linux Tracing Landscape
 
The Next Linux Superpower: eBPF Primer
The Next Linux Superpower: eBPF PrimerThe Next Linux Superpower: eBPF Primer
The Next Linux Superpower: eBPF Primer
 
Staring into the eBPF Abyss
Staring into the eBPF AbyssStaring into the eBPF Abyss
Staring into the eBPF Abyss
 
Visual Studio 2015 and the Next .NET Framework
Visual Studio 2015 and the Next .NET FrameworkVisual Studio 2015 and the Next .NET Framework
Visual Studio 2015 and the Next .NET Framework
 
Swift: Apple's New Programming Language for iOS and OS X
Swift: Apple's New Programming Language for iOS and OS XSwift: Apple's New Programming Language for iOS and OS X
Swift: Apple's New Programming Language for iOS and OS X
 
C# Everywhere: Cross-Platform Mobile Apps with Xamarin
C# Everywhere: Cross-Platform Mobile Apps with XamarinC# Everywhere: Cross-Platform Mobile Apps with Xamarin
C# Everywhere: Cross-Platform Mobile Apps with Xamarin
 
Modern Backends for Mobile Apps
Modern Backends for Mobile AppsModern Backends for Mobile Apps
Modern Backends for Mobile Apps
 
.NET Debugging Workshop
.NET Debugging Workshop.NET Debugging Workshop
.NET Debugging Workshop
 
Performance and Debugging with the Diagnostics Hub in Visual Studio 2013
Performance and Debugging with the Diagnostics Hub in Visual Studio 2013Performance and Debugging with the Diagnostics Hub in Visual Studio 2013
Performance and Debugging with the Diagnostics Hub in Visual Studio 2013
 
Mastering IntelliTrace in Development and Production
Mastering IntelliTrace in Development and ProductionMastering IntelliTrace in Development and Production
Mastering IntelliTrace in Development and Production
 
Introduction to RavenDB
Introduction to RavenDBIntroduction to RavenDB
Introduction to RavenDB
 
State of the Platforms
State of the PlatformsState of the Platforms
State of the Platforms
 
Delivering Millions of Push Notifications in Minutes
Delivering Millions of Push Notifications in MinutesDelivering Millions of Push Notifications in Minutes
Delivering Millions of Push Notifications in Minutes
 
Building Mobile Apps with a Mobile Services .NET Backend
Building Mobile Apps with a Mobile Services .NET BackendBuilding Mobile Apps with a Mobile Services .NET Backend
Building Mobile Apps with a Mobile Services .NET Backend
 
Building iOS and Android Apps with Mobile Services
Building iOS and Android Apps with Mobile ServicesBuilding iOS and Android Apps with Mobile Services
Building iOS and Android Apps with Mobile Services
 
Task and Data Parallelism
Task and Data ParallelismTask and Data Parallelism
Task and Data Parallelism
 
What's New in C++ 11?
What's New in C++ 11?What's New in C++ 11?
What's New in C++ 11?
 
Attacking Web Applications
Attacking Web ApplicationsAttacking Web Applications
Attacking Web Applications
 
Windows Azure Mobile Services
Windows Azure Mobile ServicesWindows Azure Mobile Services
Windows Azure Mobile Services
 
First Steps in Android Development
First Steps in Android DevelopmentFirst Steps in Android Development
First Steps in Android Development
 

Kürzlich hochgeladen

The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????blackmambaettijean
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Ryan Mahoney - Will Artificial Intelligence Replace Real Estate Agents
Ryan Mahoney - Will Artificial Intelligence Replace Real Estate AgentsRyan Mahoney - Will Artificial Intelligence Replace Real Estate Agents
Ryan Mahoney - Will Artificial Intelligence Replace Real Estate AgentsRyan Mahoney
 
Visualising and forecasting stocks using Dash
Visualising and forecasting stocks using DashVisualising and forecasting stocks using Dash
Visualising and forecasting stocks using Dashnarutouzumaki53779
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 

Kürzlich hochgeladen (20)

The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
What is Artificial Intelligence?????????
What is Artificial Intelligence?????????What is Artificial Intelligence?????????
What is Artificial Intelligence?????????
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Ryan Mahoney - Will Artificial Intelligence Replace Real Estate Agents
Ryan Mahoney - Will Artificial Intelligence Replace Real Estate AgentsRyan Mahoney - Will Artificial Intelligence Replace Real Estate Agents
Ryan Mahoney - Will Artificial Intelligence Replace Real Estate Agents
 
Visualising and forecasting stocks using Dash
Visualising and forecasting stocks using DashVisualising and forecasting stocks using Dash
Visualising and forecasting stocks using Dash
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 

Freckle

  • 1. ∵ FRECKLE ∴ Lock-Free Usage of Standard STL Containers* * (almost)
  • 2. Time Constrains • Very advanced talk – limited time – Must assume prior knowledge and skim a lot! • Assume: – Familiarity with reference counting smart-pointers – Basic understanding of threading concepts. • No need for C++11 compiler – Boost provides all missing functionality (v1.48 and up)
  • 3. Time Constrains • No time to go deep into: – C++11 • std::shared_ptr<> • Lock-based facilities – e.g. std::mutex • New memory model • Move semantics – r-values – Concurrent programming • Lock-based programming – Pitfalls: dead-locks, data-races, resource release problems • Lock-free programming – Pitfalls: hard, tricky, subtle, platform-specific hardware support (e.g. CAS), ABA etc.
  • 4. A Problem I Had… • A highly concurrent server system • Must update a shared vector of in-memory objects while these objects are being used by other threads. • Cannot afford locking the whole vector • Problem: How to update this shared vector without: – Delaying other threads – Causing multithreaded havoc such as data- races, deadlocks, memory leaks or premature release.
  • 5. The Ecosystem • Concurrent hardware becoming pervasive: – Hyper-Threading – Multiple cores – [GPGPUs, cloud…] • True parallelism – not just time-sharing • Demand for high performance and fast response is growing – think web-servers • One way to maximize the power of parallelism is: Lock-Free Data-Structures!
  • 6. Lock-free Programming WARNING: Lock-free and wait-free data-structures and algorithms are HARD to write! • Some wise advice: “Use Locks!” Tony Van Eerd, BoostCon 2011 presentation: “The Basics of Lock-free Programming” • Consider lock-free algorithms as a last resort.
  • 7. Lock-Free Programming • Non-blocking algorithms • Guaranteed system-wide progress at all times. • No locks used – No dead-locks – Reduced contention = improved hardware utilization • Generally, use atomic hardware read-modify-write primitives – These are like single HW instruction “critical sections” • A lot of research in recent years = many new lock-free data- structures. However, implementations are: – Often designed for particular use-cases – Not always portable (platform-dep. atomic support) – Not always well-tested, more proof-of-concept – Not always suitably licensed for commercial use.
  • 8. FRECKLE* • A general scheme for providing portable lock-free access to, and modification of, standard STL containers. • Can be easily adapted to non-STL containers. • Leverages new standard atomic reference-counting guarantees to provide automatic garbage collection. • Uses only standard C++11 components. But: • It is not a replacement for more specialized lock-free containers, but in many cases may suffice. • There is no free-lunch, and FRECKLE entails various trade- offs and certain usage conditions that will be detailed. * Poor-man’s ‘LoCKFREE’ acronym, a distant cousin of the Pimpl.
  • 9. The FRECKLE Setup • Provide shared access to a container of shared objects. • Useful for – Frequent map lookups with infrequent map updates. – Optimize greedy vector-traversal algorithms • Use only a single object that is shared between the multiple threads. • This shared object is a std::shared_ptr to an STL container of shared objects.
  • 10. What does “shared” mean? Two meanings: 1. A shared variable: A variable that can be seen concurrently by multiple threads, this ‘shared’ applies to the instance of the shared_ptr itself 2. In the shared_ptr type name: Refers to the fact that multiple objects may be “pointing” to the same data (from one or more threads) – this is the pointer abstraction. This refers to the data pointed to by the shared_ptr instance.
  • 11. The FRECKLE Setup • Use a single shared shared_ptr to an STL container of shared objects. • The container is a container of shared_ptrs to the desired data type. • The data-type can be: – Arbitrarily large – Does not even need to be CopyConstructible, CopyAssignable, and LessThanComparable and thus not even generally allowed for direct use within standard containers, it may also be an incomplete type. – A polymorphic type like an abstract interface using namespace std; shared_ptr< vector< shared_ptr< MyAbstractInterface const>>> sharedObjects; shared_ptr< vector< shared_ptr< atomic<int>>>> atomicCounters; shared_ptr< map < size_t, shared_ptr< int const>>> sharedMap;
  • 12. When/Where Can FRECKLE Help? 1. Multiple concurrent readers that use only const access to the container. – Readers do not modify the shared shared_ptr to the container; – Readers do not modify the container itself; – Readers should not, generally, modify the container data, unless this data is safe (read: designed for) for concurrent modification (e.g. using std::atomic<>). 2. One or more writers such that: – Writer(s) may modify the shared shared_ptr to the container; – Writer(s) may modify the container itself (e.g. insert, remove, sort, resize); – Writer(s) should not, generally, modify the container data, unless this data is safe for concurrent modification (e.g. std::atomic<>) or by using the approach mentioned below. • Intuitively, FRECKLE provides shared access to a container of shared objects via “virtual snapshots”. • These “virtual snapshots” do not in-fact incur any access runtime overheads.
  • 13. C++11 Facilities – shared_ptr • std::shared_ptr<> – A replacement for raw pointers for dynamic data management – Reference counted smart-pointer – Automatic deleting of data upon ref-count == 0 in dtor (RAII) – Use std::make_shared() instead of new. – Supports: • Deleters, weak_ptr, use in containers …
  • 14. const Access to shared_ptr is Atomic • From the Boost shared_ptr documentation about thread-safety: – shared_ptr objects offer the same level of thread safety as built-in types. – A shared_ptr instance can be "read" (accessed using only const operations) simultaneously by multiple threads. – Different shared_ptr instances can be "written to" (accessed using mutable operations such as operator= or reset) simultaneously by multiple threads (even when these instances are copies, and share the same reference count underneath.) – Any other simultaneous accesses result in undefined behavior. • shared_ptr<int> p(make_shared<int>(42)); // the shared shared_ptr //--- Example 1 --- // thread A shared_ptr<int> p2(p); // reads p // thread B shared_ptr<int> p3(p); // OK, multiple reads are safe • p is in shared memory and visible to both threads A and B. • Objects p2 and p3 are local variables and are not (necessarily) shared or visible to the other thread. • In legalese, section §20.7.2.2 of the C++11 ISO standard states: – For purposes of determining the presence of a data race, member functions shall access and modify only the shared_ptr [...] objects themselves and not objects they refer to. Changes in use_count() do not reflect modifications that can introduce data races. • TAKE HOME MESSAGE: as long as there are no concurrent non-use_count() changes, the single reference-count of p, p2 and p3, will always be properly, i.e. atomically, adjusted, with no data-races.
  • 15. Atomic Access to a shared_ptr • To modify a shared_ptr instance (item 4), we must use special atomic functions provided by the standard. Section §20.7.2.5 of the standard states: – Concurrent access to a shared_ptr object from multiple threads does not introduce a data race if the access is done exclusively via the functions in this section and the instance is passed as their first argument. • Of particular interest in this case are: – atomic_store(), atomic_compare_exchange(), atomic_load() • atomic_store allows the atomic update of a shared shared_ptr object to share the same ref-count and ref-object as another (e.g. thread-local) shared_ptr instance. • atomic_compare_exchange is a more generalized form of atomic_store. • atomic_load allows the atomic creation of a (e.g. thread-local) shared_ptr instance which shares the same ref-count and ref-object of another shared shared_ptr instance even in the presence of possible concurrent modification of this shared shared_ptr instance.
  • 16. FRECKLE – Single Writer class MyDataClass; typedef vector<shared_ptr<MyDataClass const>> Container; shared_ptr<Container> freckle; // The reader thread(s) void reader() { shared_ptr<Container const> p = atomic_load( &freckle ); // use *p; } // The single writer case void writer() { // copy container shared_ptr<Container> p(make_shared<Container>(*freckle)); // update *p reflecting new information; atomic_store( &freckle, move( p ) ); }
  • 17. FRECKLE – Multiple Writers class MyDataClass; typedef vector<shared_ptr<MyDataClass const>> Container; shared_ptr<Container> freckle; // The reader thread(s) void reader() { shared_ptr<Container const> p = atomic_load( &freckle ); // use *p; } // The multiple writers case void writer() { shared_ptr<Container> p = atomic_load( &freckle ); shared_ptr<Container> q; do { // copy container q = make_shared<Container>( *p ); // update *q reflecting new information; } while( !atomic_compare_exchange( &freckle, &p, move(q ))); }
  • 18. atomic_compare_exchange() • The function atomic_compare_exchange() does all of the following atomically: – If p and freckle are equivalent – i.e. pointing to the same data, then: • freckle is updated to point at q’s data - the newly created copy. Just like with the single reader case. • Else: p is updated to point at the updated data pointed to by freckle. This update is equivalent to an internal atomic_load like on the first line. // The multiple writers case void writer() { shared_ptr<Container> p = atomic_load( &freckle ); shared_ptr<Container> q; do { // copy container q = make_shared<Container>( *p ); // update *q reflecting new information; } while( !atomic_compare_exchange( &freckle, &p, move(q ))); }
  • 19. Exception Safety Provides the Strong Exception Safety Guarantee: The operation has either completed successfully or thrown an exception, leaving the program state exactly as it was before the operation started. All operations are in one of the following categories: 1. const access to shared data - no change in the program state. 2. Modifications only of stack-local non-shared variables and data - no change to the rest of the program state. 3. Only atomic non-throwing operations actually alter shared data.
  • 20. Additional Notes • Why (*almost)? • Move semantics • Primitive Data Types • Further reading: – C++ Concurrency in Action Anthony Williams
  • 21. Questions ∴ Adi Shavit ∵ adishavit@gmail.com ∴ 050-7637223