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 6752719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:04:40+00:00 2026-05-26T13:04:40+00:00

I have a program that needs to run a function M times per iteration,

  • 0

I have a program that needs to run a function M times per iteration, and those runs can be parallelized. Lets say I’m limited to running N threads at a time (say by the number of cores available). I need an algorithm that will make sure I’m always running N threads (so long as the number of threads remaining is >= N) and that algorithm needs to be invariant to the completion order of those threads. Also, the thread scheduling algorithm should not claim significant CPU time.

I have something like the following in mind, but its clearly flawed.

#include <iostream>
#include <pthread.h>
#include <cstdlib>

void *find_num(void* arg)
{
    double num = rand();
    for(double q=0; 1; q++)
        if(num == q)
        {
            std::cout << "\n--";
            return 0;
        }
}


int main ()
{
    srand(0);

    const int N = 2;
    pthread_t threads [N];
    for(int q=0; q<N; q++)
        pthread_create(&threads [q], NULL, find_num, NULL);

    int M = 30;
    int launched=N;
    int finnished=0;
    while(1)
    {
        for(int w=0; w<N; w++)
        {
            //inefficient if `threads [1]` done before `threads [2]`
            pthread_join( threads [w], NULL);
            finnished++;
            std::cout << "\n" << finnished;
            if(finnished == M)
                break;
            if(launched < M)
            {
                pthread_create(&threads [w], NULL, find_num, NULL);
                launched++;
            }
        }

        if(finnished == M)
            break;
    }
}

The obvious problem here is that if threads[1] finishes before threads[0] there is wasted CPU time, and I can’t think of how to get around that. Also, I’m assuming that having the main routine waiting on pthread_join() is not a significant drain on CPU time?

  • 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-26T13:04:41+00:00Added an answer on May 26, 2026 at 1:04 pm

    I would advice against respawining threads, it’s a rather serious overhead. Instead, create a pool of N threads and submit work to them via a work-queue, a rather standard approach. Even if your remaining work is less than N, the extra threads will not do any harm, they’ll just stay there blocked in the work-queue.

    If you insist on your current approach you can do like this:

    Do not wait for threads with pthread_join, you don’t need it, since you’re not communicating anything back to the main thread. Create the threads with the attribute PTHREAD_CREATE_DETACHED and just let them exit.

    In the main thread, wait on a semaphore, which is signaled by each exiting thread – in effect you would wait for any thread termination. If you don’t have <semaphore.h> for any reason, it’s trivial to implement it with mutexes and conditions.

    #include <semaphore.h>
    #include <iostream>
    #include <pthread.h>
    #include <cstdlib>
    
    sem_t exit_sem;
    
    void *find_num(void* arg)
    {
        double num = rand();
        for(double q=0; 1; q++)
            if(num == q)
            {
                std::cout << "\n--";
                return 0;
            }
    
        /* Tell the main thread we have exited.  */
        sem_post (&exit_sem);
        return NULL;
    }
    
    int main ()
    {
        srand(0);
    
        /* Initialize pocess private semaphore with 0 initial count.  */
        sem_init (&exit_sem, 0, 0);
        const int N = 2;
    
        pthread_attr_t attr;
        pthread_attr_init (&attr);
        pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
        for(int q=0; q<N; q++)
            pthread_create(NULL, &attr, find_num, NULL);
    
        int M = 30;
        int launched=N;
        int finnished=0;
        while(1)
        {
            for(int w=0; w<N; w++)
            {
                /* Wait for any thread to exit, don't care which.  */
                sem_wait (&exit_sem);
    
                finnished++;
                std::cout << "\n" << finnished;
                if(finnished == M)
                    break;
                if(launched < M)
                {
                    pthread_create(NULL, &attr, find_num, NULL);
                    launched++;
                }
            }
    
            if(finnished == M)
                break;
        }
    }
    

    Anyway, I would again recommend thread-pool/work-queue approach.

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

Sidebar

Related Questions

I have a program that needs to run as a separate NT user to
I have a program that needs to run as a normal user most of
I have a program that I need to run under *nix and windows. because
I have a program that needs several third-party libraries, and at the moment it
I have a program that needs a lot of memory and want to set
I have a Perl program, that needs to use packages (that I also write).
In an embedded program I have a screen object that needs to manage a
I have a Python script that needs to execute an external program, but for
Assume that i have written a program in C++ without using RTTI and run-time
I have a program which during it's run sometimes needs to call python in

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.