Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Mastering Qt 5

You're reading from   Mastering Qt 5 Create stunning cross-platform applications

Arrow left icon
Product type Paperback
Published in Dec 2016
Publisher Packt
ISBN-13 9781786467126
Length 526 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Guillaume Lazar Guillaume Lazar
Author Profile Icon Guillaume Lazar
Guillaume Lazar
Robin Penea Robin Penea
Author Profile Icon Robin Penea
Robin Penea
Arrow right icon
View More author details
Toc

Table of Contents (20) Chapters Close

Mastering Qt 5
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Preface
1. Get Your Qt Feet Wet 2. Discovering QMake Secrets FREE CHAPTER 3. Dividing Your Project and Ruling Your Code 4. Conquering the Desktop UI 5. Dominating the Mobile UI 6. Even Qt Deserves a Slice of Raspberry Pi 7. Third-Party Libraries Without a Headache 8. Animations - Its Alive, Alive! 9. Keeping Your Sanity with Multithreading 10. Need IPC? Get Your Minions to Work 11. Having Fun with Serialization 12. You Shall (Not) Pass with QTest 13. All Packed and Ready to Deploy 14. Qt Hat Tips and Tricks

Flying over Qt multithreading technologies


Built upon QThread, several threading technologies are available in Qt. First, to synchronize threads, the usual approach is to use a mutual exclusion (mutex) to have a mutual exclusion for a given resource. Qt provides it by means of the QMutex class. Its usage is straightforward:

QMutex mutex; 
int number = 1; 
 
mutex.lock(); 
number *= 2; 
mutex.unlock(); 

From the mutex.lock() instruction, any other thread trying to lock the mutex will wait until mutex.unlock() has been called.

The locking/unlocking mechanism is error-prone in complex code. You can easily forget to unlock a mutex in a specific exit condition, causing a deadlock. To simplify this situation, Qt provides a QMutexLocker that should be used where the QMutex needs to be locked:

QMutex mutex; 
QMutexLocker locker(&mutex); 
 
int number = 1; 
number *= 2; 
if (overlyComplicatedCondition) { 
    return; 
} else if (notSoSimple) { 
    return; 
} 

The mutex is locked when the locker...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime
Visually different images