callback_function\", my code:" />
开发者

callbacks question

Basically im trying to make a dispatcher, but it fails because it's always "!event->callback_function", my code:

#include "event.h"
#include "memory.h"
#include "thread.h"

#include <stdio.h>
#include <time.h>
#include <assert.h>

bool running;

typedef struct Event {
    event_callback_t cb;
    time_t delay;
    void* p;
    struct Event* next;
} Event;

Event* g_events;

void _remove_event           __P((Event**, Event *));
void event_dispatch_internal        __P(());
void add_event_internal             __P((Event**, Event *));

void
event_dispatch()
{
    g_events = (Event *)MyMalloc(sizeof(*g_events));
    create_thread((callback_t)event_dispatch_internal, (void *)NULL);
}

void
add_event_internal(Event** events, Event* event)
{
    event->next = *events;
    *events = event;
}

void
add_event(callback, param, delay)
    event_callback_t callback;
    void *param;
    time_t delay;
{
    Event* event;
    event = (Event *)MyMalloc(sizeof(*event));
    assert(0 != event);

    event->delay = time(NULL) + delay;
    event->p = param;
    event->cb = callback;
    add_event_internal(&g_events, event);
}

void
_remove_event(Event** events, Event* event)
{
    event = *events;
    *events = event->next;
}

void
event_dispatch_internal()
{
#ifdef _DEBUG
    fprintf(stderr, "Events started\n");
#endif
    while (true) {
        Event* tmp;
        for (tmp = g_events; tmp; tmp = tmp->next) {
            if (time(NULL) >= tmp->delay) {
                tmp->cb(tmp->p);
#ifdef _DEBUG
                fprintf(stderr, "Executed event %p:%u\n", (void *)tmp, (unsigned int)tmp->delay);
#endif
                _remove_event(&g_events, tmp);
            }
        }
    }
}

it crashes but when i do it like that:

            for (tmp = g_events; tmp; tmp = tmp->next) {
            if (time(NULL) >= tmp->delay) {
                if (!tmp->cb) {
                    tmp->开发者_高级运维;cb(tmp->p);
#ifdef _DEBUG
                    fprintf(stderr, "Executed event %p:%u\n", (void *)tmp, (unsigned int)tmp->delay);
#endif
                } else {
                    fprintf(stderr, "Couldnt execute event %p:%u\n", (void *)tmp, (unsigned int)tmp->delay);
                }
            }
        }

it always gives "Couldn't execute event blabla"

while i call it like that:

void test_(void *);

void
test_(void *p)
{
    fprintf(stderr, "test(): %d\n", *(int *)p);
}

int main()
{
    int test;
    test = 5;
    event_dispatch();
    add_event(test_, (void *)&test, 1);
    do { } while (1);
    return 0;
}

any help is apperciated


Your code doesn't make any sense. You fire off a thread, which then loops forever trying to walk the g_events list. However, at startup, that just has a single, uninitialised node in it, so anything could happen!

Furthermore (1), you have no synchronization between your threads, so even if you fix the above problem, you're likely to get into nasty race hazards when people try to add events.

Furthermore (2), both your threads are effectively in "busy-wait" loops, which will suck your CPU dry. You need to investigate a mechanism that causes your threads to sleep until something arrives, such as semaphores.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