Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6859971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:21:01+00:00 2026-05-27T02:21:01+00:00

Pseudo code: void * thread_start(void *arg) { while (1) { /* for each column.

  • 0

Pseudo code:

void * thread_start(void *arg) {
    while (1) {
        /* for each column. Only run columns the thread_num
           is assigned to */
        column_count = thread_num;
        for (; column_count < dim - 1; column_count+=threads) {
             /* do thread work for data chunk */
        }

        /* barrier */
        barrier_result = pthread_barrier_wait(barrier);

        if (not_finished == 0) {
            /* finished */
            break;
            /* break while loop, end thread.
               The barrier is now broken because it is no longer
               getting called by this thread */
        }

        /* we are not finished, loop around and 
           do thread work on next data chunk */
    }
}

My issue with barriers is how do you deal with threads ending before other threads?

A barrier means every thread has to wait for every other thread.

What techniques are there for ensuring all threads end at the same time?

I tried continueing the loop but ignoring the “thread work”, but in this case all 8 threads finish and there’s no sensible way to tell the threads “your all done, you can all exit now”

Edit:

Algorithm:

  1. Run operations on segment of piece of data
  2. if the thread’s segment of piece of data is finished terminate.
  3. Barrier wait.
  4. One thread replaces the piece of data with some new data
  5. repeat 1.

Edit 2:

Is there any elegant way to overwrite the barrier with a barrier that is one size smaller? (without putting a mutex around the barrier)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:21:02+00:00Added an answer on May 27, 2026 at 2:21 am

    You ensure threads end in the same time by the very nature of the program logic. If you algorithm calls for threads to exit at different times, then barriers are not a suitable concurrency control mechanism for that concrete algorithm.

    EDIT:

    You could atomically increment a “finished_count” and loop, not doing any work, other then participating the barrier, while that number is less than the barrier count (protecting read/writes to finished_count with appropriate mutexes).

    EDIT:
    I’m not sure if I understand your added description well, I just wrote this based on your original code, maybe it’ll work for you:

    int finished_count;
    
    void * thread_start(void *arg) {
        int finished = 0;
    
        while (1) {
    
            if (finished) {
                pthread_mutex_lock (&finished_lock);
                if (finished_count == 8) {
                    pthread_mutex_unlock (&finished_lock);
                    break;
                }
                pthread_mutex_unlock (&finished_lock);
            } else {
                /* for each column. Only run columns the thread_num
                   is assigned to */
                column_count = thread_num;
                for (; column_count < dim - 1; column_count+=threads) {
                    /* do thread work for data chunk */
                }
    
                /* set "finished" as appropriate */
    
                /* Check if we have just finished.  */
                if (finished) {
                    pthread_mutex_lock (&finished_lock);
                    ++finished_count;
                    pthread_mutex_unlock (&finished_lock);
                }
            }
    
            barrier_result = pthread_barrier_wait(barrier);
        }
    
        return 0;
    }
    

    EDIT:

    To clarify my comment above about copying over or reinitializing, POSIX says:

    http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_barrier_init.html

    The results are undefined if pthread_barrier_init() is called specifying an already initialized barrier.

    and

    Only the object referenced by barrier may be used for performing synchronization. The result of referring to copies of that object in calls to pthread_barrier_destroy() or pthread_barrier_wait() is undefined.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The basic pseudo code looks like this: void myFunction() { int size = 10;
Pseudo-code: for each x in someArray { // possibly add an element to someArray
How do I perform a null-check on a dynamic object? Pseudo code: public void
In Pseudo code If Domain inList
Some pseudo-code for accomplishing a linear fisheye distortion of a MovieClip's contents would be
I am using pseudo-code here, but this is in JavaScript. With the most efficient
Can anyone provide some pseudo code for a roulette selection function? How would I
Here is pseudo-code of how I setup an array representing the MandelBrot set, yet
Consider this pseudo code: // an image is initialized UIImage *imagePX = [[UIImage alloc]initWithContentsOfFile:...
In general terms and pseudo-code, what would be the best way to have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.