Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7621187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:08:03+00:00 2026-05-31T04:08:03+00:00

I created the following priority queue in C++ priority_queue < ThreadInfo*, vector<ThreadInfo*>, CompareThread >

  • 0

I created the following priority queue in C++

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

where ThreadInfo class is

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 the compare class is

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

       return false;
    }
};

then I insert element in the following 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 each time when I push some threadinfo object to the queue I get the most recent object when I call the thread_queue.top(), whether it should return the thread object with lowest priority. Are there any problems here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:08:05+00:00Added an answer on May 31, 2026 at 4:08 am

    You are passing a pointer to the same memory chunk into the queue. You are calling the register_thread with a reference to a local object and queue its address. That is the reason they are all same. Another problem is, when you leave the thread_register function the local ti will be deleted (out of scope) and you will have no valid entries in the queue.

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

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

    check this: https://stackoverflow.com/a/986093/390913

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Entity model: public class Todo { [Required] public int ID
I have the following entities public abstract class Card { public virtual int Id
I created the following method for retrieving stored settings from the database: public String
I created the following interface: <?php interface Action { public function execute(\requests\Request $request, array
I have created following rule in the .htaccess file located at the root of
I have created following thing in android using android compatibility support package Basically i
I created the following function to simplify a piece of particularly complex code. CREATE
I created the following script to query a table and return the first 30
i created the following sample method in business logic layer. my database doesn't allow
I created the following simple PL/SQL stored procedure example to ask a specific question.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.