Example of using pthreads with a polling loop. This is written with a C++ compiler so there may be bits that do not compile with C.
#include <pthread.h>
#include <stdio.h>
struct Common
{
bool mRunning; /* Tells us when to quit */
float mCount;
};
/* The number crunching bit */
void* Cruncher (void* info)
{
struct Common* cptr = (struct Common*) info;
cptr->mCount = 0.0;
while (cptr->mRunning)
{
cptr->mCount++;
}
return (void*) 0;
}
void* PollKbd (void* info)
{
struct Common* cptr = (struct Common*) info;
int ch;
printf ("Press q followed by return to quit\n"

;
do {
ch = getchar ();
} while (ch != 'q');
cptr->mRunning = false;
return (void*) 0;
}
main ()
{
pthread_t crunchThread, pollThread;
struct Common common = { true, 0.0 };
pthread_create (&pollThread, 0, PollKbd, &common);
pthread_create (&crunchThread, 0, Cruncher, &common);
pthread_join (pollThread, 0);
pthread_join (crunchThread, 0);
printf ("Count got up to %f\n", common.mCount);
return 0;
}