This example shows how two threads can exchange data using the L4 IPC mechanism. One thread is sending an integer to the other thread which is returning the square of the integer. Both values are printed.
#include <l4/sys/ipc.h>
#include <pthread-l4.h>
#include <unistd.h>
#include <stdio.h>
static pthread_t t2;
static void *thread1_fn(void *arg)
{
int ipc_error;
unsigned long value = 1;
(void)arg;
while (1)
{
printf("Sending: %ld\n", value);
if (ipc_error)
fprintf(stderr, "thread1: IPC error: %x\n", ipc_error);
else
sleep(1);
value++;
}
return NULL;
}
static void *thread2_fn(void *arg)
{
int ipc_error;
(void)arg;
while (1)
{
if (ipc_error)
{
fprintf(stderr, "thread2: IPC error: %x\n", ipc_error);
continue;
}
}
return NULL;
}
int main(void)
{
if (pthread_create(&t2, NULL, thread2_fn, NULL))
{
fprintf(stderr, "Thread creation failed\n");
return 1;
}
thread1_fn(NULL);
return 0;
}