start a thread from a callback and signal its termination

Jan Pohanka xhpohanka at gmail.com
Wed Sep 5 10:35:01 CEST 2012


Hello,

I have a newbie question regarding libev and threads. I'm implementing  
simple video server. It should receive some requests from another  
applications and then perform for example video capturing and encoding.

Initial implementation of a server listening to socket is very easy using  
available libev examples. So no I have a main loop and a callback function  
which parses the command received through socket and calling particular  
functions.

static void socket_cb(EV_P_ ev_io *w, int revents)
{
     int sock;
     VS_cmd cmd;
     int ret;

     sock = w->fd;

     ret = recv(sock, &cmd, sizeof(cmd), 0);

     switch (cmd.type) {
     case VS_REC_VIDEO:
         record_video_file(&cmd.arg.video_request);
         break;
     case VS_SET_FORMAT:
         set_video_format(&cmd.arg.format);
         break;
     case VS_SET_FPS:
         set_video_rate(&cmd.arg.time_base);
         break;
     case VS_RECODE:
         video_recode("test.mkv", "testr.mkv");
         break;
     default:
         printf("unknown command\n");
         break;
     }
}

int main(int argc, char *argp[])
{
     int sock;
     struct ev_loop *loop;
     ev_io socket_watcher;

     sock = start_server(6001);
     if (sock < 0) {
         exit(1);
     }

     loop = EV_DEFAULT;
     ev_io_init(&socket_watcher, socket_cb, sock, EV_READ);
     ev_io_start(loop, &socket_watcher);

     video_init();

     ev_run(loop, 0);

     video_exit();

     return 0;
}

In case of the video capturing this can take really long time. My function  
video_capture(...)  creates three threads (one for capturing, one for  
encoding and one for writing to a disc) and can record e.g. 60 secs of a  
video. And here is my question: how should I start this function in a  
separate thread (to allow the callback to return) and how to signal this  
thread termination to libev mainloop. I'm fairly new to this type of  
programming so excuse me if it is not a libev issue and I should to that  
in some another way. But I'd like to be for example to handle a situation  
when the video recording is enabled and a client should have a chance to  
prolong the video.

best regards
Jan

-- 
Tato zpráva byla vytvořena převratným poštovním klientem Opery:  
http://www.opera.com/mail/



More information about the libev mailing list