I'm trying to resolve this problem with QSemaphore. I made two global semaphores ( first, second), and my thread methods look like):
void MyThread::thread()
{
for (int i = 0 ; i < 100; i++)
{
// stop here and wait for first available
if (first.available())
{
first.acquire(1); // first is now 0, so it's not available (blocked)
second.release(1); // release second semaphore, so he can start
}
}
void MyThread::thread2()
{
for ( int i = 0; i < 100; i++)
{
if (second.available())
{
second.acquire(1); // after acquire this semaphore should be block untill first thread is done
first.release(1); // release first semaphore so void MyThread::thread() can lock his semaphore
}
}
}
void MyThread::thread()
{
for (int i = 0 ; i < 100; i++)
{
// stop here and wait for first available
if (first.available())
{
first.acquire(1); // first is now 0, so it's not available (blocked)
numberChange(QString("Thread 1"));
second.release(1); // release second semaphore, so he can start
}
}
void MyThread::thread2()
{
for ( int i = 0; i < 100; i++)
{
if (second.available())
{
second.acquire(1); // after acquire this semaphore should be block untill first thread is done
numberChange(QString("Thread 2"));
first.release(1); // release first semaphore so void MyThread::thread() can lock his semaphore
}
}
}
To copy to clipboard, switch view to plain text mode
Is my thinking correct ? What I'm missing ?
My main problem is' how to make them stop and wait. Right now it's looks like they don't communicate correctly.
Thanks you
Bookmarks