Results 1 to 6 of 6

Thread: QThread call-once semantics?

Hybrid View

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

    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.

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

    brcain (20th October 2006)

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

    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?

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

    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.

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

    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.

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

    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
  •  
Qt is a trademark of The Qt Company.