pthread で itron ライクな sng_msg と rcv_msg (例)

http://d.hatena.ne.jp/l1o0/20100119/1263910139 の使用例

#include <mbox_test.h>

#define NUM_OF_THREAD 3

void test_example_init()
{
    int   ercd;
    int   i;
    u_int mbox_ids[NUM_OF_THREAD];

    ercd = init_mbox_table();
    if (E_OK != ercd) {
        fprintf(stderr, "init_mbox_table() = %d\n", ercd);
    }
    else {
        for (i = 0; i < NUM_OF_THREAD; i++) {
            ercd = create_mbox(&(mbox_ids[i]));
            if (E_OK != ercd) {
                fprintf(stderr, "create_mbox() = %d\n", ercd);
                break;
            }
        }
    }
}

void test_example_snd_msg(u_int mbox_id)
{
    int   ercd;
    u_int len;
    char  data[256];

    strncpy(data, "test message", sizeof(data));
    len = strnlen(data, sizeof(data)) + 1;

    ercd = snd_msg(mbox_id, (void *)data, len);
    if (E_OK != ercd) {
        fprintf(stderr, "snd_msg() = %d\n", ercd);
    }
}

void test_example_rcv_msg(u_int mbox_id)
{
    int    ercd;
    u_int  len;
    char  *msg;

    ercd = rcv_msg(mbox_id, (void **)&msg, &len);
    if (E_OK != ercd) {
        fprintf(stderr, "snd_msg() = %d\n", ercd);
    }
    else {
        /* do process */
        printf("len = %d msg = %s\n", len, msg);
        free(msg);
    }
}

void test_example_trcv_msg(u_int mbox_id)
{
    int    ercd;
    u_int  len;
    char  *msg;
    int    timeout;

    /* milli second */
    timeout = 100;

    ercd = trcv_msg(mbox_id, (void **)&msg, &len, timeout);
    if (E_OK != ercd) {
        fprintf(stderr, "snd_msg() = %d\n", ercd);
    }
    else {
        /* do process */
        printf("len = %d msg = %s\n", len, msg);
        free(msg);
    }
}