loop each queue

Pages: 12
I have n number of queues (depends on the no. of threads ) and what i need is that, i want loop each of queue starting from from the 2nd queue to the nth (end) queue.

how can i loop this?

std::queue<int> queue;

for(queue=queue+1; queue<=n; queue++) // will this idea loop from the 2nd to the nth (end) queue

please check and let me know some ideas how to loop the queues
Last edited on
Is the question clear? or more information needed
Are your queues organize as an array?

Like so:
1
2
3
4
5
6
std::queue<int> queue[n];

for(i=1; i<n; i++)
{
  // Do something with queue[i]
}
http://vichargrave.com/multithreaded-work-queue-in-c/ i have the work queue which has tasks picked up by the threads

the idea i need to solve - loop each queue and get the size of each queue and minimum size among the given number of queues.

i tried this logic

std::queue<int> q;
/* fill queue ... */
int min_value = INT_MAX;
std::size_t size = q.size();
for( q=1; q<n; q++){ // given loop of queues
if {
(q.size()<min_value) // q.size() is compared with the min_value (limits MAX)
min_value=q.size(); // store minimum size among the queues.
}
}
i think i succeeded in getting the minimum size among the the queues.

But i dont know whether looping of queues is correct?

please can u check this and help me out
Last edited on
@coder777 did u check that code, do you have any idea how to loop with the worker queues as i mentioned above?
Seems that you don't understand what a queue is:

http://www.cplusplus.com/reference/queue/queue/

It's basically a container where you put in data to one side and get data out from the other side. The data in between is queued.

You cannot use it as an index for a loop.


the idea i need to solve - loop each queue and get the size of each queue and minimum size among the given number of queues.
why do you think you need that?
The link you provided shows how to feed a thread with data using a queue. Each thread itself manage its own queue.

store minimum size among the queues
Either you mean the items of the queue (a queue doesn't support access to it's item other than first/last)
or you mean multiple queues (organized via array, vector ... as I've shown above)
@coder777
i have multiple queues( equal to the number of threads). i have to access the each queue and find its size.

std::queue<int> q[num_threads];

int min_value = INT_MAX;
std::size_t size = q[i].size();
for( i=1; i<num_threads; i++){ //accessing loop of queues
if {
(min_value > q[i].size())
min_value = q[i].size() // q[i].size() is compared with the min_value (limits MAX)
}
}

i think this code will loop through the multiple queuesand store the minimum size among the different queues.

but i need to loop through the multiple queues till last queue (equal to number of threads), but here is it not possible because i<num_threads right? so how to loop till the end queue. will i=num_threads will work?
Last edited on
i think this code will loop through the multiple queues=num of threads and store the minimum size among the different queues.
yes

but i need to loop through the multiple queues till last queue (equal to number of threads), but here is it not possible i<num_threads, so how to loop till the end queue. will i=num_threads will work?
I'd think that you mean num_threads is not constant.

organize your queues in a vector or list:
std::vector<std::queue<int> > q_vector;

http://www.cplusplus.com/reference/vector/vector/
http://www.cplusplus.com/reference/list/list/

both will provide the number of actual queues and you don't need an artificial upper limit
@coder777
yes as u said using vectors will have no upper bounds.

so Updated:

1
2
3
4
5
6
7
8
9
10
std::vector<std::queue<int> > q_vector[num_threads];

int min_value = INT_MAX;
std::size_t size = q[i].size();
for( i=1; i<num_threads; i++){ //accessing loop of queues
if {
(min_value > q[i].size())
min_value = q[i].size() // q[i].size() is compared with the min_value (limits MAX)
}
}


but one doubt is as i mentioned i<num_threads will it loop till the last queue of the thread or last but before one?

and this notation q_vector[num_threads] should be given or just q_vector is enough
Last edited on
but one doubt is as i mentioned i<num_threads will it loop till the last queue of the thread or last but before one?
the last queue.

You will always see i<... when iterating over arrays. arrays are 0 based (so is vector) hence you have the valid indexes 0, 1, 2 for an array with the size of 3 (like int a[3]) -> for(int i = 0; i < 3; i++)

and this notation q_vector[num_threads] should be given or just q_vector is enough
q_vector[num_threads] is an array of vectors. You don't want that.

Use size() to determine the number of queues:
1
2
std::size_t size = q[i].size();
for( i=1; i<num_threads size; i++){ //accessing loop of queues 
for( i=1; i<num_threads size; i++){ //accessing loop of queues

how a size() function can find the number of queues? size() is the function which give the no. of the elements in each queue right?
Last edited on
how a size() function can find the number of queues? size() is the function which give the no. of the elements in each queue right?
Ok, you're right it's like so:
1
2
3
4
5
std::size_t size = q_vector.size(); // Note that this is the size of the vector
for( i=1; i< size; i++){ //accessing loop of queues
if {
(min_value > q_vector[i].size()) // Note that this is the size of the queue
min_value = q_vector[i].size()


most STL [compliant] container have functions that are named equally
@coder777
1
2
3
4
5
6
7
8
9
10
std::vector<std::queue<int> > q_vector;

int min_value = INT_MAX;
std::size_t size = q_vector.size();
for( i=1; i<size; i++){ //accessing loop of queues
if {
(min_value > q_vector[i].size())
min_value = q_vector[i].size() 
}
}


so my complete code will looks like this?

thank you very much
Last edited on
so my complete code will looks like this?
Somehow, to make it compile:
1
2
3
4
5
6
7
8
std::vector<std::queue<int> > q_vector;

int min_value = INT_MAX;
std::size_t size = q_vector.size();
for( i=1; i<size; i++){ //accessing loop of queues
  if(min_value > q_vector[i].size())
    min_value = q_vector[i].size();
}


it'd look like this
@coder777
okay, thanks much
@coder777
now i wish to do a additional task in this, i want to push (enqueue) integers to the queue of mininimum queue size.

q.push(int) // but this queue must be the queue which has minimum size.

1
2
q.get (min_value)
q.push(int)
// will this one, does the required operation?
Last edited on
No it wont, because min_value is the size of the queue which is the smallest, not the index of one, you need to remember the index of the queue when you get the size, add one extra int and set his value on "i" inside if.
will this one, does the required operation?
No.

Just add another variable like say min_index, assign i to min_index where you assigned min_value. That's it.

You can access it like so: q_vector[min_index].push(x);
@coder777
Updated
1
2
3
4
5
6
7
8
std::vector<std::queue<int> > q
int min_index = 0;
std::size_t size = q.size();
for( i=1; i<size; i++) //accessing loop of queues
    if(q[min_index].size() > q[i].size())
        min_index = i;
// Now q[min_index] is the shortest queue
 q[min_index].push(int)

I think the above code would work.

but i need to calculate from 2nd queue q[1] not from the 1st queue,q[0] so do i need to change int min_index = 0 to int min_index = 1 ?

and the header would be a vector #include <vector> or a queue#include <queue>
Last edited on
I think the above code would work.
It looks very much like it does

do i need to change int min_index = 0 to int min_index = 1 ?
Yes, and since it does not make sense to compare a queue with itself you may start the loop with 2

Edit:
and the header would be a vector #include <vector> or a queue#include <queue>
include both, since you use both
Last edited on
Pages: 12