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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:54:42+00:00 2026-05-15T13:54:42+00:00

Is there a way to make worker threads Basically I tried creating threads every

  • 0

Is there a way to make “worker threads” Basically I tried creating threads every time I needed them and this resulted in being slower than with 1 thread becase creating a new thread all the time is expensive. Would there be a way to create worker threads when the application first starts, then give them work when necessary?

Thanks

  • 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-15T13:54:43+00:00Added an answer on May 15, 2026 at 1:54 pm

    Yes, you can create the threads up front and have them wait for signals to go and start their work.

    These signals can be message queues or semaphores or any other sort of inter-thread communication method.

    As an example, we once put together a system under UNIX which had a master thread to receive work over the net and farm the jobs out to slave threads. The slave threads would actually have to interact with another system by screen scraping (basically emulating a user) to get their desired information.

    In other words, work could come in faster than slave threads could do it.

    So we started up about fifty odd slaves and just created a condition variable for them (this was using pthreads). Each slave that wasn’t doing active work simply waited on the condition variable.

    When a job came in to the master thread, it loaded the relevant data into known memory and “kicked” the condition variable, waking up one of the slaves which would then grab the work and start processing it (after notifying the master that it could continue).

    So there was no overhead in having to create threads on the fly – all were created on application start-up and simply waited for work to be handed over.

    Of course, there’s a potential downside to this sort of static sizing in that you may get into trouble if you actually need more threads. We solved it by simply monitoring the maximum number of threads and ensuring the process was restarted the following day with more if we were consistently running out.


    If you want to have a more dynamic solution, you need to look into a concept called thread pooling. This is basically what I’ve described above but it generally lets you set a minimum and maximum number of threads along with a maximum time an inactive thread will be allowed to survive without work (unless you’re already at the minimum).

    Its implementation could be something as simple as:

    master:
        minslaves = 7
        maxslaves = 20
        maxtime = 600
        numslaves = 0
        freeslaves = 0
    
        while running:
            while numslaves < minslaves:
                increment numslaves and freeslaves
                start a slave
            endwhile
            wait for work
            if freeslaves = 0 and numslaves < maxslaves:
                start a slave
            endif
            queue work to slavequeue
        endwhile
        while numslaves > 0:
            wait
        endwhile
        exit
    

    and:

    slave:
        while running:
            get work from slavequeue with timeout
            if timed out:
                if time since last work > maxtime and numslaves > minslaves:
                    break out of while loop
                endif
                restart while loop
            endif
            decrement freeslaves
            do the work
            increment freeslaves
        endwhile
        decrement numslaves and freeslaves
        exit
    

    (with proper semaphore protection of all shared variables of course, numslaves, freeslaves, slavequeue and the others if you decide to change them after threads have been started).

    And, if your environment already has thread pooling, I suggest you use it rather than trying to convert my pseudo-code above. My code is from memory and is meant to illustrate a point – it may or may not be bug-free 🙂

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

Sidebar

Related Questions

Is there a way to make Timeline start recording EXACTLY at the time where
Is there a way to make the SVG version of an image not being
Is there a way to make a button's background color change from a color
Is there a way to make MySQL cause some kind of error when doing
Is there a way to make each form, (all controls, all strings, all integers
Is there a way to make my mysql and php server automatically display dates
Is there a way to make a macro to save a file with the
Is there a way to make my js files not viewable or debuggable through
Is there a way to make z3 solver emit symbolic solutions? For example, for
Is there any way to make the text align left and justify as same

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.