Results 1 to 11 of 11

Thread: global variable declaration

Hybrid View

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

    Default Re: global variable declaration

    something like this:
    Its not thread-safe and leaks memory Try again

  2. #2
    Join Date
    Nov 2011
    Posts
    79
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    5
    Thanked 5 Times in 5 Posts

    Default Re: global variable declaration

    Ehhm ...
    Quote Originally Posted by stampede View Post
    ... and leaks memory
    Yes, you have to destroy the global object when program terminates. But it was just an idea, an example of singleton. You can build it with static member etc.
    If you want it thread safe, use semaphores or eny else method.
    But idea of singleton is good, in any case it more useful then you comment

    Cheers

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

    Default Re: global variable declaration

    Ok, ok I know Anyway, here is a paper on Singleton and Double-Checked Locking, by Meyers and Alexandrescu http://www.aristeia.com/Papers/DDJ_J...04_revised.pdf.
    I think its good to be aware of the problems with singletons.
    Ok sorry guys end of off-topic.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: global variable declaration

    Quote Originally Posted by stampede View Post
    Ok, ok I know Anyway, here is a paper on Singleton and Double-Checked Locking, by Meyers and Alexandrescu http://www.aristeia.com/Papers/DDJ_J...04_revised.pdf.
    I think its good to be aware of the problems with singletons.
    Ok sorry guys end of off-topic.
    You can also protect singleton creation by using fetch-and-add semantics (e.g. with QAtomicInt) or simply by creating the singleton instance manually in the main thread so that instance() always returns an existing object and never tries to create one (just like QCoreApplication does). Another viable approach is to use a wait condition so that many threads at once can execute instance() but still just one can create the singleton instance.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts

    Default Re: global variable declaration

    And all singleton problems can be neatly avoided by creating the shared object explicitly and passing it to all objects that need it

    It can be constructed before any of the threads are, can be deleted after all of them have finished, creation can take arguments, ...

    Not even talking about making unittest easier since each test can have a clean instance instead of using the same instance over and over again.

    Cheers,
    _

Similar Threads

  1. Global declaration
    By nighil in forum Newbie
    Replies: 1
    Last Post: 24th January 2011, 16:51
  2. global variable in QT: getting ISSUES
    By Girija in forum Qt Programming
    Replies: 8
    Last Post: 19th September 2010, 16:15
  3. function prototype variable declaration
    By john_god in forum General Programming
    Replies: 4
    Last Post: 31st July 2010, 18:47
  4. global variable
    By Shuchi Agrawal in forum General Programming
    Replies: 10
    Last Post: 15th February 2007, 04:19
  5. declaration of global variables???
    By pranav_kavi in forum Newbie
    Replies: 6
    Last Post: 31st January 2006, 19:56

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