File: ex7.c

package info (click to toggle)
eglibc 2.11.3-4
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 140,396 kB
  • ctags: 119,927
  • sloc: ansic: 909,722; asm: 178,348; sh: 8,983; makefile: 7,625; perl: 2,221; awk: 1,728; cpp: 1,278; pascal: 715; yacc: 317; sed: 131
file content (45 lines) | stat: -rw-r--r-- 823 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/* This is a test of the special shutdown that occurs
   when all threads, including the main one, call
   pthread_exit(). It demonstrates that atexit
   handlers are properly called, and that the
   output is properly flushed even when stdout is
   redirected to a file, and therefore fully buffered. */

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#define NTHREADS 20		/* number of threads */

static void *
thread (void *arg)
{
  printf ("thread terminating\n");
  return 0;
}

static void
cleanup (void)
{
  printf ("atexit handler called\n");
}

int
main (void)
{
  int i;

  atexit (cleanup);

  for (i = 0; i < NTHREADS; i++)
    {
      pthread_t id;
      if (pthread_create (&id, 0, thread, 0) != 0)
	{
	  fprintf (stderr, "pthread_create failed\n");
	  abort ();
	}
    }

  pthread_exit (0);
}