質問

私は小さなトライアルプロジェクトを書いていますが、スレッドプールスレッドに値によってタイプのQueulistの対象を渡す必要があります。それはブーストスレッドプールであり、私はbindを使用してargsをスレッドに渡しています。

何らかの理由で、私は自分のアイテムを価値だけでthreadpoolスレッドに渡すことができないようです...

誰かが私が間違っていることを手伝うことができますか?

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

}

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

役に立ちましたか?

解決

これを使用して解決できます boost::shared_ptr<QueueList> キューとスレッドプールのスケジューリング。それは、あなたが望む共有データの手を外し、 unique_ptr 一部のSTLで。

他のヒント

エラーは、実行時またはコンピレーション時間に発生しますか?

私は自分のコードを作成し、コンピレーションエロを持っていません。

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

編集:コメントする特権がないので、答えとして投稿しました

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top