Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cpp usage errors #121

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions thpool.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ typedef struct thpool_{
volatile int num_threads_working; /* threads currently working */
pthread_mutex_t thcount_lock; /* used for thread count etc */
pthread_cond_t threads_all_idle; /* signal to thpool_wait */
jobqueue jobqueue; /* job queue */
Copy link
Owner

@Pithikos Pithikos Dec 18, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this is the main issue with C++? Do you think we could simply use jobqueue_t instead?
Namely I'm a bit hesitant, since renaming to job_queue breaks naming convention with other functions

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think of a simple way to solve it by using extern "C" to avoid problems in C++.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But there is also a problem with this code style. The struct name should not be consistent with the variable name.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using jobqueue_t may be a better choice.

jobqueue job_queue; /* job queue */
} thpool_;


Expand Down Expand Up @@ -144,7 +144,7 @@ struct thpool_* thpool_init(int num_threads){
thpool_p->num_threads_working = 0;

/* Initialise the job queue */
if (jobqueue_init(&thpool_p->jobqueue) == -1){
if (jobqueue_init(&thpool_p->job_queue) == -1){
err("thpool_init(): Could not allocate memory for job queue\n");
free(thpool_p);
return NULL;
Expand All @@ -154,7 +154,7 @@ struct thpool_* thpool_init(int num_threads){
thpool_p->threads = (struct thread**)malloc(num_threads * sizeof(struct thread *));
if (thpool_p->threads == NULL){
err("thpool_init(): Could not allocate memory for threads\n");
jobqueue_destroy(&thpool_p->jobqueue);
jobqueue_destroy(&thpool_p->job_queue);
free(thpool_p);
return NULL;
}
Expand Down Expand Up @@ -193,7 +193,7 @@ int thpool_add_work(thpool_* thpool_p, void (*function_p)(void*), void* arg_p){
newjob->arg=arg_p;

/* add job to queue */
jobqueue_push(&thpool_p->jobqueue, newjob);
jobqueue_push(&thpool_p->job_queue, newjob);

return 0;
}
Expand All @@ -202,7 +202,7 @@ int thpool_add_work(thpool_* thpool_p, void (*function_p)(void*), void* arg_p){
/* Wait until all jobs have finished */
void thpool_wait(thpool_* thpool_p){
pthread_mutex_lock(&thpool_p->thcount_lock);
while (thpool_p->jobqueue.len || thpool_p->num_threads_working) {
while (thpool_p->job_queue.len || thpool_p->num_threads_working) {
pthread_cond_wait(&thpool_p->threads_all_idle, &thpool_p->thcount_lock);
}
pthread_mutex_unlock(&thpool_p->thcount_lock);
Expand All @@ -225,19 +225,19 @@ void thpool_destroy(thpool_* thpool_p){
double tpassed = 0.0;
time (&start);
while (tpassed < TIMEOUT && thpool_p->num_threads_alive){
bsem_post_all(thpool_p->jobqueue.has_jobs);
bsem_post_all(thpool_p->job_queue.has_jobs);
time (&end);
tpassed = difftime(end,start);
}

/* Poll remaining threads */
while (thpool_p->num_threads_alive){
bsem_post_all(thpool_p->jobqueue.has_jobs);
bsem_post_all(thpool_p->job_queue.has_jobs);
sleep(1);
}

/* Job queue cleanup */
jobqueue_destroy(&thpool_p->jobqueue);
jobqueue_destroy(&thpool_p->job_queue);
/* Deallocs */
int n;
for (n=0; n < threads_total; n++){
Expand Down Expand Up @@ -354,7 +354,7 @@ static void* thread_do(struct thread* thread_p){

while(threads_keepalive){

bsem_wait(thpool_p->jobqueue.has_jobs);
bsem_wait(thpool_p->job_queue.has_jobs);

if (threads_keepalive){

Expand All @@ -365,7 +365,7 @@ static void* thread_do(struct thread* thread_p){
/* Read job from queue and execute it */
void (*func_buff)(void*);
void* arg_buff;
job* job_p = jobqueue_pull(&thpool_p->jobqueue);
job* job_p = jobqueue_pull(&thpool_p->job_queue);
if (job_p) {
func_buff = job_p->function;
arg_buff = job_p->arg;
Expand Down