please help me with this. will upvote if correct threadpool.c #include #include #include
Posted: Thu Jul 14, 2022 2:11 pm
please help me with this. will upvote if correct
threadpool.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <assert.h>
#include "threadpool.h"
struct tpool {
/* TO BE COMPLETED BY THE STUDENT */
};
/* Function executed by each pool worker thread. This functionis
* responsible for running individual tasks. The functioncontinues
* running as long as either the pool is not yet joined, or thereare
* unstarted tasks to run. If there are no tasks to run, and thepool
* has not yet been joined, the worker thread must beblocked.
*
* Parameter: param: The pool associated to the thread.
* Returns: nothing.
*/
void *run_tasks(void *param) {
tpool_t pool = param;
/* TO BE COMPLETED BY THE STUDENT */
return NULL;
}
/* Creates (allocates) and initializes a new thread pool. Alsocreates
* `num_threads` worker threads associated to the pool, sothat
* `num_threads` tasks can run in parallel at any given time.
*
* Parameter: num_threads: Number of worker threads to becreated.
* Returns: a pointer to the new thread pool object.
*/
tpool_t tpool_create(unsigned int num_threads) {
/* TO BE COMPLETED BY THE STUDENT */
}
/* Schedules a new task, to be executed by one of the workerthreads
* associated to the pool. The task is represented by function`fun`,
* which receives the pool and a generic pointer as parameters.If any
* of the worker threads is available, `fun` is startedimmediately by
* one of the worker threads. If all of the worker threads arebusy,
* `fun` is scheduled to be executed when a worker threadbecomes
* available. Tasks are retrieved by individual worker threads inthe
* order in which they are scheduled, though due to the natureof
* concurrency they may not start exactly in the same order.This
* function returns immediately, and does not wait for `fun`to
* complete.
*
* Parameters: pool: the pool that is expected to run thetask.
* fun: the function that should be executed.
* arg: the argument to be passed to fun.
*/
void tpool_schedule_task(tpool_t pool, void (*fun)(tpool_t, void*), void *arg) {
/* TO BE COMPLETED BY THE STUDENT */
}
/* Blocks until the thread pool has no more scheduled tasks;then,
* joins all worker threads, and frees the pool and allrelated
* resources. Once this function returns, no additional tasks canbe
* scheduled, and the thread pool resources aredestroyed/freed.
*
* Parameters: pool: the pool to be joined.
*/
void tpool_join(tpool_t pool) {
/* TO BE COMPLETED BY THE STUDENT */
}
Posted: Thu Jul 14, 2022 2:11 pm
please help me with this. will upvote if correct
threadpool.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <assert.h>
#include "threadpool.h"
struct tpool {
/* TO BE COMPLETED BY THE STUDENT */
};
/* Function executed by each pool worker thread. This functionis
* responsible for running individual tasks. The functioncontinues
* running as long as either the pool is not yet joined, or thereare
* unstarted tasks to run. If there are no tasks to run, and thepool
* has not yet been joined, the worker thread must beblocked.
*
* Parameter: param: The pool associated to the thread.
* Returns: nothing.
*/
void *run_tasks(void *param) {
tpool_t pool = param;
/* TO BE COMPLETED BY THE STUDENT */
return NULL;
}
/* Creates (allocates) and initializes a new thread pool. Alsocreates
* `num_threads` worker threads associated to the pool, sothat
* `num_threads` tasks can run in parallel at any given time.
*
* Parameter: num_threads: Number of worker threads to becreated.
* Returns: a pointer to the new thread pool object.
*/
tpool_t tpool_create(unsigned int num_threads) {
/* TO BE COMPLETED BY THE STUDENT */
}
/* Schedules a new task, to be executed by one of the workerthreads
* associated to the pool. The task is represented by function`fun`,
* which receives the pool and a generic pointer as parameters.If any
* of the worker threads is available, `fun` is startedimmediately by
* one of the worker threads. If all of the worker threads arebusy,
* `fun` is scheduled to be executed when a worker threadbecomes
* available. Tasks are retrieved by individual worker threads inthe
* order in which they are scheduled, though due to the natureof
* concurrency they may not start exactly in the same order.This
* function returns immediately, and does not wait for `fun`to
* complete.
*
* Parameters: pool: the pool that is expected to run thetask.
* fun: the function that should be executed.
* arg: the argument to be passed to fun.
*/
void tpool_schedule_task(tpool_t pool, void (*fun)(tpool_t, void*), void *arg) {
/* TO BE COMPLETED BY THE STUDENT */
}
/* Blocks until the thread pool has no more scheduled tasks;then,
* joins all worker threads, and frees the pool and allrelated
* resources. Once this function returns, no additional tasks canbe
* scheduled, and the thread pool resources aredestroyed/freed.
*
* Parameters: pool: the pool to be joined.
*/
void tpool_join(tpool_t pool) {
/* TO BE COMPLETED BY THE STUDENT */
}
threadpool.c
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <assert.h>
#include "threadpool.h"
struct tpool {
/* TO BE COMPLETED BY THE STUDENT */
};
/* Function executed by each pool worker thread. This functionis
* responsible for running individual tasks. The functioncontinues
* running as long as either the pool is not yet joined, or thereare
* unstarted tasks to run. If there are no tasks to run, and thepool
* has not yet been joined, the worker thread must beblocked.
*
* Parameter: param: The pool associated to the thread.
* Returns: nothing.
*/
void *run_tasks(void *param) {
tpool_t pool = param;
/* TO BE COMPLETED BY THE STUDENT */
return NULL;
}
/* Creates (allocates) and initializes a new thread pool. Alsocreates
* `num_threads` worker threads associated to the pool, sothat
* `num_threads` tasks can run in parallel at any given time.
*
* Parameter: num_threads: Number of worker threads to becreated.
* Returns: a pointer to the new thread pool object.
*/
tpool_t tpool_create(unsigned int num_threads) {
/* TO BE COMPLETED BY THE STUDENT */
}
/* Schedules a new task, to be executed by one of the workerthreads
* associated to the pool. The task is represented by function`fun`,
* which receives the pool and a generic pointer as parameters.If any
* of the worker threads is available, `fun` is startedimmediately by
* one of the worker threads. If all of the worker threads arebusy,
* `fun` is scheduled to be executed when a worker threadbecomes
* available. Tasks are retrieved by individual worker threads inthe
* order in which they are scheduled, though due to the natureof
* concurrency they may not start exactly in the same order.This
* function returns immediately, and does not wait for `fun`to
* complete.
*
* Parameters: pool: the pool that is expected to run thetask.
* fun: the function that should be executed.
* arg: the argument to be passed to fun.
*/
void tpool_schedule_task(tpool_t pool, void (*fun)(tpool_t, void*), void *arg) {
/* TO BE COMPLETED BY THE STUDENT */
}
/* Blocks until the thread pool has no more scheduled tasks;then,
* joins all worker threads, and frees the pool and allrelated
* resources. Once this function returns, no additional tasks canbe
* scheduled, and the thread pool resources aredestroyed/freed.
*
* Parameters: pool: the pool to be joined.
*/
void tpool_join(tpool_t pool) {
/* TO BE COMPLETED BY THE STUDENT */
}