mirror of
https://github.com/AfonsoCMSousa/Thread-Master.git
synced 2026-05-14 01:58:38 +01:00
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:
parent
b06987919d
commit
d0eb9ef193
BIN
bin/example
BIN
bin/example
Binary file not shown.
1
build.sh
1
build.sh
@ -1,5 +1,6 @@
|
|||||||
# This script builds the project
|
# This script builds the project
|
||||||
mkdir -p build
|
mkdir -p build
|
||||||
|
mkdir -p bin
|
||||||
cd ./build
|
cd ./build
|
||||||
cmake ..
|
cmake ..
|
||||||
make
|
make
|
||||||
|
|||||||
@ -61,7 +61,15 @@ 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);
|
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;
|
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");
|
fprintf(stderr, "Thread Master - Error: Failed to create thread master.\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
isThreadMasterRunning = 1;
|
isThreadMasterRunning = 1;
|
||||||
|
|
||||||
pthread_detach(thread_master);
|
pthread_detach(thread_master);
|
||||||
|
|||||||
12
src/main.c
12
src/main.c
@ -21,6 +21,8 @@ void *thread_worker(void *params)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sleep_ms(100);
|
||||||
|
|
||||||
custom->a += 10;
|
custom->a += 10;
|
||||||
|
|
||||||
printf("Thread %d - WE ARE RUNNING THIS TASK\n", param->thread_id);
|
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_t *custom_params = (custom_params_t *)malloc(sizeof(custom_params_t));
|
||||||
custom_params->a = 0;
|
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();
|
thread_master_free();
|
||||||
printf("Custom Params: %d\n", custom_params->a);
|
printf("Custom Params: %d\n", custom_params->a);
|
||||||
|
|
||||||
|
free(custom_params);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user