158k views
2 votes
Need help with implementation of the struct and the three functions.

Part I: Thread-Safe, Unbounded Priority Queue Type
The first part of the assignment is to implement a thread-safe, unbounded priority queue. "Unbounded" means that instances of the queue does not have a fixed capacity: any number of items can be inserted without removing an item. This means you will have to use dynamic memory allocation to grow and shrink the queue as items are inserted and removed.
"Items" can be anything - they are represented by a pointer. It is up to the client (i.e., the code using the implementation) to ensure that the void * that is returned is cast to the actual type. Note that, unless everything placed into the queue is of the same type, there is no way for the client code to know what to cast the void *pointer returned by pq_next() to. So the best thing is for each instance of pq_t to be used to store only one type of item.
You will define a type:
typedef struct { /* your code here */ } pq_t;
You will also define three functions:
pq_t *pq_create(void) { /* your code here */ } — Creates and initializes a priority queue instance and returns a pointer to it. Returns NULL on error (e.g., OS out of memory - should never happen). This includes creating and initializing the
void pq_insert(pq_t *q, void *item, short prio) { /* your code here */ } — insert the given item into the given queue at the given priority. This operation never blocks. If item A has priority x, and item B has priority y, and x > y, then item A will be returned before item B. Negative priorities are allowed.
void *pq_next(pq_t *q) { /* your code here */ } — Returns the item in the given queue that was inserted with the highest priority. If there is more than one item with the same priority, returns the oldest one. If the queue is empty, this operation blocks until an item is inserted.
You may implement the queue abstraction any way you want, but you must use pthread_mutex_t and pthread_cond_t to synchronize access to your data structure, since it will be used by multiple threads at the same time. (In particular, your definition of pq_t must include at least one variable of type pthread_mutex_t and one of type pthread_cond_t.) You will also need to use malloc() and free() to dynamically adjust the size of the data structure. (Note: using pthreads' mutex and condition variable is probably the simplest way to implement this.)

User Tomassilny
by
7.6k points

1 Answer

4 votes

To implement struct and 3 functions, use pthread_mutex_t and pthread_cond_t to synchronize data structure and use malloc() and free().

To implement the struct and the three functions, you will need to define a struct that includes at least one variable of type pthread_mutex_t and one of type pthread_cond_t to synchronize access to the data structure.

Then, you will need to write the three functions:

pq_insert, pq_remove, and pq_peek.

To implement pq_insert, you will need to insert the given item into the given queue at the given priority, without blocking.

You can use pthread_mutex_t and pthread_cond_t to ensure that the queue is accessed safely by multiple threads at the same time.

Negative priorities are allowed.

To dynamically adjust the size of the data structure, you will need to use malloc() and free().

For more such questions on Struct:

User Thesecretmaster
by
7.5k points