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

  • Home
  • SEARCH
  • 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 6701221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:54:29+00:00 2026-05-26T06:54:29+00:00

I am working on creating a threadpool from scratch as part of an assignment

  • 0

I am working on creating a threadpool from scratch as part of an assignment and am able to create the thread pool and then pass each created thread a function that constantly loops. My question is how can I accept the input and pass it to an already executing pthread. After figuring this out I will add mutexes to lock the function to a specific thread, but I am unable to get to that part.

class ThreadPool{
public:
    ThreadPool(size_t threadCount);
    int dispatch_thread(void *(dispatch_function(void *)), void *arg);
    bool thread_avail();
    int numThreads;
    pthread_t * thread;
    pthread_mutex_t * mutexes;
};


int ThreadPool::dispatch_thread(void *(dispatch_function(void *)), void *arg){

  flag = 1;
//This is where I would like to pass the function the running pthread
}

void *BusyWork(void *t)
{
  while(true){
  //This is where I would like to run the passed function from each thread
  //I can run the passed function by itself, but need to pass it to the threadpool
  }
}

ThreadPool::ThreadPool(size_t threadCount){
 pthread_t thread[threadCount];

 for(t=0; t<threadCount; t++) {
 //printf("Main: creating thread %ld\n", t);
 rc = pthread_create(&thread[t], NULL, BusyWork, (void *)t); 
 }
}

void *test_fn(void *par)
{
   cout << "in test_fn " << *(int *)par << endl;
}

int main (){
  ThreadPool th(3);
  int max = 100;

  for (int i = 0; i < 20; i++) {
    max = 100 * i;
    th.dispatch_thread(test_fn, (void *)&max);
    sleep(1);
  }
}
  • 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-26T06:54:29+00:00Added an answer on May 26, 2026 at 6:54 am

    The best pattern that I can think of is to use some sort of queue to pass messages to the thread-pool. These messages may contain functions to be run as well as some control messages for shutting down the thread-pool. As you already have guessed, the queue will have to be thread safe.

    A simple approach for the queue is to use a fixed size array which you turn into a circular buffer. The array will have a Mutex to lock it when accessing the array and a Condition Variable to awaken the thread-pool thread.

    When putting an item on the queue, we lock the mutex, add to the queue and then signal the thread-pool with the Condition Variable.

    Each running thread in the in the thread pool will start life by locking the mutex and waiting on the condition varaible (which automaticall unlocks the Mutex). When awoken it will remove the item from the queue, and then unlock the mutex. It is now free do its stuff. When finished it goes to sleep until re-signaled.

    As general advice, avoid sharing memory between threads because this either leads to race conditions (if access is not protected) or leads to interlocking (if access is locked). Also avoid locking a mutex when performing any long running operation such as calling new (malloc), delete (free) or any system calls.

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

Sidebar

Related Questions

I'm working on creating a domain layer in Zend Framework that is separate from
I am working on creating a thread safe control for my windows forms application.
I am working on creating some grails domain objects dynamically and then adding them
I'm working on creating a Hibernate query engine that can create dynamic queries. Thus
I am working on creating an XSL to format some incoming XML from an
I've been working on creating a Windows Desktop Gadget. I can create HTML elements
i'm working on creating an app that streams mp3s from a server. i'm using
I'm currently working on creating a new C# project that needs to interact with
I am working on creating a daemon in Ruby using the daemons gem. I
I am working on creating a tag cloud in Silverlight 2 and trying to

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.