Enhance thread management and build script

- Added check for thread worker in thread_master_init to provide informative error message.
- Updated main function to properly assign jobs to the thread worker.
- Modified build script to create a bin directory if it doesn't exist.
This commit is contained in:
Afonso Clerigo Mendes de Sousa 2025-03-18 19:11:54 +00:00
parent b06987919d
commit d0eb9ef193
5 changed files with 22 additions and 2 deletions

Binary file not shown.

View File

@ -1,5 +1,6 @@
# This script builds the project
mkdir -p build
mkdir -p bin
cd ./build
cmake ..
make

BIN
example

Binary file not shown.

View File

@ -61,7 +61,15 @@ void *__thread_master_init__(void *params)
printf("Thread Master - Success: Ready.\n");
thread_master_assign_new_job(param->__thread_worker__, param->custom_params);
if (param->__thread_worker__ == NULL)
{
fprintf(stderr, "Thread Master - Info: No task given, use \"thread_master_assign_new_job\" to run a task\n");
}
else
{
thread_master_assign_new_job(param->__thread_worker__, param->custom_params);
}
return NULL;
}
@ -84,6 +92,7 @@ void thread_master_init(int max_threads, void *(*__thread_worker__)(void *), voi
fprintf(stderr, "Thread Master - Error: Failed to create thread master.\n");
exit(EXIT_FAILURE);
}
isThreadMasterRunning = 1;
pthread_detach(thread_master);

View File

@ -21,6 +21,8 @@ void *thread_worker(void *params)
return NULL;
}
sleep_ms(100);
custom->a += 10;
printf("Thread %d - WE ARE RUNNING THIS TASK\n", param->thread_id);
@ -33,9 +35,17 @@ int main(void)
{
custom_params_t *custom_params = (custom_params_t *)malloc(sizeof(custom_params_t));
custom_params->a = 0;
thread_master_init(MAX_THREAD, thread_worker, NULL);
thread_master_init(MAX_THREAD, NULL, custom_params);
sleep_ms(1000);
thread_master_assign_new_job(thread_worker, custom_params);
thread_master_assign_new_job(thread_worker, custom_params);
thread_master_assign_new_job(thread_worker, custom_params);
thread_master_free();
printf("Custom Params: %d\n", custom_params->a);
free(custom_params);
return 0;
}