Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example of passing struct to task function #119

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
example
26 changes: 25 additions & 1 deletion example.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(){

Expand All @@ -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;
}