🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #5958

Re: Callback mechanics

#5958
From: Andrue Cope [TeamB]
Date: Wed, 22 Jun 2005 08:31
46 lines
3100 bytes
AlexB wrote:

> As I understand, in this case callback works in context of another
> thread, right?

Yes. The callback is being called from another thread. Remy's original
answer is technically correct but omits (I think <g>) to mention that
some API calls can result in a thread being created to call the
callback.


The generic and simple answer to your question is that you have no
control over when a callback is going to be called. Even if it's called
within the context of your thread there's no guarantee when that will
happen. The function taking a callback may only be storing it for later
to save having to ask for it all the time.

My own library does this. It's common to pass a callback to a ctor or
processing start function then have that called throughout the lifetime
of the object or the processing rather than have to pass a callback on
every method call or processing related function.

In later code we actually use a callback /class/ and that helps
concentrate the mind to avoid getting caught out. The class has
'CallBack_Starting' and 'CallBack_Ended' methods to tell the class when
its owner is going to start making callback calls and when no further
callback calls will occur.

When you hand a callback to something you are telling that 'something'
where to find some executable code. Only the 'something' can decide
when the callback will be called.

If that 'something' calls the callback before it returns to you then
the code will be executed in your thread. But if the 'something'
returns control to you without calling the callback or if it states (as
'timeSetEvent' does) that it will call the callback later it becomes
less clear and you need to study the documentation.

My advice is to always ensure that callbacks are thread safe. Not only
that but if at all possible make them entirely self contained.
Unfortunately the latter is often impossible but making them thread
safe goes a long way to resolving problems.
-- 
Andrue Cope [TeamB]
[Bicester, Uk]
http://info.borland.com/newsgroups/guide.html
Technical Details
Message-ID: <42b921d4$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!news-out.newsfeeds.com!propagator3-LAX.newsfeeds.com!news-in.spamkiller.net!news-in.superfeed.net!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <42b7fc1d$1@newsgroups.borland.com> <42b84cd0$1@newsgroups.borland.com> <42b8e174$1@newsgroups.borland.com> <42b90490@newsgroups.borland.com> <42b916e4$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,bcabb6910c82b605
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Andrue Cope [TeamB]" <no.spam@not.a.valid.address>
Subject: Re: Callback mechanics
Newsgroups: borland.public.cppbuilder.nativeapi
References: <42b7fc1d$1@newsgroups.borland.com> <42b84cd0$1@newsgroups.borland.com> <42b8e174$1@newsgroups.borland.com> <42b90490@newsgroups.borland.com> <42b916e4$1@newsgroups.borland.com>
Date: Wed, 22 Jun 2005 09:31:16 +0100
User-Agent: XanaNews/1.17.5.3
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
NNTP-Posting-Host: 217.207.223.34
Message-ID: <42b921d4$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1119429076 217.207.223.34 (22 Jun 2005 01:31:16 -0700)
Lines: 46
X-Authenticated-User: acope
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!news-out.newsfeeds.com!propagator3-LAX.newsfeeds.com!news-in.spamkiller.net!news-in.superfeed.net!caladan.landsraad.net!newsgroups.borland.com!not-for-mail