[Qt-interest] question about threads in console application

Anton Chernov mechernov at gmail.com
Tue Mar 22 20:35:26 CET 2011


Ehmm... guess it depens on your handler - if you have a custom one
then the thread safety is in your hands. The default one (on windows)
is

Q_CORE_EXPORT void qWinMsgHandler(QtMsgType t, const char* str)
{
    Q_UNUSED(t);
    // OutputDebugString is not threadsafe.

    // cannot use QMutex here, because qWarning()s in the QMutex
    // implementation may cause this function to recurse
    static QWinMsgHandlerCriticalSection staticCriticalSection;

    if (!str)
        str = "(null)";

    staticCriticalSection.lock();

    QString s(QString::fromLocal8Bit(str));
    s += QLatin1Char('\n');
    OutputDebugString((wchar_t*)s.utf16());

    staticCriticalSection.unlock();
}

2011/3/22 franki <franki at franki.eu.org>:
> Tuesday 22 of March 2011 13:41:11 Anton Chernov napisał(a):
>> A clean main function sounds great! And if all other functions are
>> clean too you are programming with qt!
>>
>> The thing is that you get a reaction on a event from the other thread
>> only in the qApp->processEvents(); If you do some heavy stuff you can
>> call that method to see if there is some processing to do.
>
> So in my case if i have handler for qDebug function defined and installed
> inside main(), then every thread that tries to use qDebug must wait until
> function from main have finished, because qDebug call is processed as an
> event inside main qApp right?
>
> Marek
>
>>
>> To make it clean take a look at QtConcurrent.
>>
>> 2011/3/22 franki <franki at franki.eu.org>:
>> > Hi,
>> >
>> > I'm building console application with several threads. So far I was
>> > creating every object inside main() function and then I moved some of
>> > them to their threads, and some stayed in main thread.
>> > I have also installed handler for qDebug, qCritical and so one, inside
>> > main() function like this:
>> > qInstallMsgHandler(myMessageOutput);
>> >
>> > So now if I'm doing some heavy computing stuff inside object created in
>> > main() function, every other thread waits for it, till it finishes, even
>> > though they are in separate threads with signal/slot connection type
>> > Qt::QueuedConnection.
>> >
>> > So I have two questions
>> > 1. Should I do another class like "MyAppClass" and create myAppClass
>> > object inside main() function, so the function main() would be much
>> > cleaner, and then inside myAppClass object I would create other objects
>> > and their threads, make signal/slot connection and so one.
>> >
>> > 2. Should I create myAppThread inside mian() function (or myAppClass
>> > object) and move every other objects (that don't have their own thread
>> > yet) to "myAppThread" so in that way I leave first thread entirely for
>> > qtApplication. As I observed, when I do that, threads don't wait for each
>> > other while they are using qDebug which is, as I assume, from
>> > qtApplication thread. Also if I do explicitly sleep (which is wrong of
>> > course) in some thread (not qtApplication thread) other threads don't
>> > wait for that thread.
>> >
>> > So in simple words: is it worth of effort to:
>> > 1. maintain clean main() function
>> > 2. maintain mainthread only for qtApplication and rest of the objects
>> > inside separate thread/threads
>> >
>> > best regards
>> > Marek
>> > _______________________________________________
>> > Qt-interest mailing list
>> > Qt-interest at qt.nokia.com
>> > http://lists.qt.nokia.com/mailman/listinfo/qt-interest
>
>
>



More information about the Qt-interest-old mailing list