Back to index

glibc  2.9
Defines | Functions | Variables
ex11.c File Reference
#include <errno.h>
#include <error.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

Go to the source code of this file.

Defines

#define NWRITERS   15
#define WRITETRIES   10
#define NREADERS   15
#define READTRIES   15
#define TIMEOUT   1000000
#define DELAY   1000000

Functions

static void * writer_thread (void *nr)
static void * reader_thread (void *nr)
int main (void)

Variables

static pthread_rwlock_t lock = PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP

Define Documentation

#define DELAY   1000000

Definition at line 35 of file ex11.c.

#define NREADERS   15

Definition at line 31 of file ex11.c.

#define NWRITERS   15

Definition at line 29 of file ex11.c.

#define READTRIES   15

Definition at line 32 of file ex11.c.

#define TIMEOUT   1000000

Definition at line 34 of file ex11.c.

#define WRITETRIES   10

Definition at line 30 of file ex11.c.


Function Documentation

int main ( void  )

Definition at line 116 of file ex11.c.

{
  pthread_t thwr[NWRITERS];
  pthread_t thrd[NREADERS];
  int n;
  void *res;

  /* Make standard error the same as standard output.  */
  dup2 (1, 2);

  /* Make sure we see all message, even those on stdout.  */
  setvbuf (stdout, NULL, _IONBF, 0);

  for (n = 0; n < NWRITERS; ++n)
    {
      int err = pthread_create (&thwr[n], NULL, writer_thread,
                            (void *) (long int) n);

      if (err != 0)
       error (EXIT_FAILURE, err, "cannot create writer thread");
    }

  for (n = 0; n < NREADERS; ++n)
    {
      int err = pthread_create (&thrd[n], NULL, reader_thread,
                            (void *) (long int) n);

      if (err != 0)
       error (EXIT_FAILURE, err, "cannot create reader thread");
    }

  /* Wait for all the threads.  */
  for (n = 0; n < NWRITERS; ++n)
    pthread_join (thwr[n], &res);
  for (n = 0; n < NREADERS; ++n)
    pthread_join (thrd[n], &res);

  return 0;
}

Here is the call graph for this function:

static void* reader_thread ( void *  nr) [static]

Definition at line 80 of file ex11.c.

{
  struct timespec ts;
  struct timespec delay;
  int n;

  delay.tv_sec = 0;
  delay.tv_nsec = DELAY;

  for (n = 0; n < READTRIES; ++n)
    {
      do
       {
         clock_gettime (CLOCK_REALTIME, &ts);

         ts.tv_nsec += TIMEOUT;

         printf ("reader thread %ld tries again\n", (long int) nr);
       }
      //while (pthread_rwlock_rdlock (&lock), 0);
      while (pthread_rwlock_timedrdlock (&lock, &ts) == ETIMEDOUT);

      printf ("reader thread %ld succeeded\n", (long int) nr);

      nanosleep (&delay, NULL);

      pthread_rwlock_unlock (&lock);

      printf ("reader thread %ld released\n", (long int) nr);
    }

  return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void* writer_thread ( void *  nr) [static]

Definition at line 41 of file ex11.c.

{
  struct timespec ts;
  struct timespec delay;
  int n;

  ts.tv_sec = 0;
  ts.tv_nsec = TIMEOUT;

  delay.tv_sec = 0;
  delay.tv_nsec = DELAY;

  for (n = 0; n < WRITETRIES; ++n)
    {
      do
       {
         clock_gettime (CLOCK_REALTIME, &ts);

         ts.tv_nsec += 2 * TIMEOUT;

         printf ("writer thread %ld tries again\n", (long int) nr);
       }
      //while (pthread_rwlock_wrlock (&lock), 0);
      while (pthread_rwlock_timedwrlock (&lock, &ts) == ETIMEDOUT);

      printf ("writer thread %ld succeeded\n", (long int) nr);

      nanosleep (&delay, NULL);

      pthread_rwlock_unlock (&lock);

      printf ("writer thread %ld released\n", (long int) nr);
    }

  return NULL;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

pthread_rwlock_t lock = PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP [static]

Definition at line 37 of file ex11.c.