#ifndef CONCURRENCY_H #define CONCURRENCY_H #include #include typedef struct _thread { int id; //UID for thread tracking. No generated thread should repeat ID pthread_t pthread; // The acutal thread...

The file necessary must be the source file that matches with the header file uploaded.


#ifndef CONCURRENCY_H #define CONCURRENCY_H #include #include typedef struct _thread { int id; //UID for thread tracking. No generated thread should repeat ID pthread_t pthread; // The acutal thread struct _threadPool *pthreadPool; // Pointer to the thread pool }thread; typedef struct _threadPool { thread **threads; //array of the threads to do work volatile int threadAliveCount; // Count of threads that are alive volatile int poolMaxThreadCount; // Max Size the thread pool can be pthread_mutex_t aliveCountLock; // Used to control count var changes pthread_mutex_t threadPoolResize; // Used when the threadPool must expand pthread_mutex_t killLock; // The kill lock will be a locked mutex. Once unlocked the threads // should begin a graceful shutdown. // Gracefully shut down threads will return a value of 101 }threadPool; /* threadPool *ThreadPoolInit(int maxSize); Description: This function will be used to initialize a threadPool struct. The threads var should allocate the space but not populate the thread structs into it. Meaning the memory pointed to by the location of the threads array will be 0 The volatile int threadAliveCount should be set to 0 on creation The volatile int poolMaxThread Count should be set to int maxSize aliveCountLock and threadPoolResize should be created with default settings and not locked on return. killLock should be locked on return. Args: int maxSize: The thread pools initialized max size. Return: On Failure or if maxSize <= 0 : null on success: a pointer to the initialized threadpool struct; */ threadpool *threadpoolinit(int maxsize); /* int addthreadtopool(threadpool *pool); description: the function will attempt to add a thread to the threadpool it should not add more threads if the threadalivecount is equal to the poolmaxthreadcount. the threadalivecount should be incremented with the addition of a new thread. the thread array should populate the threads id with the return value of pthread_self the pthread var in the thread struct should contain the created pthread the thread should have a reference to the pool struct that contains it. note: the call to pthread_create will require you to pass an "idle" function so the thread can wait to receive a job. the thread should also be allowed to cancel asynchronously if this is not done. a segmentation fault may occur. man pthread_setcanceltype() for guidence. args: threadpool *pool - a pointer to the threadpool to add the thread to return 0: on success 1: on error */ int addthreadtopool(threadpool * pool); /* int cancelthreadinpool(threadpool *pool, int threadid) description: this function will cancel a thread with the id that matches the provided threadid. the threadalivecount should be updated the thread array in the threadpool should be updated to a null value at the index of canceled thread note: if the canceled thread is joined it should have a return value pthread_canceled args: threadpool *pool - the threadpool to find the thread in int threadid - the uid assigned to the thread contained within the thread tuct return 0: on success 1: unexpected error 2: thread id not found */ int cancelthreadinpool(threadpool *pool, int threadid); /* bool destroythreadpool(threadpool *pool); description: this will terminate all threads running and free all allocated memory associated with the thread pool. this includes destroying the mutex inside the thread pool args: threadpool *pool - the threadpool to destroy return true: on success false: on failure note: there is no assert on this function and you will be graded against valgrind output. there is an expected 32 bytes due to a known valgrind bug. besides that it should be all clean. */ bool destroythreadpool(threadpool *pool); /*int getthreadreturncode(pthread_t *joinablethread, char *threadreturn); description: returns the return status of thread indicated by the threadid. this should be a non blocking call and return args: pthread_t *joinablethread - pointer to pthread_t to get its return code from char *threadreturn - the joinable threads return code return: -1 - on general error 0 - on success */ int getthreadreturncode(pthread_t *joinablethread, char **threadreturn); /* int gracefulthreadkill(threadpool *pool) description: this function will terminate all threads in the thread pool but it will leave the thread pool intact. all threads terminated this way should return 101 on exit no join should be called on the threads. this will be done by the test or your getthreadreturncode function. the thread array indices should be set to null note: this function relies on your implementation of the "idle" function in the addthreadtopool function. because of this do not develop this function first, but keep it in mind. args: threadpool *pool - the threadpool that contains the threads to shut down. return: -1 - on general error 0 - on success */ int gracefulthreadkill(threadpool *pool); #endif 0="" :="" null="" on="" success:="" a="" pointer="" to="" the="" initialized="" threadpool="" struct;="" */="" threadpool="" *threadpoolinit(int="" maxsize);="" *="" int="" addthreadtopool(threadpool="" *pool);="" description:="" the="" function="" will="" attempt="" to="" add="" a="" thread="" to="" the="" threadpool="" it="" should="" not="" add="" more="" threads="" if="" the="" threadalivecount="" is="" equal="" to="" the="" poolmaxthreadcount.="" the="" threadalivecount="" should="" be="" incremented="" with="" the="" addition="" of="" a="" new="" thread.="" the="" thread="" array="" should="" populate="" the="" threads="" id="" with="" the="" return="" value="" of="" pthread_self="" the="" pthread="" var="" in="" the="" thread="" struct="" should="" contain="" the="" created="" pthread="" the="" thread="" should="" have="" a="" reference="" to="" the="" pool="" struct="" that="" contains="" it.="" note:="" the="" call="" to="" pthread_create="" will="" require="" you="" to="" pass="" an="" "idle"="" function="" so="" the="" thread="" can="" wait="" to="" receive="" a="" job.="" the="" thread="" should="" also="" be="" allowed="" to="" cancel="" asynchronously="" if="" this="" is="" not="" done.="" a="" segmentation="" fault="" may="" occur.="" man="" pthread_setcanceltype()="" for="" guidence.="" args:="" threadpool="" *pool="" -="" a="" pointer="" to="" the="" threadpool="" to="" add="" the="" thread="" to="" return="" 0:="" on="" success="" 1:="" on="" error="" */="" int="" addthreadtopool(threadpool="" *="" pool);="" *="" int="" cancelthreadinpool(threadpool="" *pool,="" int="" threadid)="" description:="" this="" function="" will="" cancel="" a="" thread="" with="" the="" id="" that="" matches="" the="" provided="" threadid.="" the="" threadalivecount="" should="" be="" updated="" the="" thread="" array="" in="" the="" threadpool="" should="" be="" updated="" to="" a="" null="" value="" at="" the="" index="" of="" canceled="" thread="" note:="" if="" the="" canceled="" thread="" is="" joined="" it="" should="" have="" a="" return="" value="" pthread_canceled="" args:="" threadpool="" *pool="" -="" the="" threadpool="" to="" find="" the="" thread="" in="" int="" threadid="" -="" the="" uid="" assigned="" to="" the="" thread="" contained="" within="" the="" thread="" tuct="" return="" 0:="" on="" success="" 1:="" unexpected="" error="" 2:="" thread="" id="" not="" found="" */="" int="" cancelthreadinpool(threadpool="" *pool,="" int="" threadid);="" *="" bool="" destroythreadpool(threadpool="" *pool);="" description:="" this="" will="" terminate="" all="" threads="" running="" and="" free="" all="" allocated="" memory="" associated="" with="" the="" thread="" pool.="" this="" includes="" destroying="" the="" mutex="" inside="" the="" thread="" pool="" args:="" threadpool="" *pool="" -="" the="" threadpool="" to="" destroy="" return="" true:="" on="" success="" false:="" on="" failure="" note:="" there="" is="" no="" assert="" on="" this="" function="" and="" you="" will="" be="" graded="" against="" valgrind="" output.="" there="" is="" an="" expected="" 32="" bytes="" due="" to="" a="" known="" valgrind="" bug.="" besides="" that="" it="" should="" be="" all="" clean.="" */="" bool="" destroythreadpool(threadpool="" *pool);="" *int="" getthreadreturncode(pthread_t="" *joinablethread,="" char="" *threadreturn);="" description:="" returns="" the="" return="" status="" of="" thread="" indicated="" by="" the="" threadid.="" this="" should="" be="" a="" non="" blocking="" call="" and="" return="" args:="" pthread_t="" *joinablethread="" -="" pointer="" to="" pthread_t="" to="" get="" its="" return="" code="" from="" char="" *threadreturn="" -="" the="" joinable="" threads="" return="" code="" return:="" -1="" -="" on="" general="" error="" 0="" -="" on="" success="" */="" int="" getthreadreturncode(pthread_t="" *joinablethread,="" char="" **threadreturn);="" *="" int="" gracefulthreadkill(threadpool="" *pool)="" description:="" this="" function="" will="" terminate="" all="" threads="" in="" the="" thread="" pool="" but="" it="" will="" leave="" the="" thread="" pool="" intact.="" all="" threads="" terminated="" this="" way="" should="" return="" 101="" on="" exit="" no="" join="" should="" be="" called="" on="" the="" threads.="" this="" will="" be="" done="" by="" the="" test="" or="" your="" getthreadreturncode="" function.="" the="" thread="" array="" indices="" should="" be="" set="" to="" null="" note:="" this="" function="" relies="" on="" your="" implementation="" of="" the="" "idle"="" function="" in="" the="" addthreadtopool="" function.="" because="" of="" this="" do="" not="" develop="" this="" function="" first,="" but="" keep="" it="" in="" mind.="" args:="" threadpool="" *pool="" -="" the="" threadpool="" that="" contains="" the="" threads="" to="" shut="" down.="" return:="" -1="" -="" on="" general="" error="" 0="" -="" on="" success="" */="" int="" gracefulthreadkill(threadpool="" *pool);="">
Jan 20, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here