Back to index

glibc  2.9
Classes | Functions
ex13.c File Reference
#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

Go to the source code of this file.

Classes

struct  thr_ctrl

Functions

static void * thread_start (void *ptr) __attribute__((__noreturn__))
static void dump_mut (pthread_mutex_t *mut)
static void pthr_cond_signal_mutex (pthread_cond_t *cond, pthread_mutex_t *mut)
int main (void)

Class Documentation

struct thr_ctrl

Definition at line 31 of file ex13.c.

Collaboration diagram for thr_ctrl:
Class Members
pthread_cond_t cond
pthread_mutex_t mutex
int retval

Function Documentation

static void dump_mut ( pthread_mutex_t mut) [static]

Definition at line 39 of file ex13.c.

{
  size_t i;
  for (i = 0; i < sizeof (*mut); i++)
    printf (" %02x", *((unsigned char *) mut + i));
  printf ("\n");
};

Here is the call graph for this function:

Here is the caller graph for this function:

int main ( void  )

Definition at line 78 of file ex13.c.

{
  struct thr_ctrl threadctrl;
  pthread_t thread;
  int err;
  void *res = &threadctrl.retval;
  pthread_mutexattr_t mutattr;
  pthread_mutexattr_init (&mutattr);
  pthread_mutex_init (&threadctrl.mutex, &mutattr);
  pthread_cond_init (&threadctrl.cond, NULL);
  err = pthread_mutex_lock (&threadctrl.mutex);
  if (err)
    printf ("mutex_lock : %s\n", strerror (err));
  dump_mut (&threadctrl.mutex);
  pthread_create (&thread, NULL, thread_start, &threadctrl);
  /* Wait until it's ready.  */
  err = pthread_cond_wait (&threadctrl.cond, &threadctrl.mutex);
  if (err)
    printf ("cond_wait  : %s\n", strerror (err));
  /* Now, we should have acquired the mutex again!  */
  dump_mut (&threadctrl.mutex);
  sleep (1);
  dump_mut (&threadctrl.mutex);
  err = pthread_cond_wait (&threadctrl.cond, &threadctrl.mutex);
  if (err)
    {
      printf ("cond_wait  : %s\n", strerror (err));
      printf ("ERROR\n");
      abort ();
    };
  dump_mut (&threadctrl.mutex);
  pthread_join (thread, &res);
  printf ("OK\n");
  return 0;
}

Here is the call graph for this function:

static void pthr_cond_signal_mutex ( pthread_cond_t cond,
pthread_mutex_t mut 
) [static]

Definition at line 49 of file ex13.c.

{
  int err;
  err = pthread_mutex_lock (mut);
  if (err)
    printf ("mutex_lock  : %s\n", strerror (err));
  err = pthread_cond_signal (cond);
  if (err)
    printf ("cond_signal : %s\n", strerror (err));
  err = pthread_mutex_unlock (mut);
  if (err)
    printf ("mutex_unlock: %s\n", strerror (err));
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void * thread_start ( void *  ptr) [static]

Definition at line 64 of file ex13.c.

{
  struct thr_ctrl *tc = ptr;
  /* Do initialization.  */
  /* ... */
  /* Signal that we are ready.  */
  pthr_cond_signal_mutex (&tc->cond, &tc->mutex);
  sleep (2);
  pthr_cond_signal_mutex (&tc->cond, &tc->mutex);
  tc->retval = 0;
  pthread_exit (&tc->retval);
}

Here is the call graph for this function:

Here is the caller graph for this function: