File: Test_threads_b.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 (182 lines) | stat: -rw-r--r-- 4,055 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
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 

*/

/* This test program starts by puttting 10 into a 100x1000 array
then incrments it by 5 and decrements it by 2, one thousand 
times. The incrment and decrement operations are prevented from
operlapping by a mutex variable. */

/* If we #define DISABLE_MUTEX, there is no locking
on the array and so any thread can overwrite at any time
so we would expect jibberish output */

#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(void *y);
void *decrement(void *z);

int  check_array();

int r1 = 0, r2 = 0;

pthread_mutex_t array_mutex;

int array[1000][1000];

#endif /* ifdef ENABLE_POSIX_THREADS */

int main()
{
#ifdef ENABLE_POSIX_THREADS
  pthread_t       thread1, thread2;
  int i, j, ret;
  /* Put 10 in each array element */
  for(i=0; i<1000; ++i)
    for(j=0; j<1000; ++j)
      array[i][j]=10;

  pthread_mutex_init(&array_mutex, NULL);
#ifdef ENABLE_POSIX_THREADS
#ifdef HAVE_PTHREAD_SETCONCURRENCY     
  pthread_setconcurrency(6);
#endif 
#endif 
  for(i=1; i<=100; ++i)
  {
     if( pthread_create(&thread1, NULL, increment, (void *) &r1) != 0)
     {
        perror("Thread 1 not created properly");
        exit(1);
      }

     if( pthread_create(&thread2, NULL, decrement, (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);
     }
   }
   ret=check_array(); /* Returns 0 or 1 */
   return(ret); 
#else
     return 77;
#endif
  exit(0);
}

#ifdef ENABLE_POSIX_THREADS
void *increment(void *pnum_times)
{
  int i, j;
  if( pthread_mutex_lock(&array_mutex) != 0)
  {
    perror("pthread_mutex_lock failed");
    exit(1);
  }
  /* Increment each value in the array by 5, so 
  we can check the values later. */
  for(i=0; i<1000; ++i)
    for(j=0; j<1000; ++j)
      array[i][j]+=5;
  if( pthread_mutex_unlock(&array_mutex) != 0)
  {
    perror("pthread_mutex_unlock failed");
    exit(1);
  }
  return(0);

}

void *decrement(void *pnum_times)
{
  int i,j;


#ifndef DISABLE_MUTEX 
  if( pthread_mutex_lock(&array_mutex) != 0)
  {
    perror("pthread_mutex_lock failed");
    exit(1);
  }
#endif

  /* Decrement each value in the array by 2, so 
  we can check the values later. */
  for(i=0; i<1000; ++i)
    for(j=0; j<1000; ++j)
      array[i][j]-=2;

#ifndef DISABLE_MUTEX 
  if(pthread_mutex_unlock(&array_mutex) != 0)
  {
    perror("pthread_mutex_unlock failed");
    exit(1);
  }
#endif
  return 0;
}

int check_array()
{
  int i,j;
  for(i=0; i<1000; ++i)
  {
    for(j=0; j<1000; ++j)
    {
      /* Numers start at 10, but get incremented by 
      5 by 2, which is 3 . Loop runs 2000 times,
      so resuls should be 2000*(5-2) + 10 = 6010 */

      if (array[i][j] != 310)
      {
	fprintf(stderr,"array[%d][%d]=%d\n",i,j,array[i][j]);
	return(1);
      }
    }
  }
  return(0);
}
#endif