Results 1 to 6 of 6

Thread: QThread call-once semantics?

  1. #1
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Thanks
    16
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default QThread call-once semantics?

    I'm relatively new to QThread and am changing some code that previously used Boost threads.

    Does QThread support the call-once semantics ... similar to Boost code below?

    Qt Code:
    1. /// Description: Thread-safe singleton class.
    2.  
    3. // Code in header file ...
    4. class cSingleton
    5. {
    6. public:
    7. static cSingleton * getInstance();
    8. private:
    9. cSingleton() {}
    10. static void createInstance(); // This method invoked by boost::call_once
    11. static boost::once_flag mgOnceFlag; // Special flag used to check if associated
    12. // boost::call_once routine has been called yet
    13. static cSingleton * mgInstance; // Singleton instance
    14. };
    15.  
    16.  
    17. // Code in source file ...
    18. cSingleton * cSingleton::mgInstance(0);
    19.  
    20. // Statically intialized at compile time ... thus not subject to multithreaded
    21. // initialization problems.
    22. boost::once_flag cSingleton::mgOnceFlag(BOOST_ONCE_INIT);
    23.  
    24. cSingleton * cSingleton::getInstance()
    25. {
    26. // boost::call_once is necesary for thread-safetly because multiple threads
    27. // can call this method at the same time ... creating two instances of the
    28. // singleton. Using boost::call_once(), only one thread is actually able to
    29. // call createInstance().
    30. boost::call_once(&cSingleton::createInstance, mgOnceFlag);
    31. return mgInstance;
    32. }
    33.  
    34. void cSingleton::createInstance()
    35. {
    36. if(!mgInstance)
    37. mgInstance = new cSingleton;
    38. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Aug 2006
    Location
    Switzerland
    Posts
    52
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QThread call-once semantics?

    Well, it's about access serialization, so I would use QMutex:
    Qt Code:
    1. // Singleton.h
    2. #include <QMutex>
    3.  
    4. class Singleton
    5. {
    6. public:
    7. static Singleton *getInstance();
    8.  
    9. private:
    10. Singleton();
    11.  
    12. static Singleton *instance;
    13. static QMutex instanceMutex;
    14. };
    15.  
    16. // Singleton.cpp
    17. #include <QtDebug>
    18. #include <QMutexLocker>
    19.  
    20. Singleton *Singleton::instance(0);
    21. QMutex Singleton::instanceMutex;
    22.  
    23. Singleton *Singleton::getInstance()
    24. {
    25. if (!instance) {
    26. QMutexLocker instanceMutexLocker(&instanceMutex);
    27. if (!instance) {
    28. instance = new Singleton();
    29. }
    30. }
    31.  
    32. return instance;
    33. }
    34.  
    35. Singleton::Singleton()
    36. {
    37. qDebug() << "Singleton ctor()";
    38. }
    To copy to clipboard, switch view to plain text mode 
    The Wheel weaves as the Wheel wills.

  3. The following user says thank you to danadam for this useful post:

    brcain (20th October 2006)

  4. #3
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Thanks
    16
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QThread call-once semantics?

    Quote Originally Posted by danadam
    Well, it's about access serialization, so I would use QMutex
    Yep. I knew I could do that for this case. Maybe call-once semantics is unnecessary with the mutex. I wonder why Boost thought it necessary ... other cases that it might address.

    Do you know if Qt has something similar ... just as a mental note?

  5. #4
    Join Date
    Aug 2006
    Location
    Switzerland
    Posts
    52
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QThread call-once semantics?

    Quote Originally Posted by brcain View Post
    I wonder why Boost thought it necessary ... other cases that it might address.
    Unfortunately I'm not very much familiar with boost, and I cannot think up any reason.

    Quote Originally Posted by brcain View Post
    Do you know if Qt has something similar
    I've never come across anything similar, but I've been dealing with Qt for only a few months, so it doesn't mean that no such thing exists.
    The Wheel weaves as the Wheel wills.

  6. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QThread call-once semantics?

    Quote Originally Posted by brcain View Post
    Do you know if Qt has something similar ... just as a mental note?
    You can construct it yourself using QMutex or QSemaphore.

  7. #6
    Join Date
    Feb 2006
    Location
    US
    Posts
    173
    Thanks
    16
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QThread call-once semantics?

    Quote Originally Posted by jacek
    You can construct it yourself using QMutex or QSemaphore.
    Yep ... did that

    Thanks

Similar Threads

  1. QTimer and QThread
    By TheKedge in forum Qt Programming
    Replies: 4
    Last Post: 21st September 2006, 14:52
  2. how to use QHttp inside QThread in Qt3
    By alusuel in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2006, 11:19
  3. why cant i call setGeometry
    By freegnu in forum Qt Programming
    Replies: 1
    Last Post: 14th June 2006, 04:59
  4. Is it possible to create a QThread without inheriting ?
    By probine in forum Qt Programming
    Replies: 6
    Last Post: 23rd March 2006, 22:51
  5. QProcess in a QThread
    By chombium in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2006, 15:52

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.