My task is to simulate a bottler process.
There is a person who is in charged of putting "bottles" in a queue. For example his speed is 1 bottle per second. I did this with a thread. But the problem is that there must be a second thread, this will be a machine who will be in charged of taking these "bottles" and dequeue them and then put them into another queue.
I did this by creating the queues and threads in the 'main' function. Then I started the thread, and as parameter I entered the queue which I just created. In this way the thread (in this case, the person) will put the "bottles" into the queue passed as parameter.
Then when the program is run, is does work, but not as it should be. The first thread (person) start putting elements into the queue, and when it finishes, then the second thread (machine) starts removing the elements of the queue.
What I want my program to do is to do these two tasks at the same time, this means that as soon as the person (the first thread) starts putting elements into the queue, the machine (second thread) start removing them from the queue.
Here is some of my code:
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Queue *queue1 = new Cola (""); // First queue
Thread *Person = new Thread (); // Person who is in charged of putting bottles
Thread *Machine = new Thread (); // Machine in charged of removing elements of the queue
Person->queue(queue1);
Machine->dequeue(queue1);
system("Pause");
return 0;
return a.exec();
}
and here is some of the code of Thread
void Thread::queue(queue *c)
{
for (int i = 0; i < 10; i++)
{
c -> push (i);
cout << "Inserting to the queue the: " << i << endl;
this -> sleep (1);
}
}
void Thread::dequeue(queue *c)
{
while (!c -> empty())
{
c -> pop ();
this -> sleep (2);
}
}
Any ideas of how can these two threads work at the same time? Thanks for your help and ideas, I really appreciate them.