Posix Queue De-queuer/Consumer in C++

Last post we read about daemon, which posting message in queue. In this post we write code about consumer who consume those messages. you can use daemon as consumer( look daemon code). we need to use signal processing and mq_notify from posix mqueue. we used old mqueue routines from old post(Posix queue)
Consumer application.

int main() { update_notification(); while(1) pause(); }

Used Code

#include <signal.h>
/* Thread start function */
static void my_sig_handler(union sigval sv) { mqd_t mqid = *((mqd_t *) sv.sival_ptr); std::string strmsg; while(get_message(mqid,strmsg)){ cout<<"Message : "<<strmsg<<endl; } update_notification(mqid);
// re-registor notification
}


void update_notification(mqd_t& mqid) { struct sigevent sigev1;
// For notification
sigev1.sigev_notify = SIGEV_THREAD; sigev1.sigev_notify_function = my_sig_handler; sigev1.sigev_notify_attributes = NULL; sigev1.sigev_value.sival_ptr = &mqid;
/* Arg. to thread func. */
if (mq_notify (mqid, &sigev1) == -1) { if (errno == EBUSY) printf ("Another process has registered for notification.\n"); perror("Error:mq_notify: "); _exit (EXIT_FAILURE); } }


No comments:

Post a Comment

would you like it. :)