This repository has been archived by the owner on Apr 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pthread_inc_mutex.c
99 lines (89 loc) · 2.01 KB
/
pthread_inc_mutex.c
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
/*
* pthread_inc.c
* compile this file by:
gcc pthread_inc.c -lpthread
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/time.h>
typedef struct thread_arg {
pthread_t tid;
int idx;
long n_inc;
long c[1];
long *p;
} * thread_arg_t;
//mutex
pthread_mutex_t mg, ms, mp0;
long g = 0;
void * thread_func(void * _arg) {
thread_arg_t arg = _arg;
long i;
long n_inc = arg->n_inc;
static long s = 0;
long l = 0;
for (i = 0; i < n_inc; i++) {
pthread_mutex_lock(&mg);
g++;
pthread_mutex_unlock(&mg);
}
for (i = 0; i < n_inc; i++) {
pthread_mutex_lock(&ms);
s++;
pthread_mutex_unlock(&ms);
}
for (i = 0; i < n_inc; i++) {
l++;
}
for (i = 0; i < n_inc; i++) {
arg->c[0]++;
}
for (i = 0; i < n_inc; i++) {
pthread_mutex_lock(&mp0);
arg->p[0]++;
pthread_mutex_unlock(&mp0);
}
//printf("g = %ld, s = %ld, l = %ld, c[0] = %ld, p[0] = %ld\n",
//g, s, l, arg->c[0], arg->p[0]);
//printf("&g = %p, &s = %p, &l = %p, &arg->c[0] = %p, arg->p = %p\n", &g, &s, &l, &arg->c[0], arg->p);
return 0;
}
double cur_time() {
struct timeval tp[1];
gettimeofday(tp, NULL);
return tp->tv_sec + tp->tv_usec * 1.0E-6;
}
int main(int argc, char ** argv)
{
if (argc <= 2) {
fprintf(stderr,
"usage: %s no_of_threads no_of_increments\n"
"example:\n %s 16 10000000\n",
argv[0], argv[0]);
exit(1);
}
int n_threads = atoi(argv[1]);
long n_inc = atol(argv[2]);
struct thread_arg args[n_threads];
double t0 = cur_time();
long a_counter[1] = { 0 };
int i;
/* スレッドを N_THREADS 個作る */
for (i = 0; i < n_threads; i++) {
args[i].idx = i;
args[i].n_inc = n_inc;
args[i].c[0] = 0;
args[i].p = a_counter;
pthread_create(&args[i].tid, NULL,
thread_func, (void *)&args[i]);
}
/* 終了待ち */
for (i = 0; i < n_threads; i++) {
pthread_join(args[i].tid, NULL);
}
double t1 = cur_time();
printf("OK: elapsed time: %f\n", t1 - t0);
return 0;
}