简体   繁体   中英

How to decrement count of barrier on thread completion using pthreads in C

I have a function: createrWorkerPool which will spawn "n" worker threads and each of them will take input as file specified in the args for pthread_create, read the file modify a shared variable by using a mutex around it and wait at a barrier until all threads have done modifying their shared variable. This operation happens in a loop a large number of times.

The problem I am facing is that lets consider there are two files file1 and file2 and file2 is much bigger in size than file1. The barrier synchronization works till completion of file1- but since it finishes execution it no longer reaches the barrier and file2 is stuck in the barrier forever.

My question is that is there a way to dynamically change the count of the number of threads the barrier is waiting on when a thread exits. so in the above case if file1 finishes early it decrements the barrier count from 2 to 1 so file1 can proceed with its execution. I tried looking at the man page but don't see any function.Example code

pthread_mutex_t m1;
pthread_barrier_t b1;
//common function executed by each worker thread
void* doWork(void* arg) {
    const char* file = arg;
    while(1) {
        pthread_mutex_lock(&m1);
        // Modify shared variable
        pthread_mutex_unlock(&m1);
        // Wait for all threads to finish modifying shared variable
        pthread_barrier_wait(&b1);
        // Once all threads reach barrier check state of shared variable and do some task based on state
        // check for condition if true break out of loop

    }
    return 0;
}

So basically thread1 manipulating file1 finishes before and thread2 is stuck at the barrier forever

You can't really change the barrier count while the barrier is in use like that.

Presumably the problem is that the condition that is tested to break out of the loop is not true for all files at the same time - ie, each thread might execute a different number of loops.

If this is the case, one solution is to have each thread that finishes early continue to loop around, but do nothing but wait on the barrier in each loop. Then arrange for the threads to all exit together - something like this:

void* doWork(void* arg)
{
    const char* file = arg;
    int work_done = 0;

    while(1) {
        if (work_done)
        {
            if (all_threads_done)
                break;

            pthread_barrier_wait(&b1);
            continue;
        }

        pthread_mutex_lock(&m1);
        // Modify shared variable
        pthread_mutex_unlock(&m1);
        // Wait for all threads to finish modifying shared variable
        pthread_barrier_wait(&b1);
        // Once all threads reach barrier check state of shared variable and do some task based on state
        if (finish_condition)
            work_done = 1;
    }
    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM