~ubuntu-branches/ubuntu/natty/eglibc/natty-security

« back to all changes in this revision

Viewing changes to linuxthreads/Examples/ex1.c

  • Committer: Bazaar Package Importer
  • Author(s): Aurelien Jarno
  • Date: 2009-05-05 09:54:14 UTC
  • Revision ID: james.westby@ubuntu.com-20090505095414-c45qsg9ixjheohru
ImportĀ upstreamĀ versionĀ 2.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Creates two threads, one printing 10000 "a"s, the other printing
 
2
   10000 "b"s.
 
3
   Illustrates: thread creation, thread joining. */
 
4
 
 
5
#include <stddef.h>
 
6
#include <stdio.h>
 
7
#include <unistd.h>
 
8
#include "pthread.h"
 
9
 
 
10
static void *
 
11
process (void *arg)
 
12
{
 
13
  int i;
 
14
  fprintf (stderr, "Starting process %s\n", (char *) arg);
 
15
  for (i = 0; i < 10000; i++)
 
16
    {
 
17
      write (1, (char *) arg, 1);
 
18
    }
 
19
  return NULL;
 
20
}
 
21
 
 
22
int
 
23
main (void)
 
24
{
 
25
  int retcode;
 
26
  pthread_t th_a, th_b;
 
27
  void *retval;
 
28
 
 
29
  retcode = pthread_create (&th_a, NULL, process, (void *) "a");
 
30
  if (retcode != 0)
 
31
    fprintf (stderr, "create a failed %d\n", retcode);
 
32
  retcode = pthread_create (&th_b, NULL, process, (void *) "b");
 
33
  if (retcode != 0)
 
34
    fprintf (stderr, "create b failed %d\n", retcode);
 
35
  retcode = pthread_join (th_a, &retval);
 
36
  if (retcode != 0)
 
37
    fprintf (stderr, "join a failed %d\n", retcode);
 
38
  retcode = pthread_join (th_b, &retval);
 
39
  if (retcode != 0)
 
40
    fprintf (stderr, "join b failed %d\n", retcode);
 
41
  return 0;
 
42
}