Results 1 to 8 of 8

Thread: static QMutex

  1. #1
    Join Date
    Jan 2006
    Location
    Maui, Hawaii
    Posts
    120
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    65
    Thanked 4 Times in 4 Posts

    Default static QMutex

    For some reason when I declare a public or private static QMutex in a class header file and try to use it in the source file, I get to the linking phase and then it barfs.

    sclient.o: In function `SClient::setHostName(QString const&)':
    /home/rhoover/sclient.cpp:176: undefined reference to `SClient::mutex'
    /home/rhoover/sclient.cpp:180: undefined reference to `SClient::mutex'
    collect2: ld returned 1 exit status
    make: *** [aodsc] Error 1
    When I declared it plain as day:

    Qt Code:
    1. class SClient : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. SClient(QObject *parent = 0);
    6. SClient( QObject *parent, const QString & _host, bool _parsing );
    7. ~SClient();
    8. static QMutex mutex;
    9. ...
    10. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Maui, Hawaii
    Posts
    120
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    65
    Thanked 4 Times in 4 Posts

    Default Re: static QMutex

    You know what's crazy is it builds fine if I leave off the "static". It just doesn't work.
    Last edited by mhoover; 20th August 2009 at 04:44. Reason: left off an important detail

  3. #3
    Join Date
    Jan 2006
    Location
    Maui, Hawaii
    Posts
    120
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    65
    Thanked 4 Times in 4 Posts

    Default Re: static QMutex

    So I moved the "static mutex" declaration to a global variable in my thread source file and it works.

    locked.
    Opening config file.
    about to unlock
    unlocked.
    setHostName() succeeded.
    locked.
    Opening config file.
    about to unlock
    unlocked.
    setHostName() succeeded.
    locked.
    Opening config file.
    about to unlock
    unlocked.
    setHostName() succeeded.
    locked.
    Opening config file.
    about to unlock
    unlocked.
    But I hear global variables don't mix with threading.

    Opinions? Suggestions? Threats?

  4. #4
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    8
    Thanked 133 Times in 128 Posts

    Default Re: static QMutex

    for static variable you have to define them in cpp file..

    /
    Qt Code:
    1. //.h file
    2. class someclass
    3. {
    4. public:
    5. someclass();
    6. static QMutex mutex;//declare it
    7. }
    8.  
    9. //.cpp file
    10. QMutex someclass::mutex; //define it.
    11.  
    12. someclass::someclass()
    13. {
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 

  5. The following 2 users say thank you to nish for this useful post:

    Corinzio (12th October 2009), mhoover (21st August 2009)

  6. #5
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 69 Times in 67 Posts

    Default Re: static QMutex

    Why do you want to declare it static? Static makes it available to the containing file only, effectively making it private for your class. What's wrong with just declaring it private?
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  7. The following user says thank you to franz for this useful post:

    mhoover (21st August 2009)

  8. #6
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    1
    Thanked 29 Times in 27 Posts

    Default Re: static QMutex

    Quote Originally Posted by franz View Post
    ... Static makes it available to the containing file only...
    It is true for C.

    I think he wants to make it shared among several instances of SClient.

    mhoover: Where do you define your mutex?
    I'm a rebel in the S.D.G.

  9. The following user says thank you to lyuts for this useful post:

    mhoover (21st August 2009)

  10. #7
    Join Date
    Jan 2006
    Location
    Maui, Hawaii
    Posts
    120
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    65
    Thanked 4 Times in 4 Posts

    Default Re: static QMutex

    Thanks again, Mr. Death. Using static variables is elementary, so i perhaps I am a newb after

    Lyuts, I defined it as a global variable in my .cpp and that works. I will change it to use Mr. Death's approach as that feels safer.

    Franz, the QMutex can't lock between threads unless it has scope access to the different threads. To make sure they can all see the same mutex, I defined it as static. Hope that explains it.

    Thanks, everyone!

  11. #8
    Join Date
    Aug 2014
    Posts
    1
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows Android
    Thanks
    1

    Default Re: static QMutex

    thank you, Mr. Nish.

Similar Threads

  1. Replies: 11
    Last Post: 13th July 2009, 17:05
  2. Using a QMutex as a static class member
    By emostar in forum Qt Programming
    Replies: 2
    Last Post: 15th June 2009, 14:48
  3. Replies: 22
    Last Post: 8th October 2008, 14:54
  4. new problem with simple C++ static variables
    By landonmkelsey in forum Qt Programming
    Replies: 2
    Last Post: 18th August 2008, 06:42
  5. Replies: 16
    Last Post: 23rd May 2008, 11:12

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.