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 now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Qt 6 C++ GUI Programming Cookbook

You're reading from   Qt 6 C++ GUI Programming Cookbook Practical recipes for building cross-platform GUI applications, widgets, and animations with Qt 6

Arrow left icon
Product type Paperback
Published in Apr 2024
Publisher Packt
ISBN-13 9781805122630
Length 428 pages
Edition 3rd Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Lee Zhi Eng Lee Zhi Eng
Author Profile Icon Lee Zhi Eng
Lee Zhi Eng
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Chapter 1: Look-and-Feel Customization with Qt Designer FREE CHAPTER 2. Chapter 2: Event Handling – Signals and Slots 3. Chapter 3: States and Animations with Qt and QML 4. Chapter 4: QPainter and 2D Graphics 5. Chapter 5: OpenGL Implementation 6. Chapter 6: Transitioning from Qt 5 to Qt 6 7. Chapter 7: Using Network and Managing Large Documents 8. Chapter 8: Threading Basics –Asynchronous Programming 9. Chapter 9: Building a Touch Screen Application with Qt 6 10. Chapter 10: JSON Parsing Made Easy 11. Chapter 11: Conversion Library 12. Chapter 12: Accessing Databases with SQL Driver and Qt 13. Chapter 13: Developing Web Applications Using Qt WebEngine 14. Chapter 14: Performance Optimization 15. Index 16. Other Books You May Enjoy

Data protection and sharing data between threads

Even though multithreading makes processes run asynchronously, there will be times when threads must stop and wait for other threads. This usually happens when both threads modify the same variable simultaneously. It is common to force threads to wait for one another to protect shared resources, such as data. Qt 6 also provides both low-level methods and high-level mechanisms to synchronize threads.

How to do it…

We will continue to use the code from the previous example project, since we have already established a working program with multithreading:

  1. Open up myworker.h and add the following header:
    #include <QObject>
    #include <QDebug>
    #include <QMutex>
  2. Then, we will add two new variables and make some changes to the class constructor:
    public:
        explicit MyWorker(QMutex *mutex);
        int* myInputNumber;
        QMutex* myMutex;
    signals:
    ...
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 €18.99/month. Cancel anytime