stbamb
stbamb

Reputation: 197

How can I run two Threads at the same time? Qt C++

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.

Upvotes: 0

Views: 2325

Answers (2)

Luca Carlon
Luca Carlon

Reputation: 9986

What you're trying to implement is the producer-consumer. My advice is to completely forget what you've done and start reading the documentation: thread support in Qt, which you proved to have completely skipped.

Also consider reading this, which is an example of producer-consumer in Qt, which almost identical to what you need.

Upvotes: 2

Christian Stieber
Christian Stieber

Reputation: 12496

You are not actually using the threads at all. I've never used Qt so I can't (or won't) try to fix the whole program, but accordings to the docs you have to start a thread to make it run. It will then execute it's run method, which you have to implement -- your loop should probably go into this "run" method.

What you ARE doing is to create dormant thread objects, and then you just call your thread functions from your main thread. The thread never runs, and the methods are run in sequence on your main thread.

For correct operation you will probably have to protect access to your queue, so only one thread can use it at a time (unless the queue is threadsafe, but most libraries aren't trying to do that). For some additional research, message passing queues like the one you are making are usually protected by a condition lock, as that allows an operation such as "wait until something is in the queue".

Upvotes: 2

Related Questions