Discussion about "Threads, Events and QObjects" article
-
wrote on 20 Dec 2010, 15:17 last edited by
It is not just Qt terminology. It's general programming terminology and something everyone who does at least the slightest bit of multi-threading should know about.
http://en.wikipedia.org/wiki/Reentrant_(subroutine)
http://en.wikipedia.org/wiki/Thread_safety -
wrote on 20 Dec 2010, 15:53 last edited by
[quote author="Franzk" date="1292858262"]...and something everyone who does at least the slightest bit of multi-threading should know about.[/quote] thx for the WP references :)
BTW: I did some multithreaded coding without problems, and without thinking about reentrance.
-
wrote on 20 Dec 2010, 15:54 last edited by
To be honest, the little problem is that there might be some confusion due to literature and/or other toolkits. That's why I specified that in the article I follow the Qt conventions; anyway, I added a link to http://doc.qt.nokia.com/latest/threads-reentrancy.html, just to make it even more clear :-)
-
wrote on 20 Dec 2010, 15:59 last edited by
It's good. The basics are the same across libraries though (bad library if it diverges...).
-
wrote on 21 Dec 2010, 09:12 last edited by
After reading "Reentrancy and Thread-Safety":http://doc.qt.nokia.com/latest/threads-reentrancy.html , I think the term reentrance is not the best choice, because re-entering (in a sense of entering it twice) isn't really possible. (My problem seems to be that I'm familiar with the non-reentrance of MS-DOS.)
Classes that can be safely used by different threads at different times, I would name just safe. To be honest, I would not discuss it at all, but rather mark those that cannot be used from different threads at different times, maybe as "tread-local" or so.
Am I completely wrong here?
-
wrote on 21 Dec 2010, 09:20 last edited by
Hi Wolf,
Thread-local is normally used for members/memory. So there is the "ThreadLocalStorage":http://en.wikipedia.org/wiki/Thread-local_storage for example. "Reentrant":http://en.wikipedia.org/wiki/Reentrant_(subroutine) and "thread-safety":http://en.wikipedia.org/wiki/Thread_safety are general terms (from my understanding) which are widely used. So I would stay with the used terms. -
wrote on 21 Dec 2010, 09:36 last edited by
[quote author="Wolf P." date="1292922748"]
Classes that can be safely used by different threads at different times, I would name just safe. To be honest, I would not discuss it at all, but rather mark those that cannot be used from different threads at different times, maybe as "tread-local" or so.Am I completely wrong here?[/quote]
There are three possible cases:
- Classes/methods/objects/functions/data structures which (...whose instances) can be used at the same time from multiple threads, without the need of serializing cuncurrent accesses. That's what thread-safe means.
- Classes/methods/objects/functions/data structures which (...whose instances) cannot be used at the same time from multiple threads, therefore all accesses must be externally serialized. That's what reentrant means. Notice that
** Thread-safe implies reentrant
** Taking a reentrant class and forcibly serializing all possible accesses with a mutex makes it thread-safe - Classes/methods/objects/functions/data structures which (...whose instances) cannot be used from multiple threads at all. There isn't a specific name for this case (we usually say "not thread-safe nor reentrant"). For instance, QWidget and all of its subclasses are usable only from the main thread.
-
wrote on 21 Dec 2010, 10:48 last edited by
Ok. The term reentrant (in the given context) is now clear to me.
But please note the following example: when you call the Win32 function SendMessage (sending to another process) and get reply-blocked, your process can be re-entered by SendMessage calls from other processes. So, for me, reentrance (in general) has also something to do with recursion.
-
wrote on 21 Dec 2010, 11:40 last edited by
I added a toc to the page. (and to the "wiki syntax help":http://developer.qt.nokia.com/wiki/WikiSyntax too)
-
wrote on 21 Dec 2010, 12:01 last edited by
Unfortunately, the term "reentrancy" is not really clearly defined in computer world.
Michael Suess complains in his "blog entry:"http://www.thinkingparallel.com/2007/06/08/the-most-overused-word-in-parallel-programming-reentrancy/ about the situation. Reading the comments, it seems that there are at least two definitions of reentrancy in the context of single threading (regarding recursive function calls) and in the context of multi threading. This may confuse the people with a single threading background (DOS!) when heading over to multithreaded programming.
Anyways, the definitions are out in the wild and as long as we are in Qt context, we should use the terms defined by the Trolls to avoid further confusion. Otherwise we would need another round of BabelFishing for these kinds of things, but I doubt there's any T-Shirts to win :-)
-
wrote on 21 Dec 2010, 12:43 last edited by
[quote author="Volker" date="1292932902"]but I doubt there's any T-Shirts to win :-)[/quote]Huh, imagine T-shirts stating something about your re-entrancy...
-
wrote on 21 Dec 2010, 12:49 last edited by
[quote author="Franzk" date="1292935405"][quote author="Volker" date="1292932902"]but I doubt there's any T-Shirts to win :-)[/quote]Huh, imagine T-shirts stating something about your re-entrancy...
[/quote]What a about
"I'm a male - I'm not thread safe!"
[Edit - ok, a bit offtopic now :-) Volker]
-
wrote on 21 Dec 2010, 12:51 last edited by
"I am NOT re-entrant"
-
wrote on 30 Dec 2010, 03:28 last edited by
Qt also requires that all objects living in a thread are deleted before the QThread object that represents the thread is destroyed; this can be easily done by creating all the objects living in that thread on the QThread::run() method’s stack.
Do you mean that
this can be easily done by deleting all the objects living in that thread on the QThread::run() method’s stack.
? -
wrote on 30 Dec 2010, 08:31 last edited by
Object created on the stack of QThread::run() should get destroyed automatically, when it goes out of scope.
-
wrote on 30 Dec 2010, 10:14 last edited by
[quote author="changsheng230" date="1293679684"]Qt also requires that all objects living in a thread are deleted before the QThread object that represents the thread is destroyed; this can be easily done by creating all the objects living in that thread on the QThread::run() method’s stack.
Do you mean that
this can be easily done by deleting all the objects living in that thread on the QThread::run() method’s stack.
?[/quote]No: I mean that if you do
@
MyThread::run()
{
Object obj1, obj2, obj3;
OtherObject foo, bar;
/* ... */
}
@All those objects will:
- be created on run()'s stack;
- be living in the "MyThread" thread;
- get automatically destroyed immediately before run() returns (thus, terminating the thread).
-
wrote on 30 Dec 2010, 11:02 last edited by
Remember that instance will be a dangling pointer after run() returns. One solution
would be the use of smartpointer.@
MyThread::run()
{
Class* instance = new Class;
/* ... */
}
@ -
wrote on 4 Jan 2011, 08:47 last edited by
Another solution would be, to create objects as children of objects that will be destoyed. (But choose objects for parentship that reside in the same thread.)
-
wrote on 23 Feb 2011, 21:48 last edited by
There seem to be two copies of this article now. This one: https://developer.qt.nokia.com/wiki/Threads_Events_QObjects has a revision history going back to Pepe's original post on 10 Dec through Alexandra's name change on 10 Feb. This one: https://developer.qt.nokia.com/wiki/ThreadsEventsQObjects was posted by Volker on 23 Feb under the old name. Based on a quick doc compare, I think the content of the two are identical except for the title.
@Volker: are there other changes in your 23 Feb edit that I'm not seeing? I'd like to consolidate the two back to the (new) name.
-
wrote on 23 Feb 2011, 21:53 last edited by
The two articles are identical. My "version" (that without the underscores) is only a link to the actual article. The reason is, that the old version of the link is referred in some other articles and in a blog entry.
Unfortunately it does not redirect to the actual article but pulls in its content and it does not leave a message of doing so.