ch4-condvar

Chapter_4     spin-condvar thread-pid







condvar.c     ALP, p. 86


#include <stdio.h> // for printf(), putchar(), fflush(), stdout, NULL
#include <unistd.h> // for sleep()
#include <pthread.h> // for pthread_t, pthread_cond_t, pthread_mutex_t,
// pthread_cond_init(), pthread_cond_wait(), pthread_cond_signal(),
// pthread_mutex_init(), pthread_mutex_lock(), pthread_mutex_unlock(),
// pthread_create()
// pthread.h includes bits/pthreadtypes.h, full path
// /usr/include/x86_64-linux-gnu/bits/pthreadtypes.h,
// which defines pthread_t, pthread_cond_t, and pthread_mutex_t

int thread_flag;
pthread_cond_t thread_flag_cv;
pthread_mutex_t thread_flag_mutex;

void initialize_flag(void);
void* thread_function (void* thread_arg);
void set_thread_flag (int flag_value);

int main()
{
int i, flag = 1;
initialize_flag();
set_thread_flag(flag);

pthread_t thread;
pthread_create (&thread, NULL, &thread_function, NULL);
for(i = 0; ; i++) // for ever
{
printf("main ");
fflush(stdout); // flush the output buffer
sleep(1); // pause for 1 second
if (i % 5 == 4)
{
putchar('\n');
flag = (flag + 1) % 2; // change state, 0 <--> 1
set_thread_flag(flag);
}
}

return 0;
}

void initialize_flag(void)
{
/* Initialize the mutex and condition variable: */
pthread_mutex_init (&thread_flag_mutex, NULL);
pthread_cond_init (&thread_flag_cv, NULL);
/* Initialize the flag value: */
thread_flag = 0;
}

void do_work(void);

/* Calls do_work() repeatedly while the thread flag is set,
blocks if the flag is clear */
void* thread_function (void* thread_arg)
{
while (1) // loop forever
{
/* Lock the mutex before accessing the flag value: */
pthread_mutex_lock (&thread_flag_mutex);
while (!thread_flag)
{
/* The flag is clear. Wait for a signal on the condition
variable, indicating that the flag value has changed. When the
signal arrives and this thread unblocks, loop and check the
flag again. */
pthread_cond_wait (&thread_flag_cv, &thread_flag_mutex);
}
// When we've gotten here, we know the flag must be set
pthread_mutex_unlock (&thread_flag_mutex);
do_work(); /* Do some work */
}

return NULL;
}

void do_work(void)
{
printf("work ");
fflush(stdout);
sleep(1);
}

void set_thread_flag (int flag_value)
{
/* Lock the mutex before accessing the flag value: */
pthread_mutex_lock (&thread_flag_mutex);
/* Set the flag value, and then signal in case thread_function()
is blocked, waiting for the flag to become set. However,
thread_function() can't actually check the flag until the mutex
is unlocked: */
thread_flag = flag_value;
pthread_cond_signal (&thread_flag_cv);
pthread_mutex_unlock (&thread_flag_mutex);
}
/*
gcc condvar.c -o condvar -lpthread
./condvar
main work main work main work main work main work
main work main main main main
main work main work main work main work main work
main main main main main
main work main work work main work main work main
main main main main main
main work work main work main work main work main work
main main main main main
main work main work main work main work main work
main main main main main
main work main work main work main work main work
main work main main main main
main work work main main work main work main work work
main main ^C // Ctrl^C
*/









Chapter_4     spin-condvar BACK_TO_TOP thread-pid



Comments

Popular posts from this blog

Contents