Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > c event handling - how?

Reply
Thread Tools

c event handling - how?

 
 
Phui Hock
Guest
Posts: n/a
 
      11-21-2003
Hi,
How do I generate an event and got it handled by a handler? I know
how to do it in C++ or Java but I got no idea how to do it in C. What
is the best approach to write a function that will take a function
pointer, a pointer to user data (what is it?) and then I notify it
when a certain event happens?

I'm new to C, btw

Thanks a lot for helping.
 
Reply With Quote
 
 
 
 
Mark A. Odell
Guest
Posts: n/a
 
      11-21-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Phui Hock) wrote in
news:(E-Mail Removed) om:

> Hi,
> How do I generate an event and got it handled by a handler? I know
> how to do it in C++ or Java but I got no idea how to do it in C. What
> is the best approach to write a function that will take a function
> pointer, a pointer to user data (what is it?) and then I notify it
> when a certain event happens?


You mean call back? I don't think that you can in C. There are no threads,
interrtups, or tasks in C. You can see if setjmp/longjmp() are of interest
but I doubt standard C will allow you to do what you want without
platform-specific extensions (which are off-topic here).

--
- Mark ->
--
 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      11-21-2003
Mark A. Odell wrote:

> (E-Mail Removed) (Phui Hock) wrote in
> news:(E-Mail Removed) om:
>
>
>>Hi,
>>How do I generate an event and got it handled by a handler? I know
>>how to do it in C++ or Java but I got no idea how to do it in C. What
>>is the best approach to write a function that will take a function
>>pointer, a pointer to user data (what is it?) and then I notify it
>>when a certain event happens?

>
>
> You mean call back? I don't think that you can in C.


Well, you can.

> There are no threads,


Correct

> interrtups,


Sort of wrong. Hardware level interrupts aren't supported as such, but an
abstraction of interrupts (as a general concept) /is/ supported.

> or tasks in C. You can see if setjmp/longjmp() are of interest
> but I doubt standard C will allow you to do what you want without
> platform-specific extensions (which are off-topic here).


See the signal() and raise() functions in the Standard, along with signal.h

--
Lew Pitcher, IT Consultant, Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)

 
Reply With Quote
 
Mark A. Odell
Guest
Posts: n/a
 
      11-21-2003
Bruno Desthuilliers <(E-Mail Removed)> wrote in
news:3fbe71eb$0$9292$(E-Mail Removed):

>> You mean call back?

>
> What the OP asks for include, but is not restricted to, callbacks.
>
>> I don't think that you can in C.

>
> Do what ? callbacks ? yes you can. Have a look at the (standard) qsort()
> function. It takes a callback function as lsat argument.


Not what I meant, of course you can pass a function a pointer to a
function but the function you just called with this pointer can't call you
back without recursion. By callback I meant called by some other task or
an interrupt service routine. Apparently signal() and raise() can do some
level of this as Lew pointed out.

--
- Mark ->
--
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      11-21-2003
Mark A. Odell wrote:
> (E-Mail Removed) (Phui Hock) wrote in
> news:(E-Mail Removed) om:
>
>
>>Hi,
>>How do I generate an event and got it handled by a handler? I know
>>how to do it in C++ or Java but I got no idea how to do it in C. What
>>is the best approach to write a function that will take a function
>>pointer, a pointer to user data (what is it?) and then I notify it
>>when a certain event happens?

>
>
> You mean call back?


What the OP asks for include, but is not restricted to, callbacks.

> I don't think that you can in C.


Do what ? callbacks ? yes you can. Have a look at the (standard) qsort()
function. It takes a callback function as lsat argument.

(snip)

Bruno

 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      11-21-2003
Phui Hock wrote:

> How do I generate an event and get it handled by a handler?
> I know how to do it in C++ or Java
> but I have no idea how to do it in C.


Show us how you would do it in C++.

> What is the best approach to write a function
> that will take a function pointer,
> a pointer to user data (what is it?)
> and then I notify it when a certain event happens?


Show us an example of what you mean in C++.

 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      11-21-2003
