-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadTest.c
More file actions
65 lines (55 loc) · 1.53 KB
/
threadTest.c
File metadata and controls
65 lines (55 loc) · 1.53 KB
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
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define NOISE_SIZE 1024
#define PRINT_READ
#define PRINT_WRITE
static float noise[NOISE_SIZE];
static pthread_mutex_t noise_mutex; // = PTHREAD_MUTEX_INITIALIZER;
void intNoise() {
pthread_mutex_lock(&noise_mutex);
for (int i = 0; i < NOISE_SIZE; i++) noise[i] = rand() / (float)RAND_MAX;
pthread_mutex_unlock(&noise_mutex);
}
static void *writeNoise() {
static unsigned int writeCounter = 0;
while (1) {
pthread_mutex_lock(&noise_mutex);
noise[writeCounter % NOISE_SIZE] = rand() / (float)RAND_MAX;
writeCounter = (writeCounter + 1) % __UINT32_MAX__;
pthread_mutex_unlock(&noise_mutex);
#ifdef PRINT_WRITE
printf("write %4d\n", writeCounter % NOISE_SIZE);
fflush(stdout);
#endif
}
return NULL;
}
static void *readNoise() {
static unsigned int readCounter = 0;
float rnoise;
while (1) {
pthread_mutex_lock(&noise_mutex);
rnoise = noise[readCounter % NOISE_SIZE];
readCounter = (readCounter + 1) % __UINT32_MAX__;
pthread_mutex_unlock(&noise_mutex);
#ifdef PRINT_READ
printf("read #%4d %f\n", readCounter % NOISE_SIZE, rnoise);
fflush(stdout);
#endif
}
return NULL;
}
int main() {
intNoise();
// Create threads
pthread_t wtid, rtid;
pthread_create(&wtid, NULL, writeNoise, NULL);
pthread_create(&rtid, NULL, readNoise, NULL);
// Join threads (not really needed here as they run infinitely, but good
// practice)
pthread_join(wtid, NULL);
pthread_join(rtid, NULL);
return 0;
}