Re: Two-step object initialization (using a thread)
On May 27, 8:25 pm, "vl106" <vl...@hotmail.com> wrote:
I have the following problem: a function shall return a complex object.
The caller shall receive the object immediately (e.g. without blocking).
The real initialization is done by a worker thread (as being
time-consuming).
I came up with the following solution. Any better ideas?
[code snipped]
I read your code and the other posts in this group. I would like to
offer a word of caution:
There are certain unqualified concepts that are fundamental to
synchronization, which is what you are dealing with the moment that
you mention threads. There are some people who would have you believe:
1. that there is a type of threading that does not involve multi-
threading
2. that there is some type of synchronization magic that does not
require synchronization primitives.
3. other wishful ideas that general fall into the Hoare schoole of
thought
These are all simply wrong.
Every few years I get into an argument with someone (usually a Hoare
fan) who swears that some type of lock-free thingy has been invented.
I ask for proof. They point me to some paper written by a researcher
(the last one was from Princeton), that professes not-yet-discovered
magic that does not require traditional mutexes, etc. I will read the
paper, and invariably, buried at the bottom of say, page 8, the author
will write something like...
"...and as you can see, my super duper scheme uses no synchronization
primitives at all. All that is required is a singular, very tiny
[insert equivalent for kernel-mode fast mutex, spin-lock, or other
real synchro primitive], but except for that..."
As soon as they make this qualification, all bets are off. Period.
So, while it is not clear what your goal is from you post, I must
emphasize that if you want locking, you need a lock. Use a mutex. You
probably need to an event too.
Algorithm:
1. Get pointer to thing to be defined.
2. Spawn thread T that will define the thing.
3. Block on an event until T signals that thing has been defined.
4. Access thing.
There is no magic.
-Le Chaud Lapin-
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]