mirror of
https://github.com/Pithikos/C-Thread-Pool
synced 2024-11-22 13:41:18 +03:00
Add example of passing struct to task function
This commit is contained in:
parent
3669b9bdf3
commit
ae5f52c6be
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
example
|
24
example.c
24
example.c
@ -14,12 +14,22 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include "thpool.h"
|
#include "thpool.h"
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
int id;
|
||||||
|
} workload;
|
||||||
|
|
||||||
void task(void *arg){
|
void task(void *arg){
|
||||||
printf("Thread #%u working on %d\n", (int)pthread_self(), (int) 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(){
|
int main(){
|
||||||
|
|
||||||
@ -33,8 +43,22 @@ int main(){
|
|||||||
};
|
};
|
||||||
|
|
||||||
thpool_wait(thpool);
|
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");
|
puts("Killing threadpool");
|
||||||
thpool_destroy(thpool);
|
thpool_destroy(thpool);
|
||||||
|
|
||||||
|
for(i=0;i<40;i++){
|
||||||
|
free(instructions[i]);
|
||||||
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user