Add API function to get number of working threads

This commit is contained in:
Pithikos 2017-01-17 19:58:44 +00:00
parent bc95c0ed47
commit 48ae25d35a
6 changed files with 110 additions and 0 deletions

View File

@ -50,3 +50,4 @@ For a deeper look into the documentation check in the [thpool.h](https://github.
| ***thpool_destroy(thpool)*** | This will destroy the threadpool. If jobs are currently being executed, then it will wait for them to finish. |
| ***thpool_pause(thpool)*** | All threads in the threadpool will pause no matter if they are idle or executing work. |
| ***thpool_resume(thpool)*** | If the threadpool is paused, then all threads will resume from where they were. |
| ***thpool_num_threads_working(thpool)*** | Will return the number of currently working threads. |

33
tests/api.sh Executable file
View File

@ -0,0 +1,33 @@
#! /bin/bash
#
# This file has several tests to check that the API
# works to an acceptable standard.
#
. funcs.sh
# ---------------------------- Tests -----------------------------------
function test_api {
echo "Testing API calls.."
compile src/api.c
output=`./test`
if [[ $? != 0 ]]; then
err "$output" "$output"
exit 1
fi
}
# Run tests
test_api
echo "No API errors"

View File

@ -9,6 +9,7 @@
# ---------------------------- Tests -----------------------------------
. threadpool.sh
. api.sh
. pause_resume.sh
. heap_stack_garbage.sh
. memleaks.sh

48
tests/src/api.c Normal file
View File

@ -0,0 +1,48 @@
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include "../../thpool.h"
void sleep_2_secs(){
sleep(2);
puts("SLEPT");
}
int main(int argc, char *argv[]){
int num = 0;
threadpool thpool;
/* Test if we can get the current number of working threads */
thpool = thpool_init(10);
thpool_add_work(thpool, (void*)sleep_2_secs, NULL);
thpool_add_work(thpool, (void*)sleep_2_secs, NULL);
thpool_add_work(thpool, (void*)sleep_2_secs, NULL);
thpool_add_work(thpool, (void*)sleep_2_secs, NULL);
sleep(1);
num = thpool_num_threads_working(thpool);
if (thpool_num_threads_working(thpool) != 4) {
printf("Expected 4 threads working, got %d", num);
return -1;
};
/* Test (same as above) */
thpool = thpool_init(5);
thpool_add_work(thpool, (void*)sleep_2_secs, NULL);
thpool_add_work(thpool, (void*)sleep_2_secs, NULL);
sleep(1);
num = thpool_num_threads_working(thpool);
if (num != 2) {
printf("Expected 2 threads working, got %d", num);
return -1;
};
// thpool_destroy(thpool);
// sleep(1); // Sometimes main exits before thpool_destroy finished 100%
return 0;
}

View File

@ -253,6 +253,11 @@ void thpool_resume(thpool_* thpool_p) {
}
int thpool_num_threads_working(thpool_* thpool_p){
return thpool_p->num_threads_working;
}

View File

@ -158,6 +158,28 @@ void thpool_resume(threadpool);
*/
void thpool_destroy(threadpool);
/**
* @brief Show currently working threads
*
* Working threads are the threads that are performing work (not idle).
*
* @example
* int main() {
* threadpool thpool1 = thpool_init(2);
* threadpool thpool2 = thpool_init(2);
* ..
* printf("Working threads: %d\n", thpool_num_threads_working(thpool1));
* ..
* return 0;
* }
*
* @param threadpool the threadpool of interest
* @return integer number of threads working
*/
int thpool_num_threads_working(threadpool);
#ifdef __cplusplus
}
#endif