1

私は小さな試用プロジェクトを書いています.QueueList型のオブジェクトを値でスレッドプールスレッドに渡す必要があります。これは Boost スレッドプールであり、Bind を使用して引数をスレッドに渡しています。

何らかの理由で、アイテムをスレッドプールスレッドに値で渡すことができないようです...

誰かが私が間違っていることを助けることができますか?

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);
    }

}

値渡ししたい理由は、ポインタが最初の関数によってリストから削除されるため、各スレッドプールスレッドがオブジェクトの独自のコピーを持つためです。これにより、参照渡しするとエラーが発生します。

4

2 に答える 2

2

boost::shared_ptr<QueueList>これは、キューとスレッドプールのスケジューリングを使用して解決できます。これは、一部のSTLがない場合に、必要な共有データの受け渡しを最もよく表しますunique_ptr

于 2010-10-05T14:01:31.600 に答える
1

エラーは実行時またはコンパイル時に発生しますか?

私は自分のコードを作成し、コンパイル エラーはありません。

私はブーストを使用していませんが、実行時にエラーが発生した場合、エラーはスコープ ロックにあると思います。スコープ付きロックはブラケット内にあるべきではありませんか?

編集:コメントする権限がないため、回答として投稿しました

于 2010-10-05T14:22:44.817 に答える