使用多线程的消息队列

时间:2012-12-21 03:32:02

标签: c multithreading message-queue

gcc 4.7.2
c89
APR 1.4

您好,

我正在使用APR线程池,事件循环(轮询)和消息队列(msgget,msgsnd,msgrcv)。

目前,如果按ctrl-c,我只是测试一下。它会将while设置为false。因此,消息将在我的信号处理程序中发送,以便在我的while循环中接收。

然而,msgrcv似乎在我的事件循环中阻塞。所以我无法从我的图书馆获得任何其他活动。在我的事件循环中,我正在检查是否已收到消息。但是,我认为它会阻止等待消息。这将导致我的循环停止循环,我将无法从我的库中捕获任何事件。

这是我第一次使用消息队列。

这是我的代码,我只复制了相关的部分,所以我已经删除了所有错误检查以保持简短。

目前我只测试我的事件循环可以从队列接收消息,因此它可以停止循环。

非常感谢任何建议,

#define MSG_KEY 0001L
static int msg_id = 0;
static volatile apr_status_t is_looping = FALSE;

/* Wait for these message commands */
typedef enum tag_msg_cmd {END_LOOP, START_LOOP} msg_cmd_e;

/* message queue data */
typedef struct tag_msg_data msg_data_t;
struct tag_msg_data {
    long mtype;
    msg_cmd_e msg_cmd;
};

int main(void)
{
    /* Create message queue */
    msg_id = msgget(MSG_KEY, 0666 | IPC_CREAT);

    /* Create thread pool */
    has_error = apr_thread_pool_create(&thd_pool,
                                       init_threads,
                                       max_threads,
                                       mem_pool_app);

   /* Start event loop */
   LOG_CHECK(apr_thread_pool_schedule(thd_pool,
                                       (apr_thread_start_t)event_loop,
                                       NULL,
                                       0,
                                       NULL) == APR_SUCCESS, "Failed to schedule event loop");

    /* Pause until ctrl-c */
    pause();

    /* Destroy all memory pools and threading pools */
}

/* Poll for incoming events */
static apr_status_t event_loop(void)
{
    msg_data_t msg_data;
    int evt_id = 0;
    int msg_id = 0;

    /* Connect to the message queue */
    msg_id = msgget(MSG_KEY, 0644);
    assert(msg_id != -1 && "Failed to connect to the message queue");
    LOG_DEBUG("Connected to message queue with msg ID [ %d ]", msg_id);

    while(is_looping) {
        /* Under development - Process incoming event from shared library */
        /* evt_id = sr_waitevt(timeout_ms); */
        /* process_event(evt_id); */

        LOG_DEBUG("Looping....");

        /* Should not be blocking here, as I need to receive other events */
        msgrcv(msg_id, &msg_data, (sizeof msg_data) - (sizeof(long)), 2, 0);
        LOG_DEBUG("Waiting...");
        if(msg_data.msg_cmd == END_LOOP) {
            LOG_DEBUG("END_LOOP event queue message has been received");
            break;
        }
        sleep(1);
    }

    return 1;
}

/* Signal handler will terminated application on ctrl-c */
static void signal_handler(int sig)
{
    msg_data_t msg_data = {2,  END_LOOP};
    int rc = 0;

    rc = msgsnd(msg_id,
                &msg_data,
                (sizeof msg_data) - (sizeof(long)),
                IPC_NOWAIT);

    assert(rc == 0 && "Failed to send data to the message queue");
}

1 个答案:

答案 0 :(得分:1)

msgrcv(第5个parm)上的flag参数设置为IPC_NOWAIT。这使得队列接收非阻塞。