Wednesday, 10 April 2024
So, 2023 was a successful year for Qt - and we highlight the Qt 6.5 and Qt 6.6 releases, Qt World Summit, and Qt Contributor Summit.
Our community stays vibrant and engaged with the Qt Project through our forums, mailings lists, asking and answering questions, offering technical advice, reporting bugs, contributing patches, and in many other forms helping Qt flourish.
Tuesday, 9 April 2024
C++11线程特性
std::thread
C++11引入了std::thread来创建线程,支持对线程join或者detach.
1 |
|
1 | class ThreadGuard |
c++11还提供了获取线程id,或者系统cpu个数,获取thread native_handle,使得线程休眠等功能
1 | std::thread t(func); |
std::mutex
std::mutex是一种线程同步的手段,用于保存多线程同时操作的共享数据。
mutex分为四种:
std::mutex:独占的互斥量,不能递归使用,不带超时功能
std::recursive_mutex:递归互斥量,可重入,不带超时功能
std::timed_mutex:带超时的互斥量,不能递归
std::recursive_timed_mutex:带超时的互斥量,可以递归使用
std::mutex
1 |
|
std::timed_mutex
1 |
|
std::lock相关
这里主要介绍两种RAII方式的锁封装,可以动态的释放锁资源,防止线程由于编码失误导致一直持有锁。
c++11主要有std::lock_guard和std::unique_lock两种方式,使用方式都类似,如下:
1 | #include <iostream> |
std::lock_guard
1 |
|
Mutex 是互斥量的类型。
使用 std::lock_guard 时,只需在需要保护的代码块中创建一个 std::lock_guard 对象,并将需要保护的互斥量传递给它的构造函数。当 std::lock_guard 对象创建时,会自动锁定互斥量,当对象销毁时,会自动解锁互斥量。
std::unique_lock
1 |
|
Mutex 是互斥量的类型。
与 std::lock_guard 不同,std::unique_lock 对象可以在构造时不锁定互斥量,并且可以在后续的代码中手动锁定或解锁。此外,std::unique_lock 还提供了更多的功能,如可延迟锁定、条件变量的支持等。
std::atomic
c++11提供了原子类型std::atomic,理论上这个T可以是任意类型,但是我平时只存放整形,别的还真的没用过,整形有这种原子变量已经足够方便,就不需要使用std::mutex来保护该变量啦。看一个计数器的代码:
1 | struct OriginCounter { // 普通的计数器 |
std::call_once
c++11提供了std::call_once来保证某一函数在多线程环境中只调用一次,它需要配合std::once_flag使用,直接看使用代码吧:
1 | std::once_flag onceflag; |
volatile
volatile通常用来建立内存屏障,volatile修饰的变量,编译器对访问该变量的代码通常不再进行优化,看下面代码:
1 | int *p = xxx; |
a和b都等于p指向的值,一般编译器会对此做优化,把*p的放入寄存器,之后a,b都等于寄存器的值,但是如果把中间p地址的值改变,内存值改变了,但a,b还是从寄存器中取的值(不一定,看编译器的优化结果),这不符合需求,所以对p加上volatile修饰可以避免此类优化.
注意:volatile不能解决多线程安全问题,针对特种内存才需要使用volatile,它和atomic的特点如下:
- std::atomic用于多线程访问的数据,且不用互斥量,用于并发编程中
- volatile用于读写操作不可以被优化掉的内存,用于特种内存中
std::condition_variable
条件变量是c++11引入的一种同步机制,它可以阻塞一个线程或者个线程,直到有线程通知或者超时才会唤醒正在阻塞的线程,条件变量需要和锁配合使用,这里的锁就是上面介绍的std::unique_lock。
1 | class CountDownLatch { |
notify_one- 通知一个线程等待
notify_all- 通知所有线程等待
wait- 阻塞当前线程,直到条件变量被唤醒
wait_for- 阻塞当前线程,知道条件变量被唤醒或指定的超时时间后
wait_until- 阻塞当前线程,直到条件变量被唤醒或到达指定时间点
std::future
c++11关于异步操作提供了future相关的类,主要有std::future、std::promise和std::packaged_task,std::future比std::thread高级些,std::future作为异步结果的传输通道,通过get()可以很方便的获取线程函数的返回值,std::promise用来包装一个值,将数据和future绑定起来,而std::packaged_task则用来包装一个调用对象,将函数和future绑定起来,方便异步调用。而std::future是不可以复制的,如果需要复制放到容器中可以使用std::shared_future。
std::promise与std::future配合使用
1 |
|
std::packaged_task与std::future配合使用
1 |
|
std::future用于访问异步操作的结果,而std::promise和std::packaged_task在future高一层,它们内部都有一个future,promise包装的是一个值,packaged_task包装的是一个函数,当需要获取线程中的某个值,可以使用std::promise,当需要获取线程函数返回值,可以使用std::packaged_task。
async
async是比future,packaged_task,promise更高级的东西,它是基于任务的异步操作,通过async可以直接创建异步的任务,返回的结果会保存在future中,不需要像packaged_task和promise那么麻烦,关于线程操作应该优先使用async,看一段使用代码:
1 |
|
async具体语法如下:
1 | async(std::launch::async | std::launch::deferred, func, args...); |
第一个参数是创建策略:
- std::launch::async表示任务执行在另一线程
- std::launch::deferred表示延迟执行任务,调用get或者wait时才会执行,不会创建线程,惰性执行在当前线程。
如果不明确指定创建策略,以上两个都不是async的默认策略,而是未定义,它是一个基于任务的程序设计,内部有一个调度器(线程池),会根据实际情况决定采用哪种策略。
若从 std::async 获得的 std::future 未被移动或绑定到引用,则在完整表达式结尾, std::future的析构函数将阻塞直至异步计算完成,实际上相当于同步操作:
1 | std::async(std::launch::async, []{ f(); }); // 临时量的析构函数等待 f() |
注意:关于async启动策略这里网上和各种书籍介绍的五花八门,这里会以cppreference为主。
有时候我们如果想真正执行异步操作可以对async进行封装,强制使用std::launch::async策略来调用async。
1 | template <typename F, typename... Args> |
Sunday, 7 April 2024
Friday, 5 April 2024
KDE had a feature a lot of people didn’t know about. You could run a command when a notification triggered. The feature wasn’t very well documented and nobody really blogged about it.
However with the release of KDE Plasma 6, the feature was removed. I learned about it by accident, as it is tracked by Bug #481069. I really need the feature and I re-implemented it in KDE Plasma 6. I will be available in KDE Plasma 6.1 and KDE Frameworks 6.1.
Text-to-Speech for calendar events
I’m using the “Run a command” feature for calendar events. Normally you get a popup notification. The popup notification is small and pop up where all of them are shown. When I’m concentrated and working on some code I simply miss them. If I play a game, I miss them.
The solution for me is to use a Text to Speech (TTS) Engine. I’ve setup speech-dispatcher with piper-tts on my system. When an a reminder triggers it says: “Andreas, you have an appointment in 10 minutes: Samba Meeting”.
You can find the python script I use here.
Endless possibilities
The opportunities for doing cool stuff with this are endless. Here are some ideas what else you could do:
- Start your meeting/conferencing application prior to the meeting
- Change the desktop activity before a meeting
- Lock the screen if a specific WiFi gets disconnected
If you have some nice idea or already used it in the past, leave a comment to let me know what else you can do.
CI & CD Infrastructure
- Merge requests run the custom build and the CI builds
- Master runs the CI build (though there could be other things we run, such as CVE scans)
- Craft recipes are run from the last commit of the master or release branch, they require "build" and "build-and-test-stable" to be run manually beforehand.
- Publishing to Microsoft store is available after the Windows Craft is run.
Donut Buster
Custom Views
A view is a collection of settings: the orientation of the sky map, how the orientation changes as the sky map is panned, whether it is mirrored or not, and optionally the field-of-view to set the map to.
If no views are defined, KStars introduces a set of standard / "demo" views by default. Existing views can be edited and new views can be added using the "Edit Views..." interface. They can also be re-ordered in the interface. The ordering of the views in the "Edit Views..." dialog defines the order in which views will be cycled through using the keyboard shortcuts Shift + Page Up and Shift + Page Down. Thus, you can set up the views for easily switching between naked eye / finder scope / telescope views for easy star-hopping.
The rotation feature overlay now also marks East in addition to north and zenith, so as to know easily whether the display is mirrored or not.
Blinking
In Analyze, one can now move from one session to the next (forward or backward).
Keyboard shortcuts are provided for that.
Another set of keyboard shortcuts both advance and show the next image in the same FITS Viewer.
Thus, for example, one can advance through all the captured images of the evening, showing all the captures on the FITS Viewer by repeating a keyboard shortcut.
A useful complement to this might be adding the ability to delete bad captures, but for now that will have to wait for a rainy day.
Going along with the above, keyboard shortcuts have been added to move to the next or previous FITS Viewer tab, Also helpful to the above is a new command to zoom in/out all tabs (not just the current one).
There is a new Blink Directory menu command (in both menus, as above) which will open a single tab with a list of all the images below the directory selected (that is, both in that directory, and in directories below it). It initially displays the first image, but new commands work in that tab to switch to displaying the next (or previous) image file in the list. This could be used to blink hundreds of files.
Sky Flats
Scheduler Refactor
Standalone Editor
- Target
- Sequence File
Thursday, 4 April 2024
If you have used a moderately complex application there are chances that you have interacted with what is called a “modal” dialog. A modal dialog is a dialog that requires you to close/address it before you can continue interacting with the main application window. This can be implemented by the application in a straightforward manner but compositor didn’t know if a dialog was modal or not.
That is until now the new xdg-dialog-v1 protocol
allows applications to mark their
dialogs as modal or not modal. This allows the compositor to adapt its behavior
according to this hint. For example when trying to activate the main window it can
activate the modal dialog instead. It also enables KWin to use the darkening effect
on the parent window on Wayland.
I implemented support for the protocol into KWin and Qt which will be part of the Plasma 6.1 and Qt 6.8 releases respectively. The protocol was created from functionality in GTK and Mutter by Carlos Garnacho and I am happy seeing the overall Wayland eco-system now being able to benefit from it.
Tuesday, 2 April 2024

With this blog post, we introduce the QtQuickComputeItem - a Qt Quick item that allows you to easily integrate compute shader into your Qt Quick Code.
Compute
Shader are used to perform arbitrary computations on the GPU. For
example, the screenshot below shows a Qt Quick application that
generates Gray Scott Reaction Diffusion patterns. The simulation is executed by a compute shader that is configured directly in QML.
Continue reading Use Compute Shader in Qt Quick at basysKom GmbH.
Monday, 1 April 2024
Marknote 1.1.0 is out! Marknote is the new WYSIWYG note-taking application from KDE. Despite the latest release being just a few days ago, we have been hard at work and added a few new features and, more importantly, fixed some bugs.
Marknote now boasts broader Markdown support, and can now display images and task lists in the editor. And once you are done editing your notes, you can export them to various formats, including PDF, HTML and ODT.
Marknote’s interface now seamlessly integrates the colors assigned to your notebooks, enhancing its visual coherence and making it easier to distinguish one notebook from another. Additionally, your notebooks remember the last opened note, automatically reopening it upon selection.
We’ve also introduced a convenient command bar similar to the one in Merkuro. This provides quick access to essential actions within Marknote. Currently it only creates a new notebook and note, but we plan to make more actions available in the future. Finally we have reworked all the dialogs in Markdown to use the newly introduced FormCardDialog from KirigamiAddons.
We have created a small feature roadmap with features we would like to add in the future. Contributions are welcome!
Packager section
You can find the package on download.kde.org and it has been signed with my GPG key.
Note that this release introduce a new recommanded dependencies: md4c and require the latest Kirigami Addons release (published a few hours ago).












