سؤال

أنا أكتب مشروعًا تجريبيًا صغيرًا وأحتاج إلى تمرير وكائن من النوع queulist حسب القيمة إلى مؤشر ترابط تجمع مؤشرات الترابط. إنه ThroundPool Boost وأنا أستخدم الربط لتمرير 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> في قائمة الانتظار وجدولة ThreadPool. من الأفضل أن يعبر عن يد البيانات المشتركة التي تريدها ، في غياب unique_ptr في بعض STLs.

نصائح أخرى

يحدث الخطأ في وقت التشغيل أو وقت التجميع؟

أقوم بإنشاء الكود الخاص بي وليس لديّ erros التجميع.

لا أستخدم Boost ، لكن إذا تعرضت للخطأ في وقت التشغيل ، أعتقد أن الخطأ في القفل المنحطي. لا ينبغي أن يكون القفل الناطق داخل الأقواس؟

تحرير: ليس لدي امتيازات للتعليق ، لذلك قمت بنشر إجابة

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top