本页内容

线程支持

线程支持命令用于指定类或函数对多线程编程的支持级别。支持级别共有三种:threadsafe 、reentrant 和nonreentrant 。

默认级别为nonreentrant ,这意味着相关类或函数不能被多个线程调用。Reentrant 和threadsafe 这两个级别主要用于类。

Reentrant 表示被引用的类中的所有函数均可被多个线程同时调用,前提是每个函数调用引用的数据都是唯一的。而threadsafe 则表示,即使每个函数调用引用的数据是共享的,被引用的类中的所有函数仍可被多个线程同时调用。

当一个类被标记为 \reentrant 或 \threadsafenonreentrant 时,该类中的函数可通过 \nonreentrant 命令将该类中的函数标记为xml-ph-0000@deepl.internal。

示例

/*!
    \class QLocale
    \brief The QLocale class converts between numbers and their
    string representations in various languages.

    \reentrant
    \ingroup i18n
    \ingroup text

    QLocale is initialized with a language/country pair in its
    constructor and offers number-to-string and string-to-number
    conversion functions similar to those in QString.

    ...

    \nonreentrant

    Sets the global default locale to \a locale. These values are
    used when a QLocale object is constructed with no
    arguments. If this function is not called, the system's locale
    is used.

    \warning In a multithreaded application, the default locale
    should be set at application startup, before any non-GUI
    threads are created.

    \sa system(), c()
*/
void QLocale::setDefault(const QLocale &locale)
{
    default_d = locale.d;
}

当类被声明为可重入时,QDoc 会生成一条通知,并列出例外情况(即声明为不可重入的函数)。 其中包含指向关于可重入性和线程安全的一般文档的链接。此外,在不可重入函数的文档中会生成一条警告:“警告:此函数不可重入。”

当类被声明为线程安全时,QDoc 也会生成相同的通知和警告。

有关更多信息,请参阅关于可重入性和线程安全性的通用文档。

命令

\threadsafe

\threadsafe 命令在文档中包含一行说明,用于表明相关的类或函数是线程安全的,即使多个线程的独立调用引用了共享数据,也能被同时调用。

该命令必须独占一行。

通过此命令生成的文档将与为 \reentrant 命令生成的文档类似。请参阅引言部分中的示例。

另请参阅 \reentrant 以及 \nonreentrant。

\reentrant

\reentrant 命令表示,只要每次调用都引用其自身的数据,则相关类或函数可被多个线程同时调用。参见上文示例。

该命令必须独占一行。

另请参阅 \nonreentrant 以及 \threadsafe。

\nonreentrant

\nonreentrant 指令表示关联的类或函数不能被多个线程调用。非重入是默认情况。

该命令必须单独成行。

当类被标记为 \reentrant 或 \threadsafenonreentrant 时,该类中的函数可通过在 \fn 注释中使用此命令,将xml-ph-0000@deepl.internal标记为应被排除的函数。

另请参阅 \reentrant 以及 \threadsafe。

© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.