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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:05:52+00:00 2026-05-28T22:05:52+00:00

I am currently trying to create a very simple thread pool using std::thread .

  • 0

I am currently trying to create a very simple thread pool using std::thread.
In order to maintain threads ‘alive’ after their given task is done, I associate a std::mutex with each one of them.

The principle is somewhat like this:

// Thread loop
while (1)
{
  m_oMutex->lock();
  m_oMutex->unlock();
  m_bAvailable = false;
  m_oTask();
  m_bAvailable = true;
}

// ThreadPool function which gives a task to a thread
void  runTask(boost::function<void ()> oTask)
{
  [...]
  m_oThreads[i]->setTask(oTask);
  m_oMutexes[i]->unlock(); // same mutex as thread's m_oMutex
  m_oMutexes[i]->lock();
}

To find the i, the ThreadPool searches for a thread object with m_bAvailable set to true. It unlocks the corresponding mutex so the thread can lock it and execute its task. The thread unlocks it immediately so the ThreadPool can lock it again so the thread is halted once its task is done.

But the question is, will locks be made in the order the threads ask them? In other words, if a thread does a lock on a mutex, then the ThreadPool unlocks it and locks it again, am I sure that the lock will be given to the thread first? If not, is there a way to ensure it?

  • 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-28T22:05:53+00:00Added an answer on May 28, 2026 at 10:05 pm

    No, you cannot guarantee that your thread loop will ever acquire the lock with your example as is. Use a conditional variable to signal to the thread loop that it should awake and take the lock. See std::condition_variable::wait(...).
    condition-variable

    More on this topic in general can be found here http://en.wikipedia.org/wiki/Condition_variable. If you were using the pthread library, the equivalent call would be pthread_cond_wait in your “Thread loop” and pthread_cond_signal in your runTask function.

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

Sidebar

Related Questions

I am currently using XCODE 4.0.2 and am trying to create a very simple
I'm trying to create a very simple best-fit scheduling system. Everyone submits their free/busy
I am currently trying to create a win32 service using pywin32. My main point
I'm currently trying to create a desktop like homepage where using can move around
I'm currently trying to create a subclass of UIImageView in order to make it
I am currently trying to write a very simple app that sends an object
I am currently trying to make a very simple C program for school that
I would like to create a very simple image gallery. I am trying to
I'm currently trying to create a kernel module that will produce data based on
I am currently trying to create a menu system for a game and cannot

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.