23
THE ABCS OF THREAD SAFETY 郑郑郑

The ABCs of thread safety

Embed Size (px)

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

Citation preview

Page 1: The ABCs of  thread safety

THE ABCS OF THREAD SAFETY郑宜霖

Page 2: The ABCs of  thread safety

0 THREAD

Page 3: The ABCs of  thread safety

0.0 WHAT ARE THREADS

Page 4: The ABCs of  thread safety

0.1 WHY THREADS

Page 5: The ABCs of  thread safety

1 THREAD “UNSAFETY”

Page 6: The ABCs of  thread safety

1.0 RACE CONDITION

Page 7: The ABCs of  thread safety

1.1 CASE I: READ-INCREASE-WRITE

Page 8: The ABCs of  thread safety

1.2 CASE II: REMOVING A NODE

Page 9: The ABCs of  thread safety

2 MUTEX

Page 10: The ABCs of  thread safety

2.0 MUTUAL EXCLUSION

Page 11: The ABCs of  thread safety

2.1 INCREMENT-MUTEX

Page 12: The ABCs of  thread safety

2.2 SOUNDS GOOD?

•Hardware Interrupt

•Signal

Page 13: The ABCs of  thread safety

2.3 REENTRANCY 

Lock the Mutex

Interrupt Call increment()

Unable to LockSome Operations

Unlock the Mutex

Page 14: The ABCs of  thread safety

3 ATOMIC OPERATION

Page 15: The ABCs of  thread safety

3.0 ATOMIC OPERATIONS

Page 16: The ABCs of  thread safety

3.1 INCREMENT-ATOMIC

Page 17: The ABCs of  thread safety

3.2 STD::ATOMIC

Page 18: The ABCs of  thread safety

3.3 HOW DO THEY DO IT?

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

• Support for CAS (hardware / software)

Page 19: The ABCs of  thread safety

3.4 COMPARE-AND-SWAP

Page 20: The ABCs of  thread safety

3.5 INCREMENT-CAS

Page 21: The ABCs of  thread safety

3.6 WHAT’S MORE

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

•How to implement an atomic class?

Page 22: The ABCs of  thread safety

REFERENCES

• GStreamer Application Development Manual

• Wikipedia

• Cppreference

• GCC Wiki

Page 23: The ABCs of  thread safety

THANKS