views:

65

answers:

3

Hello

gcc 4.4.3 c89

I have a event loop that runs in a separate thread.

My design is like this below, just sample code to help explain.

I need to somehow wait for the initialization to complete before I can make a call to the get_device_params.

I did put a usleep for 3 seconds just before the call to the get_device_params, but I don't really want to block.

Many thanks for any suggestions,

void* process_events(void *data)
{
    switch(event_type)
    {
        case EVT_INITIALIZED:
            /* Device is now initialized */    
        break;
    }
}

int main(void)
{
    /* Create and start thread and process incoming events */
    process_events();

    /* Initialize device */
    initialize_device(); 

    /* Get device parameters */
    /* However, I cannot run this code until initialization is complete */
    get_device_params();

    return 0;
}
+4  A: 

If this separate thread is a POSIX thread (i.e. you're on a typical UNIX platform), then you can use pthread conditional variables.

You call pthread_cond_wait() in the waiting thread. When the init thread finishes its work, you call pthread_cond_signal(). In my opinion that's a canonical way to wait for initialization in another thread.

Tadeusz A. Kadłubowski
+2  A: 

I need to somehow wait for the initialization to complete before I can make a call to the get_device_params.

Since you apparently have some sort of a FSM inside the process_events(), and it why ever runs in a separate thread, you shouldn't do anything from the main thread with the device.

In other words, logically, call to the get_device_params(); should be placed inside the FSM, on the event that the device is initialized EVT_INITIALIZED which I presume is triggered by the initialize_device().

Alternatively, you can create second FSM (possibly in another thread) and let the process_events() (the first FSM) after it has finished its own processing, forward the EVT_INITIALIZED event to the second FSM. (Or initialize_device() could send the event to the both FSMs simultaneously.)

To me it seems (from the scarce code you have posted) that your problem is that you try to mix sequential code with an event based one. Rule of thumb: in event/FSM based application all code should run inside the FSM, being triggered by an event; there should be no code which may run on its own outside of the FSM.

Dummy00001
+1  A: 

If it were me, I would probably use a barrier. In main you can call pthread_barrier_init, indicating that you have 2 threads. Then, in main call pthread_barrier_wait, to wait on the barrier you initialized, after calling your device initialization function. Finally, in the device thread, after you initialize your device, you can call pthread_barrier_wait on the same barrier and when both threads are waiting, the barrier will have been satisfied, so both threads will continue. I find barriers easier to use than condition variables sometime, but I'm sure that's an issue of preference.

Brandon