Mark A. Odell wrote:

> Bruno Desthuilliers <(E-Mail Removed)> wrote in
> news:3fbe71eb$0$9292$(E-Mail Removed):
>
>
>>>You mean call back?

>>
>>What the OP asks for include, but is not restricted to, callbacks.
>>
>>
>>>I don't think that you can in C.

>>
>>Do what ? callbacks ? yes you can. Have a look at the (standard) qsort()
>>function. It takes a callback function as lsat argument.

>
>
> Not what I meant, of course you can pass a function a pointer to a
> function but the function you just called with this pointer can't call you
> back without recursion. By callback I meant called by some other task or
> an interrupt service routine. Apparently signal() and raise() can do some
> level of this as Lew pointed out.


Specifically, signal() accepts a pointer to the function to be "called
back", and a value indicating the signal that will invoke the callback.

raise() accepts a value that will be used as the value of a signal. When
raise() is invoked, the signal is "sent", and asychronously, the callback
function associated to that signal by a previous signal() call will be called.

There are limitations on what the callback function can and cannot do, so
don't go coding complex logic into it (i.e. no printf() statements, etc.).



--
Lew Pitcher, IT Consultant, Application Architecture
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)

 
Reply With Quote
 
Alex
Guest
Posts: n/a
 
      11-21-2003
E. Robert Tisdale <(E-Mail Removed)> wrote:
> Phui Hock wrote:


>> How do I generate an event and get it handled by a handler?
>> I know how to do it in C++ or Java
>> but I have no idea how to do it in C.


> Show us how you would do it in C++.


Not here.

>> What is the best approach to write a function
>> that will take a function pointer,
>> a pointer to user data (what is it?)
>> and then I notify it when a certain event happens?


> Show us an example of what you mean in C++.


Not here.

Alex
 
Reply With Quote
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      11-21-2003
Mark A. Odell wrote:
> Bruno Desthuilliers <(E-Mail Removed)> wrote in
> news:3fbe71eb$0$9292$(E-Mail Removed):
>
>
>>>You mean call back?

>>
>>What the OP asks for include, but is not restricted to, callbacks.
>>
>>
>>>I don't think that you can in C.

>>
>>Do what ? callbacks ? yes you can. Have a look at the (standard) qsort()
>>function. It takes a callback function as lsat argument.

>
>
> Not what I meant, of course you can pass a function a pointer to a
> function


I sure guess you know this !-)

I just wanted to clarify that point for the OP and C beginners reading this.

(sorry, I s/c/sh/ould have stated this more explicitely).

Bruno

 
Reply With Quote
 
PH
Guest
Posts: n/a
 
      11-22-2003
>By callback I meant called by some other task or
> an interrupt service routine.


What I've learned from another is to create a table and map events to
function pointers (array or linked list). So I do this:-

typedef void (*callback) (void *);
struct event_handlers {
callback cb;
struct event_handlers *next;
} listeners[SIZE];

And I have an event registration function like this:-
void register_event(int event, callback cb) ...

When an event happens:-
event_handlers *handler = listener[event]; /* event is mapped to
event_handers */
for(; handler != NULL; handler = handler->next)
handler->cb(data);

I also learned that I can create a hash table to map events to function
pointers. As the number of new events grow, we can realloc to accommodate
those.

Is this the best approach?

>Apparently signal() and raise() can do some
> level of this as Lew pointed out.


With signal() and raise(), I can't pass my own data to the target function
(except via global variable, which I think, is ugly).


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
signal handling and (structured) exception handling Peter C++ 34 10-17-2009 10:03 AM
python list handling and Lisp list handling Mark Tarver Python 22 04-26-2009 09:36 PM
Is faster handling hexadecimal values than handling chars? IƱaki Baz Castillo Ruby 1 04-15-2008 09:04 AM
ASP.NET Event handling. Makarand ASP .Net 0 07-24-2003 01:46 PM
Pager and Event Handling sunil ASP .Net 2 07-18-2003 09:08 PM



Advertisments