Compare commits

..

No commits in common. "master" and "v1.0" have entirely different histories.
master ... v1.0

2 changed files with 26 additions and 14 deletions

View File

@ -7,8 +7,6 @@ Hey, I'm Afonso, a passionate developer who loves low-level C/C++ and even some
I originally created this library as a proof-of-concept, but it quickly evolved into something much bigger. I originally created this library as a proof-of-concept, but it quickly evolved into something much bigger.
If you're interested in learning how it works, check out the [GUIDE.md](https://github.com/AfonsoCMSousa/Thread-Master/blob/master/GUIDE.md) and the `main.c` file, along with all the headers in the `include/` directory. If you're interested in learning how it works, check out the [GUIDE.md](https://github.com/AfonsoCMSousa/Thread-Master/blob/master/GUIDE.md) and the `main.c` file, along with all the headers in the `include/` directory.
Of course! Feel free to create a new [issue](https://github.com/AfonsoCMSousa/Thread-Master/issues) if you ever find any when using the library.
--- ---
## Features ## Features
@ -39,8 +37,6 @@ Or compiple manually:
gcc -o my_program src/main.c include/threadlib/threadlib.c -pthread gcc -o my_program src/main.c include/threadlib/threadlib.c -pthread
``` ```
Check the [releases](https://github.com/AfonsoCMSousa/Thread-Master/releases/latest) for a detailed instalation guide and some libraries (.a) (.dll) or even (.lib).
## All Available Functions ## All Available Functions
### thread_master_init ### thread_master_init
@ -102,9 +98,9 @@ Assigns a new job to the thread workers.
- `include/threadlib/threadlib.h`: Header file for the thread library. - `include/threadlib/threadlib.h`: Header file for the thread library.
- `include/threadlib/threadlib.c`: Implementation of the thread library. - `include/threadlib/threadlib.c`: Implementation of the thread library.
## A big thanks to: ### A big thanks to:
- Professor **Marco Ferreira** - Professor **Marco Ferreira** - <marco.ferreira@ipleiria.pt>
- Professor **Patrício Domingues** - Professor **Patrício Domingues** - <patricio.domingues@ipleiria.pt>
## Licence: ## Licence:
This content falls under [Apache License Version 2.0](https://github.com/AfonsoCMSousa/Thread-Master/blob/master/LICENSE) This content falls under [Apache License Version 2.0](https://github.com/AfonsoCMSousa/Thread-Master/blob/master/LICENSE)

View File

@ -1,4 +1,4 @@
#include "threadlib.h" #include "threadlib/threadlib.h"
int __max_threads; int __max_threads;
unsigned char isThreadMasterRunning = 0; unsigned char isThreadMasterRunning = 0;
@ -12,7 +12,7 @@ void __thread_mutex_init__(pthread_mutex_t *mutex)
{ {
if (pthread_mutex_init(mutex, NULL) != 0) if (pthread_mutex_init(mutex, NULL) != 0)
{ {
fprintf(stderr, "Thread Master - Error: Failed to initialize mutex.\n"); fprintf(stderr, "Error: Failed to initialize mutex.\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
@ -26,7 +26,7 @@ void __thread_mutex_destroy__(pthread_mutex_t *mutex)
{ {
if (pthread_mutex_destroy(mutex) != 0) if (pthread_mutex_destroy(mutex) != 0)
{ {
fprintf(stderr, "Thread Master - Error: Failed to destroy mutex.\n"); fprintf(stderr, "Error: Failed to destroy mutex.\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
@ -43,7 +43,7 @@ void *__thread_master_init__(void *params)
listT = (thread *)malloc(param->max_threads * sizeof(thread)); listT = (thread *)malloc(param->max_threads * sizeof(thread));
if (listT == NULL) if (listT == NULL)
{ {
fprintf(stderr, "Thread Master - Error: Failed to allocate memory for thread pool.\n"); fprintf(stderr, "Tread Master - Error: Failed to allocate memory for thread pool.\n");
return NULL; return NULL;
} }
@ -62,6 +62,22 @@ void *__thread_master_init__(void *params)
printf("Thread Master - Success: Ready.\n"); printf("Thread Master - Success: Ready.\n");
thread_master_assign_new_job(param->__thread_worker__, param->custom_params); thread_master_assign_new_job(param->__thread_worker__, param->custom_params);
/* for (int i = 0; i < param->max_threads; i++)
{
// 3 - Create the threads
if (pthread_create(&(listT[i].thread), NULL, param->__thread_worker__, &(listT[i].worker_param)) != 0)
{
fprintf(stderr, "Error: Failed to create thread %d.\n", i + 1);
exit(EXIT_FAILURE);
}
listT[i].worker_param.status = BUSY;
}
for (int i = 0; i < param->max_threads; i++)
{
pthread_join(listT[i].thread, NULL);
}*/
return NULL; return NULL;
} }
@ -81,7 +97,7 @@ void thread_master_init(int max_threads, void *(*__thread_worker__)(void *), voi
if (pthread_create(&thread_master, NULL, __thread_master_init__, (void *)param) != 0) if (pthread_create(&thread_master, NULL, __thread_master_init__, (void *)param) != 0)
{ {
fprintf(stderr, "Thread Master - Error: Failed to create thread master.\n"); fprintf(stderr, "Error: Failed to create thread master.\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
isThreadMasterRunning = 1; isThreadMasterRunning = 1;
@ -120,7 +136,7 @@ void thread_master_get_status()
printf("Thread Master - Max Threads: %d\n", __max_threads); printf("Thread Master - Max Threads: %d\n", __max_threads);
printf("Param: %d\n", param->max_threads); printf("Param: %d\n", param->max_threads);
#endif // DEBUG #endif // DEBUG
sleep_ms(100); sleep(1);
} while (__max_threads == 0); } while (__max_threads == 0);
pthread_mutex_lock(&param->mutex); pthread_mutex_lock(&param->mutex);
@ -171,4 +187,4 @@ void thread_master_free()
free(param); free(param);
free(listT); free(listT);
printf("Thread Master - Free and Stopped\n"); printf("Thread Master - Free and Stopped\n");
} }