mirror of
https://github.com/Pithikos/C-Thread-Pool
synced 2024-11-22 05:31:18 +03:00
Simplified
This commit is contained in:
parent
bf44140c4f
commit
9441b849dd
43
example.c
43
example.c
@ -1,61 +1,46 @@
|
||||
/*
|
||||
* This is just an example on how to use the thpool library
|
||||
* WHAT THIS EXAMPLE DOES
|
||||
*
|
||||
* We create a pool of 4 threads and then add 20 tasks to the pool(10 task1
|
||||
* functions and 10 task2 functions).
|
||||
* We create a pool of 4 threads and then add 40 tasks to the pool(20 task1
|
||||
* functions and 20 task2 functions).
|
||||
*
|
||||
* Task1 doesn't take any arguments. Task2 takes an integer. Task2 is used to show
|
||||
* how to add work to the thread pool with an argument.
|
||||
* task1 and task2 simply print which thread is running them.
|
||||
*
|
||||
* As soon as we add the tasks to the pool, the threads will run them. One thread
|
||||
* may run x tasks in a row so if you see as output the same thread running several
|
||||
* tasks, it's not an error.
|
||||
*
|
||||
* All jobs will not be completed and in fact maybe even none will. You can add a sleep()
|
||||
* function if you want to complete all tasks in this test file to be able and see clearer
|
||||
* what is going on.
|
||||
*
|
||||
* */
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "thpool.h"
|
||||
|
||||
|
||||
|
||||
/* Some arbitrary task 1 */
|
||||
void task1(){
|
||||
printf("# Thread working: %u\n", (int)pthread_self());
|
||||
printf(" Task 1 running..\n");
|
||||
printf("# Thread %u working on task1\n", (int)pthread_self());
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* Some arbitrary task 2 */
|
||||
void task2(int a){
|
||||
printf("# Thread working: %u\n", (int)pthread_self());
|
||||
printf(" Task 2 running..\n");
|
||||
printf("%d\n", a);
|
||||
void task2(){
|
||||
printf("# Thread %u working on task2\n", (int)pthread_self());
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main(){
|
||||
int i;
|
||||
|
||||
thpool_t* threadpool; /* make a new thread pool structure */
|
||||
threadpool=thpool_init(4); /* initialise it to 4 number of threads */
|
||||
puts("Making threadpool with 4 threads");
|
||||
thpool threadpool = thpool_init(4);
|
||||
|
||||
|
||||
puts("Adding 20 tasks to threadpool");
|
||||
int a=54;
|
||||
for (i=0; i<10; i++){
|
||||
puts("Adding 40 tasks to threadpool");
|
||||
int i;
|
||||
for (i=0; i<20; i++){
|
||||
thpool_add_work(threadpool, (void*)task1, NULL);
|
||||
thpool_add_work(threadpool, (void*)task2, (void*)a);
|
||||
thpool_add_work(threadpool, (void*)task2, NULL);
|
||||
};
|
||||
|
||||
|
||||
puts("Will kill threadpool");
|
||||
puts("Killing threadpool");
|
||||
thpool_destroy(threadpool);
|
||||
|
||||
return 0;
|
||||
|
Loading…
Reference in New Issue
Block a user