[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    Re: threading in kde
From:       Andrew Sutton <ansutton () sep ! com>
Date:       2001-09-10 15:21:04
[Download RAW message or body]

> It does. Read $QTDIR/doc/html/threads.html.

oops :)

>
> The problem is more that the basic toolclasses (QString and QPtrList
> esp.) aren't thread-safe (for obvious performance reasons). The main
> problem is that they're implicitely shared. So you have to be very
> careful not to use them in different threads concurrently.

i don't imagine that its any less implicitly shared than say, the stl, and 
that stuff works fine in a threaded environment, as long as precautions are 
taken to protect shared resources when writing. yea, developers are going to 
have to take concurrency issues into account when developing threaded apps, 
but when isn't that the case - regardless of what toolkits are being used. 

> But if we really restrict the use of threads to "one GUI and event
> thread and else only worker threads", it's quite obvious which objects
> need mutex protection - all the ones possibly accessed by the worker
> threads. If you keep the worker threads simple, so stay the locking
> issues and the overhead generated by that.

its my personaly opinion that many of the underlying design patterns (lists, 
maps, strings) shouldn't have to take thread safety into account with the 
exception of reentrance - which is entirely different than thread safe IMHO. 
but what you say isn't necessarily true unless you're writing to those 
objects - in which case you can protect access to the underlying shared 
resources with a mutator and a mutex. a perfect example is a message queue.

class KMessageQueue< T >
{
public:
	void put( KMessage *msg )
	{
		mutex.lock();
		q.push( msg );
		mutex.unlock();
	}

	KMessage *get()
	{
		KMessage *ret;
		mutex.lock();
		ret = q.front();
		q.pop();
		mutex.unlock();
		return ret;
	}

protected:
	QMutex mutex;
	QQeue< KMessage * > q;
};

issues solved :) maybe somebody should write a programmer's guide to writing 
mult-threaded applications for KDE that discusses the basic concepts of 
threaded applications, how to protect shared resources, and the tools 
available in qt and kde that a) need protection or b) offer protection.

Andrew Sutton
ansutton@sep.com
 
>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic