-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmtlist.c
110 lines (90 loc) · 3.03 KB
/
mtlist.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
100
101
102
103
104
105
106
107
108
109
110
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include <string.h>
#include "list.h"
struct thread_args {
list_t *list;
int values_to_add;
};
void *worker_thread(void *v) {
fprintf(stderr,"Thread 0x%0lx started.\n", (long)pthread_self());
struct thread_args *targs = (struct thread_args*)v;
const int value_range = 1000;
int i = 0;
for (i = 0; i < targs->values_to_add; i++) {
// add a random value between 0 and value_range-1 to the list
int value = random() % value_range;
list_add(targs->list, value);
}
// sleep a random amount of time (up to 1 millisecond)
usleep(random() % 1000);
for (i = 0; i < targs->values_to_add; i++) {
// try to remove a random value between 0 and value_range-1 from the list
int value = random() % value_range;
list_remove(targs->list, value);
}
fprintf(stderr,"Thread 0x%0lx done.\n", (long)pthread_self());
return NULL;
}
void usage(const char *progname) {
fprintf(stderr, "usage: %s [-h] [-t threads] [-m max_values_to_add]\n", progname);
fprintf(stderr, "\t-m: num of values for each thread to add to list (default 10)\n");
fprintf(stderr, "\t-t: number of threads to start up (default 5)\n");
fprintf(stderr, "\t-h: show this help\n");
exit(0);
}
int main(int argc, char **argv) {
int num_threads = 5;
int values_to_add = 10;
int c;
while ((c = getopt(argc, argv, "t:m:h")) != -1) {
switch(c) {
case 't':
num_threads = atoi(optarg);
if (num_threads < 1 || num_threads > 100) {
usage(argv[0]);
}
break;
case 'm':
values_to_add = atoi(optarg);
if (values_to_add < 1) {
usage(argv[0]);
}
break;
case 'h':
default:
usage(argv[0]);
break;
}
}
// see the RNG
srandom(42);
list_t *thelist = (list_t*)malloc(sizeof(list_t));
list_init(thelist);
// set up thread arguments
printf("Starting %d thread%s\n", num_threads, num_threads == 1 ? "." : "s.");
printf("Each thread will add %d random values to the list\n", values_to_add);
struct thread_args targs = {thelist, values_to_add};
// here are our threads...
pthread_t threads[num_threads];
int i = 0;
// start up the threads; they'll start adding to the hashtable
// immediately.
for (i = 0; i < num_threads; i++) {
if (0 > pthread_create(&threads[i], NULL, worker_thread, (void*)&targs)) {
fprintf(stderr, "Error creating thread: %s\n", strerror(errno));
}
}
// wait for workers to complete
for (i = 0; i < num_threads; i++) {
pthread_join(threads[i], NULL);
}
// commented out by default...
list_print(thelist, stdout);
list_clear(thelist);
free(thelist);
exit(0);
}