periodic thread

How to create a periodic thread?

Bruno <bruno.suarez@scola.ac-paris.fr> wrote:

How to create a periodic thread?

What do you mean by a periodic thread? If you want regular notification,
look at the timer_* calls, and look at creating a periodic timer.

-David

David Gibbs wrote:

Bruno <> bruno.suarez@scola.ac-paris.fr> > wrote:
How to create a periodic thread?

What do you mean by a periodic thread? If you want regular notification,
look at the timer_* calls, and look at creating a periodic timer.

-David

I want to create a thread which automatically periodically
startsagain its execution with a configurable time.

Bruno <bruno.suarez@scola.ac-paris.fr> wrote:


David Gibbs wrote:

Bruno <> bruno.suarez@scola.ac-paris.fr> > wrote:
How to create a periodic thread?

What do you mean by a periodic thread? If you want regular notification,
look at the timer_* calls, and look at creating a periodic timer.

-David



I want to create a thread which automatically periodically
startsagain its execution with a configurable time.

Yup, you want a timer. Take a look at timer_create() and
timer_settime() to setup a timer to give you periodic notification.

Block your thread – exactly how you block it depends on how you are
doing the notification – but a good choice is with notification using
a pulse on a channel that only your periodic thread will be blocked on
with MsgReceivePulse().

-David

David Gibbs wrote:

Bruno <> bruno.suarez@scola.ac-paris.fr> > wrote:

David Gibbs wrote:

Bruno <> bruno.suarez@scola.ac-paris.fr> > wrote:
How to create a periodic thread?

What do you mean by a periodic thread? If you want regular notification,
look at the timer_* calls, and look at creating a periodic timer.

-David

I want to create a thread which automatically periodically
startsagain its execution with a configurable time.

Yup, you want a timer. Take a look at timer_create() and
timer_settime() to setup a timer to give you periodic notification.

Block your thread – exactly how you block it depends on how you are
doing the notification – but a good choice is with notification using
a pulse on a channel that only your periodic thread will be blocked on
with MsgReceivePulse().

-David

Have you a complete example?

Bruno <bruno.suarez@scola.ac-paris.fr> wrote:


: David Gibbs wrote:
:>
:> Bruno <bruno.suarez@scola.ac-paris.fr> wrote:
:>
:> > David Gibbs wrote:
:> >>
:> >> Bruno <bruno.suarez@scola.ac-paris.fr> wrote:
:> >> > How to create a periodic thread?
:> >>
:> >> What do you mean by a periodic thread? If you want regular notification,
:> >> look at the timer_* calls, and look at creating a periodic timer.
:> >>
:> >> -David
:>
:> > I want to create a thread which automatically periodically
:> > startsagain its execution with a configurable time.
:>
:> Yup, you want a timer. Take a look at timer_create() and
:> timer_settime() to setup a timer to give you periodic notification.
:>
:> Block your thread – exactly how you block it depends on how you are
:> doing the notification – but a good choice is with notification using
:> a pulse on a channel that only your periodic thread will be blocked on
:> with MsgReceivePulse().
:>
:> -David


: Have you a complete example?

Here’s an example based on the dispatch framework:

-seanb


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stddef.h>
#define THREAD_POOL_PARAM_T dispatch_context_t
#include <sys/dispatch.h>

const char *my_arg1=“Got one\n”;
const char *my_arg2=“Got two\n”;

int code1, code2, coid;

dispatch_t *dpp;
int pulse_input(message_context_t *ctp, int code, unsigned flags, void *handle);
void * worker (void *arg);


int
main(int argc, char **argv) {
thread_pool_attr_t pool_attr;
thread_pool_t *tpp;
timer_t tmr;
struct sigevent sig;
struct itimerspec to= {{1, 0}, {1, 0}};


if((dpp = dispatch_create()) == NULL)
{
fprintf(stderr, “%s: Unable to allocate dispatch context.\n”,argv[0]);
return EXIT_FAILURE;
}

memset(&pool_attr, 0, sizeof pool_attr);
pool_attr.handle = dpp;
pool_attr.context_alloc = dispatch_context_alloc;
pool_attr.block_func = dispatch_block;
pool_attr.handler_func = dispatch_handler;
pool_attr.context_free = dispatch_context_free;
pool_attr.lo_water = 1;
pool_attr.hi_water = 1;
pool_attr.increment = 0;
pool_attr.maximum = 1;

if((tpp = thread_pool_create(&pool_attr, POOL_FLAG_EXIT_SELF)) == NULL)
{
fprintf(stderr, “%s: Unable to initialize thread pool.\n”,argv[0]);
return EXIT_FAILURE;
}

if ((code1 = pulse_attach (dpp, MSG_FLAG_ALLOC_PULSE, 0, pulse_input, my_arg1)) == -1 ||
(code2 = pulse_attach (dpp, MSG_FLAG_ALLOC_PULSE, 0, pulse_input, my_arg2)) == -1)
{
fprintf(stderr, “Unable to attach pulse.\n”);
return EXIT_FAILURE;
}

if ((coid = message_connect(dpp, MSG_FLAG_SIDE_CHANNEL)) == -1 )
{
fprintf(stderr, “Unable to connect.\n”);
return EXIT_FAILURE;
}

sig.sigev_notify= SIGEV_PULSE;
sig.sigev_coid= coid;
sig.sigev_priority= getprio(0);
sig.sigev_code= code1;
sig.sigev_value.sival_int= 0;

if (timer_create(CLOCK_REALTIME, &sig, &tmr))
{
fprintf(stderr, “timer_create.\n”);
return EXIT_FAILURE;
}

if (timer_settime(tmr, 0, &to, 0) == -1)
{
fprintf(stderr, “timer_settime.\n”);
return EXIT_FAILURE;
}

pthread_create (NULL, NULL, worker, NULL);
// Never returns
thread_pool_start(tpp);
return 0;
}

void *
worker (void *arg)
{
int prio = getprio(0);

for(;:wink:
{
sleep(1);
MsgSendPulse(coid, prio, code2, 0);
}
}

int
pulse_input(message_context_t *ctp, int code, unsigned flags, void *handle)
{
printf("%s", (char *)handle);
return 0;
}

Bruno <bruno.suarez@scola.ac-paris.fr> wrote:


Yup, you want a timer. Take a look at timer_create() and
timer_settime() to setup a timer to give you periodic notification.

Block your thread – exactly how you block it depends on how you are
doing the notification – but a good choice is with notification using
a pulse on a channel that only your periodic thread will be blocked on
with MsgReceivePulse().

-David



Have you a complete example?

Take a look at the docs for timer_create(), as I said above. It includes
an example of everything described above except putting it in a seperate
thread – it does it in a single-threaded process in main().

-David