mirror of
https://github.com/AfonsoCMSousa/Thread-Master.git
synced 2026-05-14 03:38:38 +01:00
Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
10c1512671 | ||
|
|
5638b576d5 | ||
| b06987919d | |||
|
|
8a0335c8da | ||
|
|
1079ec010c | ||
|
|
f38a1f3990 |
10
README.md
10
README.md
@ -7,6 +7,8 @@ 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
|
||||||
@ -37,6 +39,8 @@ 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
|
||||||
@ -98,9 +102,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** - <marco.ferreira@ipleiria.pt>
|
- Professor **Marco Ferreira**
|
||||||
- Professor **Patrício Domingues** - <patricio.domingues@ipleiria.pt>
|
- Professor **Patrício Domingues**
|
||||||
|
|
||||||
## 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)
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
#include "threadlib/threadlib.h"
|
#include "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, "Error: Failed to initialize mutex.\n");
|
fprintf(stderr, "Thread Master - 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, "Error: Failed to destroy mutex.\n");
|
fprintf(stderr, "Thread Master - 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, "Tread Master - Error: Failed to allocate memory for thread pool.\n");
|
fprintf(stderr, "Thread Master - Error: Failed to allocate memory for thread pool.\n");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,22 +62,6 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,7 +81,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, "Error: Failed to create thread master.\n");
|
fprintf(stderr, "Thread Master - Error: Failed to create thread master.\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
isThreadMasterRunning = 1;
|
isThreadMasterRunning = 1;
|
||||||
@ -136,7 +120,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(1);
|
sleep_ms(100);
|
||||||
} while (__max_threads == 0);
|
} while (__max_threads == 0);
|
||||||
|
|
||||||
pthread_mutex_lock(¶m->mutex);
|
pthread_mutex_lock(¶m->mutex);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user