views:

71

answers:

2

I am writing a little trial project and I need to pass and object of type QueuList by value to a thread pool thread. It is a Boost threadpool and I am using Bind to pass the args to the thread.

For some reason I cannot seem to pass my item to the threadpool thread by value...

Can anybody help what I'm doing wrong?

void ConsumerScheduler()
{
    int count = 0;
    typedef boost::intrusive::list<QueuList> List;
    while (true)
    {
        WaitForSingleObject(hConsumer, 2000); // Wait for hConsomuer to become > 0
        {
            //lock Queue
            QueuList *item = NULL;
            boost::mutex::scoped_lock lock(mtx);
            {//Scope of lock
                if (lst->size() == 0)
                {
                    printf("List is emtpy"); 
                    continue;
                }
                else
                {
                    List::iterator iter(lst->begin());
                    item = &*iter;
                    lst->pop_front();  //Item is removed from list, so pointer is no longer available!!!
                    printf("Popped item off list.  List current size: %d\n",lst->size());
                    count++;
                }
            }
            //Pass to threadpool
            tpp.schedule(boost::bind(taskfunc,*item)); //it will not accept *item or item in this bind function to pass it by value
            total--;
            if (total == 0)
            {
                printf("Total is now zero!\nCount is %d\n", count);
            }
            if (count == 5)
                break;

            ReleaseSemaphore(hProducer,total , NULL);  // Release the hProducer semaphore, possibly waking producer
        }
    }
}

//Thread pool thread function
void taskfunc(QueuList list)
{
    boost::mutex::scoped_lock lock(mtx);
    {
        std::string name= list.GetName();
        printf("Name gotten: %s",name);
    }

}

The reason I want to pass by value is so each threadpool thread has it's OWN copy of the object as the pointer gets removed from the list by the first function, this will cause an error if I pass by reference.

+2  A: 

You can solve this by using boost::shared_ptr<QueueList> in the queue and the threadpool scheduling. That best expresses the hand off of shared data that you want, in the absence of unique_ptr in some STLs.

Steve Townsend
Actually it doesn't: `shared_ptr` is about shared ownership, the only standard smart pointer expressing single-instance hand off is `unique_ptr` and is not available in all STLs yet. `shared_ptr` is the least worst replacement in the mean time.
Matthieu M.
@Matthieu - I figured it was best to offer a solution that's widely supported to avoid confusion
Steve Townsend
@Steve: I agree that the solution is good, but I would modify the "expresses the single-instance hand off" comment. shared and single don't really mix, semantic-wise.
Matthieu M.
A: 

The error occurs at runtime or compilation time?

I create my own code and don't have compilation erros.

I do not use boost, but, if you got error on runtime, I think the error is in scoped lock. The scoped lock shouldn't be inside the brackets?

Bruno Caponi
Compile time error, and its not the scoped lock.
Tony
What error? You have implemented the copy constructor of QueuList?
Bruno Caponi