Results 1 to 5 of 5

Thread: State variable used by multiple threads

  1. #1
    Join Date
    Feb 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default State variable used by multiple threads

    Found the answer at: http://www.qtcentre.org/wiki/index.p...gleton_pattern

    Sorry for adding to the noise.











    I've been trying to come up with a clean and computationally efficient way to declare a set of state variables that can be accessed by multiple threads. I'm sure that there are good and bad ways to handle this, and I've found much debate on the web regarding different approaches. I've got a way now that seems to work, but I wanted to vet it against the experts herein to see if I'm possibly missing something.

    My approach (certainly not unique) is to use a class made up of static variables and static getter/setter functions that utilize a mutex. See my example code below:

    Qt Code:
    1. #ifndef SYSSTATE_H
    2. #define SYSSTATE_H
    3.  
    4. #include <QMutex>
    5.  
    6. class SysState
    7. {
    8. private:
    9.  
    10. // Mutex
    11. static QMutex mutex;
    12.  
    13. // State Variables
    14. static double stateVar1;
    15.  
    16. public:
    17.  
    18. static void Init()
    19. {
    20. // this function must be called ONCE (and only once) to initialize the mutex
    21. mutex = new QMutex();
    22. }
    23.  
    24. static void Deinit()
    25. {
    26. // this function must be called to prevent a memory leak
    27. delete mutex;
    28. }
    29.  
    30. /////////////////////////////////////////////////////////////////
    31. static double GetStateVar1()
    32. {
    33. mutex.lock();
    34. double val = stateVar1;
    35. mutex.unlock();
    36. return val;
    37. }
    38.  
    39. static void SetStateVar1(double val)
    40. {
    41. mutex.lock();
    42. stateVar1 = val;
    43. mutex.unlock();
    44. }
    45. };
    46.  
    47. double SysState::stateVar1;
    48.  
    49. #endif // SYSSTATE_H
    To copy to clipboard, switch view to plain text mode 


    Each thread should include this header file ("sysstate.h"). No explicit instance of SysState is created (it's static).
    The syntax for use inside each thread is:

    double val1 = SysState::GetStateVar1();

    SysState::SetStateVar1(123.45);

    One, and only one, thread should call the COTMstate::Init() function...probably in my GUI thread that sets up all the other threads. This function initializes the class's QMutex object. It should also call the COTMstate:einit() function to clean up the QMutex.


    This seems to work cleanly, but I wasn't sure if I am misusing the QMutex, or if there are situations that I need to be aware of, or if there are any suggested improvements.

    I'm new to the concept of sharing data in this way, but am in need of a computationally efficient way to share state between many threads.

    Thanks,

    Bryan
    Last edited by bryang; 27th April 2012 at 22:28. Reason: Found Answer

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: State variable used by multiple threads

    Are You sure that this code is compilable ?

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

    Default Re: State variable used by multiple threads

    it wont.

    Mutex mutex;
    mutex = new Mutex;

    will not compile.


    op should remove Init and Deinit methods
    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.

  4. #4
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    387
    Thanks
    101
    Thanked 15 Times in 15 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: State variable used by multiple threads

    What's the advantage of using the singleton pattern over the suggested static object?

  5. #5
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: State variable used by multiple threads

    Are You sure that this code is compilable ?
    It looks kinda like Java code.

Similar Threads

  1. QNetworkAccessManager in multiple threads
    By mentalmushroom in forum Qt Programming
    Replies: 2
    Last Post: 7th August 2013, 23:14
  2. multiple table in a single variable
    By sattu in forum Qt Programming
    Replies: 0
    Last Post: 1st March 2011, 11:01
  3. Replies: 2
    Last Post: 9th December 2010, 16:49
  4. Multiple definition of a variable?
    By T0bi4s in forum Newbie
    Replies: 5
    Last Post: 14th January 2010, 22:13
  5. Phonon on multiple threads
    By Savignon in forum Qt Programming
    Replies: 5
    Last Post: 6th March 2009, 22:17

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.