【发布时间】:2012-03-28 20:39:24
【问题描述】:
我在 C++ 中创建了以下优先级队列
priority_queue < ThreadInfo*, vector<ThreadInfo*>, CompareThread > thread_queue;
ThreadInfo 类在哪里
class ThreadInfo {
public:
ThreadInfo();
ThreadInfo(const ThreadInfo& orig);
ThreadInfo(int thread_id,int init_time,int sleep_time,int run_time,int priority,int is_critical)
{
this->thread_id=thread_id;
this->is_critical=is_critical;
this->init_time=init_time;
this->priority=priority;
this->run_time=run_time;
this->sleep_time=sleep_time;
}
void set_critical(bool value)
{
is_critical=value;
}
bool get_critical()
{
return is_critical;
}
void set_sleep_time(long value)
{
sleep_time=value;
}
long get_sleep_time(long value)
{
return sleep_time;
}
void set_run_time(long value)
{
sleep_time=value;
}
long get_run_time(long value)
{
return sleep_time;
}
int get_lock_type()
{
return lock_type;
}
void set_lock_type(int lock_type)
{
this->lock_type=lock_type;
}
int get_priority()
{
return priority;
}
void set_priority(int value)
{
this->priority=value;
}
unsigned long int get_thread_id()
{
return thread_id;
}
void set_thread_id(unsigned long int value)
{
this->thread_id=value;
}
virtual ~ThreadInfo();
private:
unsigned long int thread_id;
long init_time;
long sleep_time;
long run_time;
int priority;
bool is_critical;
//1=spin,2=busy,3=semaphore
int lock_type;
};
比较类是
class CompareThread {
public:
bool operator()(ThreadInfo* th1, ThreadInfo* th2)
{
if (th1->get_priority()>th2->get_priority()) return true;
return false;
}
};
然后我在下面的函数中插入元素,
void ThreadScheduler::register_thread(ThreadInfo &th)
{
thread_queue.push(&th);
}
我从下面的函数调用线程寄存器,
int ThreadController::thread_register(pthread_t &t, int priority, bool critical)
{
ThreadInfo ti;
cout<<"t reg:"<<t<<endl;
ti.set_thread_id(t);
ti.set_critical(critical);
ti.set_priority(priority);
ThreadScheduler::Instance()->register_thread(ti);
}
但是每次当我将一些线程信息对象推送到队列时,当我调用 thread_queue.top() 时,我都会得到最新的对象,它是否应该返回具有最低优先级的线程对象。这里有什么问题吗?
【问题讨论】:
-
您能显示将条目放入队列的代码吗?
-
您好,我更新了代码。在这里你可以看到我是如何推送元素的。@JoachimPileborg
-
您发布了 很多 代码,大部分 不相关..
-
你能显示你调用 register_thread 的代码吗?
-
ok.. 现在告诉我们你在哪里创建这些 ThreadInfo-s
标签: c++ data-structures queue priority-queue