Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9022507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:31:37+00:00 2026-06-16T05:31:37+00:00

gcc 4.7.2 c89 APR 1.4 Hello, I am using APR thread pools, event loop

  • 0
gcc 4.7.2
c89
APR 1.4

Hello,

I am using APR thread pools, event loop (polling), and message queue (msgget, msgsnd, msgrcv).

Currently, I am just testing that if I press ctrl-c. It will set the while to false. So the message will be sent in my signal handler to be received in my while loop.

However, the msgrcv seems to block in my event loop. So I can’t get any other events from my library. In my event loop I am checking if a message has been recevied. However, I think it blocks waiting for a message. This will cause my loop to stop looping and I won’t be able to capture any event from my library.

This is the first time I have used message queues.

Here is my code, I am only copied the relevent parts so I have removed all error checking to keep it short.

Currently I am just testing that my event loop can receive a message from the queue, so it can stop the loop.

Many thanks for any suggestions,

#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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:31:40+00:00Added an answer on June 16, 2026 at 5:31 am

    Set the flag parameter on msgrcv (the 5th parm) to IPC_NOWAIT. This makes the queue receive non-blocking.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

gcc (GCC) 4.6.3 20120306 (Red Hat 4.6.3-2) c89 Hello, I am creating a thread
gcc 4.7.2 c89 Hello, I am using the Apache Portable Runtime and looking at
gcc 4.7.2 Apache Portable Runtime Hello, I am getting started with apr using threads.
gcc (GCC) 4.6.3 c89 apache runtime portable libraries Hello, Just a simple question I
gcc (GCC) 4.6.3 c89 Hello, I am just wondering if this is the best
gcc (GCC) 4.6.3 20120306 (Red Hat 4.6.3-2) c89 Hello, I am wondering if I
gcc 4.7.2 c89 Hello, I am wondering does any one know of any tutorials
gcc 4.7.2 c89 Hello, I am getting the following warning: pointer/integer type mismatch in
Possible Duplicate: Do-While and if-else statements in C/C++ macros gcc (GCC) 4.7.2 c89 Hello,
gcc (GCC) 4.7.2 c89 Hello, I have been looking at a test suite and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.