Is there a way to determine if PtInit() has been called already? Or is it ok
to call PtInit() multiple times?
Thanks
Markus
Markus Loffler <loffler@ces.clemson.edu> wrote:
Is there a way to determine if PtInit() has been called already? Or is it ok
to call PtInit() multiple times?
It is OK to call it multiple times, but only the first call will
succeed. (Or, more accurately – at most one of the calls will
succeed…)
It is not OK though to call PtInit() while another thread may be
executing PtInit() already. Is that perhaps what you had in mind?
–
Wojtek Lerch (wojtek@qnx.com) QNX Software Systems Ltd.
No this is exactly what I need, just from one thread, and the second time
won’t succeed.
Thanks
Markus
“Wojtek Lerch” <wojtek@qnx.com> wrote in message
news:99ahpi$kqp$1@nntp.qnx.com…
Markus Loffler <> loffler@ces.clemson.edu> > wrote:
Is there a way to determine if PtInit() has been called already? Or is
it ok
to call PtInit() multiple times?It is OK to call it multiple times, but only the first call will
succeed. (Or, more accurately – at most one of the calls will
succeed…)It is not OK though to call PtInit() while another thread may be
executing PtInit() already. Is that perhaps what you had in mind?–
Wojtek Lerch (> wojtek@qnx.com> ) QNX Software Systems Ltd.
If you wanted to explicitly handle the situation in code, rather than
relying on the undocumented (albeit logical) behaviour of PtInit(), you
could call PtInit from a function you register with pthread_once().
This would be a more “computer sciency” way of doing things as it is
explicit and guaranteed to work even if the undocumented behaviour of
PtInit changes.
-----Original Message-----
From: Markus Loffler [mailto:loffler@ces.clemson.edu]
Posted At: Wednesday, March 21, 2001 7:52 AM
Posted To: photon
Conversation: PtInit
Subject: Re: PtInit
No this is exactly what I need, just from one thread, and the second
time
won’t succeed.
Thanks
Markus
“Wojtek Lerch” <wojtek@qnx.com> wrote in message
news:99ahpi$kqp$1@nntp.qnx.com…
Markus Loffler <> loffler@ces.clemson.edu> > wrote:
Is there a way to determine if PtInit() has been called already? Or
is
it ok
to call PtInit() multiple times?
It is OK to call it multiple times, but only the first call will
succeed. (Or, more accurately – at most one of the calls will
succeed…)It is not OK though to call PtInit() while another thread may be
executing PtInit() already. Is that perhaps what you had in mind?–
Wojtek Lerch (> wojtek@qnx.com> ) QNX Software Systems
Ltd.
Rennie Allen <RAllen@csical.com> wrote:
If you wanted to explicitly handle the situation in code, rather than
relying on the undocumented (albeit logical) behaviour of PtInit(), you
could call PtInit from a function you register with pthread_once().
This will only help if all the possible calls to PtInit() in his process
are protected in the same way. But in a single-threaded application,
pthread_once() is overkill – a simple Boolean flag is all you need.
In a multi-threaded application, pthread_once() doesn’t do the trick,
either. If PtInit() has been called already, you probably need to call
PtEnter() instead.
This would be a more “computer sciency” way of doing things as it is
explicit and guaranteed to work even if the undocumented behaviour of
PtInit changes.
Or, we could document the behaviour to make sure that it doesn’t change.
-----Original Message-----
From: Markus Loffler [mailto:> loffler@ces.clemson.edu> ]
Posted At: Wednesday, March 21, 2001 7:52 AM
Posted To: photon
Conversation: PtInit
Subject: Re: PtInit
No this is exactly what I need, just from one thread, and the second
time
won’t succeed.
Thanks
Markus“Wojtek Lerch” <> wojtek@qnx.com> > wrote in message
news:99ahpi$kqp$> 1@nntp.qnx.com> …
Markus Loffler <> loffler@ces.clemson.edu> > wrote:
Is there a way to determine if PtInit() has been called already? Or
is
it ok
to call PtInit() multiple times?It is OK to call it multiple times, but only the first call will
succeed. (Or, more accurately – at most one of the calls will
succeed…)It is not OK though to call PtInit() while another thread may be
executing PtInit() already. Is that perhaps what you had in mind?
–
Wojtek Lerch (wojtek@qnx.com) QNX Software Systems Ltd.
This will only help if all the possible calls to PtInit() in his
process
are protected in the same way.
Correct.
But in a single-threaded application,
pthread_once() is overkill – a simple Boolean flag is all you need.
True, but it was not clear to me from his post that his application was
not multi-threaded.
In a multi-threaded application, pthread_once() doesn’t do the trick,
either. If PtInit() has been called already, you probably need to
call
PtEnter() instead.
I would imagine that all threads would always do PtEnter() (i.e.
pthread_once(PtInit), PtEnter();). Would this not work ?
Rennie Allen <RAllen@csical.com> wrote:
…
In a multi-threaded application, pthread_once() doesn’t do the trick,
either. If PtInit() has been called already, you probably need to
call
PtEnter() instead.I would imagine that all threads would always do PtEnter() (i.e.
pthread_once(PtInit), PtEnter();). Would this not work ?
I guess it will – but only because PtEnter() is guaranteed to fail
(rather than crash or something) if you call it when you already have
the lock.
Still, I can’t think of many possible cases where you would really have
to do that rather than just call PtInit() once before spawning your
threads…
–
Wojtek Lerch (wojtek@qnx.com) QNX Software Systems Ltd.
Still, I can’t think of many possible cases where you would really
have
to do that rather than just call PtInit() once before spawning your
threads…
Absolutely. I was by no means suggesting that the use of pthread_once()
is good design for new code (it is a kludge for legacy code), only that
in a multi-threaded program, it is better to be explicit about the fact
that you have a kludge rather than having an implicit kludge based on
the undocumented behaviour of a third party library (nobody reading only
the code and the widget lib docs would be able to determine that the
code was correct - barring comments in the code).
Even if the behaviour of PtInit was documented, pthread_once would be
preferable since it is in effect a flag that says “I have a kludge
here”, whilst simply calling PtInit does not (this is all assuming
multi-threaded code of course).
Rennie