C ++ priority queue

I created the following priority queue in C ++

priority_queue < ThreadInfo*, vector<ThreadInfo*>, CompareThread > thread_queue; 

where is the class 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; }; 

and comparison class

 class CompareThread { public: bool operator()(ThreadInfo* th1, ThreadInfo* th2) { if (th1->get_priority()>th2->get_priority()) return true; return false; } }; 

then I insert the element into the next function,

 void ThreadScheduler::register_thread(ThreadInfo &th) { thread_queue.push(&th); } 

I call the thread register from the following function,

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

but every time I click on some threadinfo object in the queue, I get the most recent object when I call thread_queue.top () whether it should return the thread object with the lowest priority. Are there any problems here?

+4
source share
2 answers

You pass a pointer to the same piece of memory in the queue. You call register_thread with a link to a local object and set its address. That is why they are all the same. Another problem is that when you leave the thread_register function, the local ti will be deleted (out of scope) and you will not have valid entries in the queue.

What you need to do is to allocate a new memory for each information and copy the data into this memory. Thus, each pointer of the element that you insert into the queue should come from another new , if you have a copy constructor, this will do:

 void ThreadScheduler::register_thread(ThreadInfo &th) { thread_queue.push(new ThreadInfo(th)); /* ... */ } 

check this out: fooobar.com/questions/564877 / ...

+3
source

The problem is that you are using a pointer to a variable declared locally in the function. Once the function ( ThreadController::thread_register ) is ThreadController::thread_register , the local variable no longer exists, and the pointer now points to some unallocated memory.

There are two solutions:

  • Use smart pointers such as std::shared_ptr and create a new pointer in ThreadController::thread_register :

     std::shared_ptr<ThreadInfo> ti(new ThreadInfo); 

    Of course, you should remember that you change to std::shared_ptr even more, and use the access operator -> instead . .

  • Do not use pointers at all, and let the class data (which are pretty minimal and simple) be copied.

I suggest moving on to alternative 2.

+1
source

Source: https://habr.com/ru/post/1400986/


All Articles