-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'assignments-base/assignment4'
- Loading branch information
Showing
5 changed files
with
79 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
assignment3 | ||
assignment4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include "threading.h" | ||
#include <unistd.h> | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
|
||
// Optional: use these functions to add debug or error prints to your application | ||
#define DEBUG_LOG(msg,...) | ||
//#define DEBUG_LOG(msg,...) printf("threading: " msg "\n" , ##__VA_ARGS__) | ||
#define ERROR_LOG(msg,...) printf("threading ERROR: " msg "\n" , ##__VA_ARGS__) | ||
|
||
void* threadfunc(void* thread_param) | ||
{ | ||
|
||
// TODO: wait, obtain mutex, wait, release mutex as described by thread_data structure | ||
// hint: use a cast like the one below to obtain thread arguments from your parameter | ||
//struct thread_data* thread_func_args = (struct thread_data *) thread_param; | ||
return thread_param; | ||
} | ||
|
||
|
||
bool start_thread_obtaining_mutex(pthread_t *thread, pthread_mutex_t *mutex,int wait_to_obtain_ms, int wait_to_release_ms) | ||
{ | ||
/** | ||
* TODO: allocate memory for thread_data, setup mutex and wait arguments, pass thread_data to created thread | ||
* using threadfunc() as entry point. | ||
* | ||
* return true if successful. | ||
* | ||
* See implementation details in threading.h file comment block | ||
*/ | ||
return false; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include <stdbool.h> | ||
#include <pthread.h> | ||
|
||
/** | ||
* This structure should be dynamically allocated and passed as | ||
* an argument to your thread using pthread_create. | ||
* It should be returned by your thread so it can be freed by | ||
* the joiner thread. | ||
*/ | ||
struct thread_data{ | ||
/* | ||
* TODO: add other values your thread will need to manage | ||
* into this structure, use this structure to communicate | ||
* between the start_thread_obtaining_mutex function and | ||
* your thread implementation. | ||
*/ | ||
|
||
/** | ||
* Set to true if the thread completed with success, false | ||
* if an error occurred. | ||
*/ | ||
bool thread_complete_success; | ||
}; | ||
|
||
|
||
/** | ||
* Start a thread which sleeps @param wait_to_obtain_ms number of milliseconds, then obtains the | ||
* mutex in @param mutex, then holds for @param wait_to_release_ms milliseconds, then releases. | ||
* The start_thread_obtaining_mutex function should only start the thread and should not block | ||
* for the thread to complete. | ||
* The start_thread_obtaining_mutex function should use dynamic memory allocation for thread_data | ||
* structure passed into the thread. The number of threads active should be limited only by the | ||
* amount of available memory. | ||
* The thread started should return a pointer to the thread_data structure when it exits, which can be used | ||
* to free memory as well as to check thread_complete_success for successful exit. | ||
* If a thread was started succesfully @param thread should be filled with the pthread_create thread ID | ||
* coresponding to the thread which was started. | ||
* @return true if the thread could be started, false if a failure occurred. | ||
*/ | ||
bool start_thread_obtaining_mutex(pthread_t *thread, pthread_mutex_t *mutex,int wait_to_obtain_ms, int wait_to_release_ms); |