PDA

View Full Version : wait until a variable is set



kevindebb
4th May 2012, 19:38
Hello everybody,

I'm trying to make a multiplayer tetris. I wan't to synchronize the two games on each computer with each other. Now I'm stuck with the following problem:

On a certain moment in the game the client has to wait until a variable is set. The variable is send by the server. Now i'm wondering how the program can "wait" until the variable is set. I tried it like I would do it in C but my program crashed that way. I fully understand signals and slots but I think i can't use it in this case.



else
{
while(!ack_piece) // <---- I need a way to fix this
{
}
this->set_ack_piece(false);
qDebug()<<"beginstadium van nextpiece = "<<nextpiece_temp;
nextPiece.setRandomShape(nextpiece_temp);
}


Hope somebody can help me.

Thanks

Le_B
5th May 2012, 02:52
from what i get you have two threads that want to read and write to the same variable
a mutex (QMutex class) on every access and change to the variable might be the answer

kevindebb
5th May 2012, 10:58
No, that's not the problem. I just want the program to "wait" until the variable is set. I dit it like you would do it in C. But it won't work in Qt.

amleto
5th May 2012, 11:49
the problem is not that you did it 'like C'. Qt is built from C++. anything you do in C is compatible with Qt.

You don't have to do anything to 'wait' for a variable. When the client receives the variable, it needs to MAKE something happen. There shouldn't be something paused in the middle of an operation.

This kind if thing is a workflow/statemachine issue. You should separate the steps in your workflow/statemachine more clearly

ChrisW67
7th May 2012, 06:03
No, that's not the problem. I just want the program to "wait" until the variable is set. I dit it like you would do it in C. But it won't work in Qt.

The problem is your understanding. The remote server cannot magically change a variable in your program... your code, regardless of language, has to do that in response to receiving something from the server. Your while loop contains nothing that will change the variable, so it will spin at 100% CPU waiting for the variable to be changed by some other part of your code that is able to run 'simultaneously': this must be in another thread you tell us you do not have.

You have three ways to make this work:

If you use Qt networking asynchronously and in a single thread (as intended) then the program must be waiting at the Qt event loop in order to process the response from the server and change your variable. Your busy wait loop is not allowing that to happen so your variable cannot change that way.
If you have the networking in a second thread accessing a shared variable then you need to ensure accesses are consistent. The naïve approach might work some of the time but you generally must ensure that the variable is never accessed from both threads at the same time with a mutex.
If you have the networking in a second thread but are using signals and slots to ensure that the variable is only written from the main thread then you must ensure the program reaches the Qt event loop and processes queued signals.

All require you to think about how you have designed the program. The first option is the cleanest IMO.