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

The Archive Base Latest Questions

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

i run the following example from book c++ concurrency in action, and pass function

  • 0

i run the following example from book c++ concurrency in action, and pass function a, b and c with its submit, it print pop and sure return true, but why not print “start task”
it seems not run the task()

#include <thread>
#include <atomic>
#include <vector>
#include <queue>

class join_threads
{
    std::vector<std::thread>& threads;
public:
    explicit join_threads(std::vector<std::thread>& threads_):threads(threads_)
    {}
    ~join_threads()
    {
        for(unsigned long i=0;i<threads.size();++i)
        {
            if(threads[i].joinable())
                threads[i].join();
        }
    }
};

template<typename T>
class thread_safe_queue
{
private:
    mutable std::mutex mut;
    std::queue<T> data_queue;
    std::condition_variable data_cond;
public:
    thread_safe_queue(){}
    thread_safe_queue(thread_safe_queue const& other)
    {
        std::lock_guard<std::mutex> lk(other.mut);
        data_queue=other.data_queue;
    }
    void push(T new_value)
    {
        std::lock_guard<std::mutex> lk(mut);
        data_queue.push(new_value);
        data_cond.notify_one();
    }
    void wait_and_pop(T& value)
    {
        std::unique_lock<std::mutex> lk(mut);
        //data_cond.wait(lk,[this]{return !data_queue.empty();});
        data_cond.wait(lk);
        value=data_queue.front();
        data_queue.pop();
    }
    std::shared_ptr<T> wait_and_pop()
    {
        std::unique_lock<std::mutex> lk(mut);
        //data_cond.wait(lk,[this]{return !data_queue.empty();});
        data_cond.wait(lk);
        std::shared_ptr<T> res(new T(data_queue.front()));
        data_queue.pop();
        return res;
    }
    bool try_pop(T& value)
    {
        std::lock_guard<std::mutex> lk(mut);
        if(data_queue.empty())
        return false;
        value=data_queue.front();
        data_queue.pop();
        printf("pop");
        return true;
    }
    std::shared_ptr<T> try_pop()
    {
        std::lock_guard<std::mutex> lk(mut);
        if(data_queue.empty())
        return std::shared_ptr<T>();
        std::shared_ptr<T> res(new T(data_queue.front()));
        data_queue.pop();
        return res;
    }
    bool empty() const
    {
        std::lock_guard<std::mutex> lk(mut);
        return data_queue.empty();
    }
};

class thread_pool
{
    std::atomic_bool done;
    thread_safe_queue<std::function<void()> > work_queue;
    std::vector<std::thread> threads;
    join_threads joiner;
    void worker_thread()
    {
        while(!done)
        {
            //printf("workerthread");
            std::function<void()> task;
            if(work_queue.try_pop(task))
            {
                printf("task start\n");
                task();
                printf("task end\n");
            }
            else
            {
                std::this_thread::yield();
            }
        }
    }
public:
    thread_pool() : joiner(threads),done(false)
    {
        unsigned const thread_count=std::thread::hardware_concurrency();
        try
        {
            for(unsigned i=0;i<6;++i)
            {
                printf("push %d",i);
                threads.push_back(std::thread(&thread_pool::worker_thread,this));
            }
        }
        catch(std::bad_alloc)
        {
            done=true;
            throw;
        }
    }
    ~thread_pool()
    {
        done=true;
    }
    template<typename FunctionType>
    void submit(FunctionType f)
    {
        work_queue.push(std::function<void()>(f));
    }
};
void a()
{
    //while(true)
    {
        printf("a\n");
    }
}
void b()
{
    //while(true)
    {
        printf("b\n");
    }
}
void c()
{
    //while(true)
    {
        printf("c\n");
    }
}
int main()
{
    printf("begin\n");
    thread_pool* pool = new thread_pool(); //start thread pool
    printf("submit start\n");
    pool->submit((*a)); // pass function to queue
    pool->submit((*b));
    pool->submit((*c));

    printf("submit finish\n");
    while(pool->done == false)
        std::this_thread::sleep(std::milliseconds(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-26T13:31:43+00:00Added an answer on May 26, 2026 at 1:31 pm

    After fixing all compilation errors, this compiles with GCC 4.6.1 on linux and runs, producing the following output:

    begin
    push 0push 1push 2push 3push 4push 5submit start
    submit finish
    poptask start
    a
    task end
    poptask start
    b
    task end
    poptask start
    c
    task end
    

    after which point it remains in endless loop because done never becomes true

    It appears that “task start” is, in fact, printed.

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

Sidebar

Related Questions

I'm following through the Learn Rails by Example book, and I'm trying to run
The following example is taken from: http://php.net/manual/en/function.curl-multi-close.php#example-3540 This example will create two cURL handles,
I'm following the ASP.Net MVC TaskList example video and on clicking Run in Visual
I get the following error when I try to run sample example of Google
Can not run following SQL from ant's sql task : BEGIN DBMS_AQADM.CREATE_QUEUE_TABLE( queue_table =>
I'm trying to extract snippets of dialogue from a book text. For example, if
I'm following the examples from the book 'Programming Clojure', and I'm at page 17
I was working the following example from Doug Hellmann tutorial on multiprocessing: import multiprocessing
I have copied the code from the following example on the Apple Developer site.
I am trying to get a simple example working from an assembly book I

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.