Results 1 to 5 of 5

Thread: wait until a variable is set

  1. #1

    Default wait until a variable is set

    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.

    Qt Code:
    1. else
    2. {
    3. while(!ack_piece) // <---- I need a way to fix this
    4. {
    5. }
    6. this->set_ack_piece(false);
    7. qDebug()<<"beginstadium van nextpiece = "<<nextpiece_temp;
    8. nextPiece.setRandomShape(nextpiece_temp);
    9. }
    To copy to clipboard, switch view to plain text mode 

    Hope somebody can help me.

    Thanks

  2. #2
    Join Date
    Nov 2010
    Posts
    82
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: wait until a variable is set

    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

  3. #3

    Default Re: wait until a variable is set

    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.

  4. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: wait until a variable is set

    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
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: wait until a variable is set

    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.

Similar Threads

  1. wait until a signal is processed
    By mentalmushroom in forum Qt Programming
    Replies: 9
    Last Post: 10th November 2011, 13:17
  2. how to set wait condition
    By hema in forum Newbie
    Replies: 5
    Last Post: 25th July 2011, 08:08
  3. How to Implement Sleep() or wait() in Qt app
    By sosanjay in forum Qt Programming
    Replies: 5
    Last Post: 29th December 2009, 20:02
  4. Wait for a signal
    By adonel in forum Qt Programming
    Replies: 1
    Last Post: 13th May 2008, 19:53
  5. how to make program wait
    By psmech in forum Qt Programming
    Replies: 3
    Last Post: 26th May 2006, 05:24

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.