Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Circular queue using arrays and thread safe

Posted on 2007-07-23
6
Medium Priority
?
7,855 Views
Last Modified: 2011-04-14
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
0
Comment
Question by:sunnybrad
5 Comments
 
LVL 1

Expert Comment

by:sleep_pilot
ID: 19551538
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
 

Author Comment

by:sunnybrad
ID: 19552256
Its not a homework new to things, please specify pseudo code or ideas.

-sunnybrad
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 2000 total points
ID: 19554011
>>>> 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
 
LVL 9

Expert Comment

by:jhshukla
ID: 19571460
>> - 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
 
LVL 9

Expert Comment

by:jhshukla
ID: 19571466
never mind. i was thinking of circular linked list while yours was array based implementation.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question