-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.c
More file actions
81 lines (61 loc) · 1.46 KB
/
example.c
File metadata and controls
81 lines (61 loc) · 1.46 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "macroThreadPool.h"
struct myArgs
{
int id;
char *str;
};
static void workerFunction(struct myArgs args);
/* this could also take a myArgs pointer instead of a value copy but since
* nothing is being passed out of the function into the struct this is easier
* as it requires no additional allocation */
MACRO_THREAD_POOL_COMPLETE(foo, struct myArgs, workerFunction);
static void workerFunction(struct myArgs args)
{
const int thread_id = fooGetThreadId();
usleep(rand() % 5);
fprintf(stdout, "thread: %d, job: %d, str: %s\n",
thread_id, args.id, args.str);
}
static void printHelp(void)
{
fputs("Simple Macro Thread Pool Example Program:\n\n", stdout);
fputs("Usage:\n", stdout);
fputs("\t./mtpExample [args]...\n", stdout);
fputs("\n", stdout);
fputs("Must supply a non-zero number of args, ideally multiple\n\n",
stdout);
}
int main(int argc, char **argv)
{
struct fooThreadPool *pool = fooNewThreadPool(5, 10);
unsigned int seed = 0;
size_t i;
if (argc < 2)
{
printHelp();
return 1;
}
/* Lazy way to initialize random number generator */
for (i = 0; argv[1][i] != '\0'; i++)
{
seed += argv[1][i];
}
srand(seed);
if (pool == NULL)
{
fprintf(stderr, "Failed to allocate thread pool\n");
return 1;
}
for (i = 1; i < argc; i++)
{
struct myArgs args = {0};
args.id = i;
args.str = argv[i];
fooEnqueueJob(pool, args);
}
fooCleanupThreadPool(pool);
return 0;
}