From ae5f52c6be248e24f479f257b01d31bb3934ad09 Mon Sep 17 00:00:00 2001 From: James Morris Date: Sun, 5 Mar 2023 08:16:19 -0500 Subject: [PATCH] Add example of passing struct to task function --- .gitignore | 1 + example.c | 26 +++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..33a9488 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +example diff --git a/example.c b/example.c index 22ea241..6d9a055 100644 --- a/example.c +++ b/example.c @@ -14,12 +14,22 @@ #include #include #include +#include #include "thpool.h" +typedef struct +{ + int id; +} workload; + void task(void *arg){ printf("Thread #%u working on %d\n", (int)pthread_self(), (int) arg); } +void task_via_struct(void *arg){ + workload wl = *((workload *)arg); + printf("Thread #%u working on %d\n", (int)pthread_self(), wl.id); +} int main(){ @@ -33,8 +43,22 @@ int main(){ }; thpool_wait(thpool); + + puts("Adding 40 tasks to threadpool using struct"); + workload *instructions[40]; + for (i=0; i<40; i++){ + instructions[i]= malloc(sizeof(workload)); + instructions[i]->id=i; + thpool_add_work(thpool, task_via_struct, instructions[i]); + }; + + thpool_wait(thpool); + puts("Killing threadpool"); thpool_destroy(thpool); - + + for(i=0;i<40;i++){ + free(instructions[i]); + } return 0; }