Circular queue using arrays and thread safe

Hi All:

I have to implement a circular queue of integers of user-specified size using a simple array. Provide routines to initialize(), enqueue() and dequeue() the queue. Please suggest ideas to make it thread safe.

Regards

sunnybrad
sunnybradAsked:
Who is Participating?
 
itsmeandnobodyelseCommented:
>>>> a circular queue of integers of user-specified size using a simple array

means:

- you need a class, e. g. class CircularQueue
- which has a int* member for the dynamic array
- a int member for the size of the array
- a int member for the current circular start
- a int member for the current circular end
- a constructor which takes a user-defined size and creates the array
- a destructor which frees (deletes) the array

if start == end the queue is empty. So initially start = end = 0;

>>>> Provide routines to initialize(), enqueue() and dequeue() the queue.
These should be members of the class. initialize is a protected member cause it was called internally only (by constructor and resize member function). enqueue and dequeue are public members.

>>>> Please suggest ideas to make it thread safe.
On Windows platform you use a CRITICAL_SECTION as a (private) member. On other platforms use a pthread_mutex. In any case the 'mutex' needs a initialization in the constructor (e. g. with InitializeCriticalSection). Whenever you are going to make an update on the private data members you need to 'enter' the critical section to do it exclusively (lock the the pthread_mutex). If the update was done, you 'leave' the critical section (unlock the pthread_mutex).

Regards, Alex
0
 
sleep_pilotCommented:
Make use of mutex to protect sections (known as critical sections) of your code that access/modify the parts of your class's data structure.  This will get you thread-safety.

As jkr mentioned, we can't do your homework for you, but we can help with specific questions.
0
 
sunnybradAuthor Commented:
Its not a homework new to things, please specify pseudo code or ideas.

-sunnybrad
0
 
jhshuklaCommented:
>> - a int member for the current circular start
>> - a int member for the current circular end

isn't that redundant since the queue is circular?
0
 
jhshuklaCommented:
never mind. i was thinking of circular linked list while yours was array based implementation.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.