Thread pool
|
This library contains the thread-pool and future mechanism (known from C++ std::future) implemented in C.
The thread_pool_init call initiates the argument pointed to by pool as the new pool in which it will have pool_size of serving threads to complete the task. Library correctness is only guaranteed if each pool created by thread_pool_init is destroyed by calling thread_pool_destroy with an argument representing these pools.
A call to defer(pool, runnable) instructs the pool to execute the task described by the argument runnable. Function arguments are passed by the args pointer. In the args field there is the length of the buffer available for writing and reading located under this pointer.
The tasks commissioned by defer are concurrent and independent of each other as far as it's possible. Pool size is the limit of concurrent tasks. The pot during its operation no should have more threads than specified by the pool_size parameter. Created threads are kept alive until thread_pool_destroy.
Running async initializes memory for future, assigns callable to calculate the value in the pool and returns it to future mechanism.
User can now:
This is the program that uses the thread-pool to calculate the row-sums in matrix. The first two lines contain two numbers k and n (number of rows and columns). Then the program should read k*n lines with data. Every line contains two numbers v and t. Number v in line i (number of row is defined by calculating floor(i/n)) specify the value. Number t specify the time (in milliseconds) needed for calculating the v value. Here is the example of correct input data:
Input date above represents matrix below.
Running the program:
should have the same result as stated below
This is the program that will calculate the n! value using three threads and future mechanism: Running:
should result with
Should compile the library to build/libasyncc.
You can run tests by