0

I have a process that uses different threads for making different type of jobs.

One such thread must send push notifications in a non-blocking way (so I will use libcurl because of the multi interface and SSL support). The master thread must pass the job to the worker and I thought about using apache apr message queue for message passing. Because in the same thread I must check for incoming messages and for the availability of the curl handles I think that I will use something like this:

while (1)
{
    while (apr_queue_try_pop(queue, &msg) == APR_SUCCESS)
    {
        // do something with the message
    }

    // perform a select or poll in the curl multi handle
    // treat the handles that are available for reads/writes
}

in the thread start function.

This is kind of busy waiting, is there a better solution?

Using C99 and Linux x86_64.

Victor Dodon
  • 1,796
  • 3
  • 18
  • 27

1 Answers1

0

It is difficult to answer your question precisely, there is just not enough information. But, if the code you are showing is the worker code, i.e. launched in a thread, and the need to push notices is frequent, then I do not see a reason this would not work. As for your question This is kind of busy waiting, is there a better solution? I usually do something to cause the thread to be idle once the task it is responsible to do is finished, such as use a sleep(1000). The remainder of the thread time slice is not used looping. Like this:

while (1)
{
    while (apr_queue_try_pop(queue, &msg) == APR_SUCCESS)
    {
        // do something with the message
    }

    // perform a select or poll in the curl multi handle
    // treat the handles that are available for reads/writes
    Sleep(1000);
}
ryyker
  • 22,849
  • 3
  • 43
  • 87