Needing a QEventLoop for a kioslave

Thiago Macieira thiago at kde.org
Wed Feb 11 07:35:07 GMT 2009


Friedrich W. H. Kossebau wrote:
>What to do then with static QObjects created by K_GLOBAL_STATIC inside
> the 3-party code? It is said all QObject created in the thread have to
> be deleted before the thread ends. So if I wrap the execution of the
> non-gui (KDE) library code in a thread not the main
> QCoreApplication::exec() one, can/will K_GLOBAL_STATIC bind them to the
> thread somehow?

Well, you're out of luck. You can ask the 3rd-party developer to make his 
library thread-safe.

A global static QObject means it's not thread-safe. Like the GUI parts of 
Qt. But ioslaves aren't GUI-enabled, so asking for thread-safety is 
reasonable.

A side note: QObject classes in global statics is a bad idea, regardless 
of how many threads you're actually using. Keeping QObjects alive past 
QCoreApplication's demise will cause all sorts of havoc with the internal 
QThreadData (it's only deleted when the last QObject is deleted), which in 
turn may cause problems for properly disposing of QThreadStorage objects.

Also note that destructors of global-static objects have to be *extremely* 
careful, because many subsystems may already be shut down, like the 
Local8Bit conversion in QString.

>I for now tried a q'n'd approach I found for the JabberDisco in Kopete.
> But it does not work for me, is this non-ported KDE 3 code or is there
> a flaw?
>
>--- 8< ---
>bool breakEventLoop = false;
>
>class EventLoopThread : public QThread
>{
>public:
>	void run ();
>};
>
>void EventLoopThread::run ()
>{
>	while ( true )
>	{
>		qApp->processEvents ();
>		msleep ( 100 );
>
>		if ( breakEventLoop )
>			break;
>	}
>}
>
>void JabberDiscoProtocol::dispatchLoop ()
>{
>	EventLoopThread eventLoopThread;
>
>	eventLoopThread.start ();
>	SlaveBase::dispatchLoop ();
>	breakEventLoop = true;
>	eventLoopThread.wait ();
>}
>--- 8< ---

You forgot the "volatile" part in the breakEventLoop variable.

-- 
  Thiago Macieira  -  thiago (AT) macieira.info - thiago (AT) kde.org
    PGP/GPG: 0x6EF45358; fingerprint:
    E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20090211/e7123568/attachment.sig>


More information about the kde-core-devel mailing list