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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
/* Example code to demonstrate behavior of multi-core, threaded application
*
* Written by: Mike Anderson
* The PTR Group, Inc.
* mike@theptrgroup.com
*
* License: GPLv2
*
*/
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
pthread_mutex_t mutex_fix; /* protect the critical region */
pthread_t looperThId;
pthread_t conflictThId;
volatile int value1;
volatile int value2;
volatile int counter = 0;
volatile int bTimeToExit = FALSE;
int bUseMutex = FALSE; /* The "s" option */
/* Conflict thread */
void *conflictTh(void *arg)
{
int same;
unsigned long loopCount = 0;
struct timespec sleep_time = {
.tv_sec = 0,
.tv_nsec = 7 * 1000 * 1000,
};
while (!bTimeToExit) {
if (bUseMutex) {
// Acquire mutex using pthread_mutex_lock(3p)
}
same = (value1 == value2);
if (!same)
printf("\nHey! value1 = %d, value2 = %d\n", value1,
value2);
loopCount++;
if ((loopCount % 1000) == 0) {
printf("\nConflict thread Counter = %lu \n", loopCount);
fflush(stdout);
counter = 0;
}
if (bUseMutex) {
// Release mutex using pthread_mutex_unlock(3p);
}
/* Sleep for 7ms */
nanosleep(&sleep_time, NULL);
}
return NULL;
}
/* Main loop thread */
void *looperTh(void *arg)
{
int counterTemp;
struct timespec sleep_time = {
.tv_sec = 0,
.tv_nsec = 13 * 1000 * 1000,
};
/* give the conflict thread a chance to start */
sleep(1);
while (!bTimeToExit) {
if (bUseMutex) {
// Acquire mutex using pthread_mutex_lock(3p)
}
value1 = counter;
value2 = counter;
counterTemp = counter++;
if (bUseMutex) {
// Release mutex using pthread_mutex_unlock(3p);
}
if ((counterTemp % 1000000) == 0) {
printf("Main loop counter = %d \r", counterTemp);
fflush(stdout);
/* Sleep for 13ms */
nanosleep(&sleep_time, NULL);
}
}
return NULL;
}
/* Ctrl-C Signal Handler */
void gotCtrlC(int sig)
{
printf("\nGot Ctrl-C! Shutting down...\n");
bTimeToExit = TRUE;
}
void print_usage(char *program)
{
fprintf(stderr, "Usage: %s [options]\n%s", program,
"-h This help\n"
"-s (Use mutex to enforce synchronization)\n");
}
void parse_options(int argc, char *argv[])
{
int c;
opterr = 0; /* get opt stuff */
while ((c = getopt(argc, argv, "hs")) != -1) {
switch (c) {
case 's':
bUseMutex = TRUE;
break;
case '?':
if (isprint(optopt))
fprintf(stderr, "Unknown option '-%c'.\n",
optopt);
else
fprintf(stderr,
"Unknown option character '\\x%x'.\n",
optopt);
/*FALLTHRU*/
case 'h':
default:
print_usage(argv[0]);
exit(EXIT_SUCCESS);
}
}
printf("Options in effect:\n");
if (bUseMutex)
printf(" Thread synchronization using mutexes is in effect \n");
else
printf(" Mutex-based thread synchronization is off \n");
}
int main(int argc, char *argv[])
{
int res;
/* Parse the command line arguments */
parse_options(argc, argv);
printf("\nUse ^C to exit application...\n");
/* Set up the ^C signal handler so we can shut down cleanly */
(void)signal(SIGINT, gotCtrlC);
/* Go ahead and initialize the mutex in case we need it */
// Initialize the mutex using pthread_mutex_init(3p)
if (res != 0) {
perror("Mutex initialization failed");
exit(EXIT_FAILURE);
}
/* Create the main loop thread */
res = pthread_create(&looperThId, NULL, looperTh, NULL);
if (res != 0) {
perror("Main loop thread creation failed");
exit(EXIT_FAILURE);
}
/* Create the conflict thread */
res = pthread_create(&conflictThId, NULL, conflictTh, NULL);
if (res != 0) {
perror("Conflict thread creation failed");
exit(EXIT_FAILURE);
}
printf("\nMain is waiting for threads to finish...\n");
res = pthread_join(conflictThId, NULL);
if (res != 0) {
perror("Thread join with conflictTh() failed");
exit(EXIT_FAILURE);
}
res = pthread_join(looperThId, NULL);
if (res != 0) {
perror("Thread join with looperTh() failed");
exit(EXIT_FAILURE);
}
printf("Threads joined -- exiting main\n");
// Destroy the mutex using pthread_mutex_destroy(3p);
return EXIT_SUCCESS;
}
|