The ABCs of thread safety

Post on 02-Jan-2016

24 views 3 download

description

The ABCs of thread safety. 郑宜霖. 0 thread. 0.0 what are threads. 0.1 why threads. 1 thread “unsafety”. 1.0 Race condition. 1.1 CASE I: read-increase-write. 1.2 CASE II: Removing a Node. 2 Mutex. 2.0 Mutual Exclusion. 2.1 Increment-Mutex. 2.2 Sounds good?. Hardware I nterrupt Signal. - PowerPoint PPT Presentation

Transcript of The ABCs of thread safety

THE ABCS OF THREAD SAFETY郑宜霖

0 THREAD

0.0 WHAT ARE THREADS

0.1 WHY THREADS

1 THREAD “UNSAFETY”

1.0 RACE CONDITION

1.1 CASE I: READ-INCREASE-WRITE

1.2 CASE II: REMOVING A NODE

2 MUTEX

2.0 MUTUAL EXCLUSION

2.1 INCREMENT-MUTEX

2.2 SOUNDS GOOD?

•Hardware Interrupt

•Signal

2.3 REENTRANCY 

Lock the Mutex

Interrupt Call increment()

Unable to LockSome Operations

Unlock the Mutex

3 ATOMIC OPERATION

3.0 ATOMIC OPERATIONS

3.1 INCREMENT-ATOMIC

3.2 STD::ATOMIC

3.3 HOW DO THEY DO IT?

• Hardware Support for Atomic Operations (adding, incrementing, subtracting, etc.)

• Support for CAS (hardware / software)

3.4 COMPARE-AND-SWAP

3.5 INCREMENT-CAS

3.6 WHAT’S MORE

•Only CAS with 1, 2 ,4 ,8 Bytes

•How to implement an atomic class?

REFERENCES

• GStreamer Application Development Manual

• Wikipedia

• Cppreference

• GCC Wiki

THANKS