Add example of passing struct to task function

This commit is contained in:
James Morris 2023-03-05 08:16:19 -05:00
parent 3669b9bdf3
commit ae5f52c6be
2 changed files with 26 additions and 1 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
example

View File

@ -14,12 +14,22 @@
#include <stdio.h>
#include <pthread.h>
#include <stdint.h>
#include <stdlib.h>
#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;
}