~bkerensa/ubuntu/raring/valgrind/merge-from-deb

« back to all changes in this revision

Viewing changes to helgrind/tests/tc06_two_races.c

  • Committer: Bazaar Package Importer
  • Author(s): Andrés Roldán
  • Date: 2008-06-13 02:31:40 UTC
  • mto: (1.4.1 upstream) (2.2.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 24.
  • Revision ID: james.westby@ubuntu.com-20080613023140-iwk33rz9rhvfkr96
Import upstream version 3.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
#include <pthread.h>
 
3
#include <stdio.h>
 
4
#include <stdlib.h>
 
5
 
 
6
/* Simple test program, has two races.  A happens-before detector can only
 
7
   ever detect one of them, though. */
 
8
 
 
9
int unprot1 = 0, unprot2 = 0, prot = 0;
 
10
pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER;
 
11
 
 
12
void* child_fn ( void* arg )
 
13
{
 
14
   unprot1 ++;
 
15
   pthread_mutex_lock( &mu );
 
16
   prot ++;
 
17
   pthread_mutex_unlock( &mu );
 
18
   unprot2 ++;
 
19
   return NULL;
 
20
}
 
21
 
 
22
int main ( void )
 
23
{
 
24
   pthread_t child;
 
25
 
 
26
   if (pthread_create(&child, NULL, child_fn, NULL)) {
 
27
      perror("pthread_create");
 
28
      exit(1);
 
29
   }
 
30
 
 
31
   unprot1 ++;
 
32
   pthread_mutex_lock( &mu );
 
33
   prot ++;
 
34
   pthread_mutex_unlock( &mu );
 
35
   unprot2 ++;
 
36
 
 
37
   if (pthread_join(child, NULL)) {
 
38
      perror("pthread join");
 
39
      exit(1);
 
40
   }
 
41
 
 
42
   return 0;
 
43
}