File: Test_threads_a.c

package info (click to toggle)
atlc 4.6.1-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 134,508 kB
  • sloc: ansic: 8,452; sh: 4,862; makefile: 1,055; cpp: 64
file content (156 lines) | stat: -rw-r--r-- 3,158 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
atlc - arbitrary transmission line calculator, for the analysis of
transmission lines are directional couplers. 

Copyright (C) 2002. Dr. David Kirkby, PhD (G8WRB).

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either package_version 2
of the License, or (at your option) any later package_version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
USA.

Dr. David Kirkby, e-mail drkirkby@ntlworld.com 

*/
#include "config.h"

#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif

#ifdef HAVE_STDIO_H
#include <stdlib.h>
#endif

#ifdef ENABLE_POSIX_THREADS
#include <pthread.h>

void *increment_a(void *arg);
void *increment_b(void *arg);
int  finalise(int, int);

int r1 = 0, r2 = 0, r3 = 0;

pthread_mutex_t r3_mutex;

#endif /* ifdef ENABLE_POSIX_THREADS */

int main()
{
#ifdef ENABLE_POSIX_THREADS
  pthread_t       thread1, thread2;
  int counter=0, i;

  pthread_mutex_init(&r3_mutex, NULL);

  r3 = 4;

  for(i=1; i<10000; ++i)
  {
     if( pthread_create(&thread1, NULL, increment_a, (void *) &r1) != 0)
     {
        perror("Thread 1 not created properly");
        exit(1);
      }

     if( pthread_create(&thread2, NULL, increment_b, (void *) &r2) != 0)
     {
        perror("Thread 2 not created properly");
        exit(1);
      }
  
     if(pthread_join(thread1, NULL) != 0)
     {
       perror("Thread 1 did not join properly");
       exit (1);
     }
     if(pthread_join(thread2, NULL) != 0)
     {
       perror("Thread 2 did not join properly");
       exit (1);
     }

     counter+=finalise(r1, r2);
   }
   if (counter == 399960000)
     return 0;
   else
     return 1;
#else
     return 77;
#endif
}

#ifdef ENABLE_POSIX_THREADS
void *increment_a(void *arg)
{
  int  i,x;
  int *pnum_times=(int *) arg;
 
  if( pthread_mutex_lock(&r3_mutex) != 0)
  {
    perror("pthread_mutex_lock failed");
    exit(1);
  }
  if (r3 > 3) {
	x = r3;
        r3--;
  } else {
        x = 1;
  }
  if( pthread_mutex_unlock(&r3_mutex) != 0)
  {
    perror("pthread_mutex_unlock failed");
    exit(1);
  }
  for (i = 0;  i < 4; i++) {
    (*pnum_times)++;
  }
  return (NULL);
}

void *increment_b(void *arg)
{
  int i,x;
  int *pnum_times=(int *) arg;

  if( pthread_mutex_lock(&r3_mutex) != 0)
  {
    perror("pthread_mutex_lock failed");
    exit(1);
  }
  if (r3 > 3) {
        x = r3;
        r3--; 
  } else {
        x = 1;
  }
  if(pthread_mutex_unlock(&r3_mutex) != 0)
  {
    perror("pthread_mutex_unlock failed");
    exit(1);
  }
  for (i = 0;  i < 4; i++) {
    (*pnum_times)++;
  }
  return (NULL);
}

int  finalise(int one_times, int another_times)
{
  int total;

  total = one_times + another_times;
  return(total);
}
#endif