Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Question about "singleton in multi-thread"

Question about "singleton in multi-thread"

Scheduled Pinned Locked Moved C / C++ / MFC
question
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    chenxiujie
    wrote on last edited by
    #1

    Hello, everyone! I encounter a strange question. I defined a singleton class like this: static CMySocket& GetMySocket(); CMySocket& CMySocket::GetMySocket() { static CMySocket temp; return temp; } In single thread, it works properly, but in Multi-thread, (I can ensure in main thread, invoke CMySocket::GetMySocket first), when invoke GetMySocket I get different instance , who can tell me , why? Thanks a lot and best regards.

    Y 1 Reply Last reply
    0
    • C chenxiujie

      Hello, everyone! I encounter a strange question. I defined a singleton class like this: static CMySocket& GetMySocket(); CMySocket& CMySocket::GetMySocket() { static CMySocket temp; return temp; } In single thread, it works properly, but in Multi-thread, (I can ensure in main thread, invoke CMySocket::GetMySocket first), when invoke GetMySocket I get different instance , who can tell me , why? Thanks a lot and best regards.

      Y Offline
      Y Offline
      YaronNir
      wrote on last edited by
      #2

      As i recall u need to declare a certain macro for a singleton class both when using it in a single threaded and apartment. try to look in the MSDN for DECLARE_CLASSFACTORY_SINGLETON macro. hope this helps you Yaron Ask not what your application can do for you, Ask what you can do for your application

      T 1 Reply Last reply
      0
      • Y YaronNir

        As i recall u need to declare a certain macro for a singleton class both when using it in a single threaded and apartment. try to look in the MSDN for DECLARE_CLASSFACTORY_SINGLETON macro. hope this helps you Yaron Ask not what your application can do for you, Ask what you can do for your application

        T Offline
        T Offline
        ThatsAlok
        wrote on last edited by
        #3

        YaronNir wrote:

        try to look in the MSDN for DECLARE_CLASSFACTORY_SINGLETON macro.

        that use to make COM Class SingleTon!

        Y 1 Reply Last reply
        0
        • T ThatsAlok

          YaronNir wrote:

          try to look in the MSDN for DECLARE_CLASSFACTORY_SINGLETON macro.

          that use to make COM Class SingleTon!

          Y Offline
          Y Offline
          YaronNir
          wrote on last edited by
          #4

          right! if he isn't using COM, i suggest he use a protection (critical section) because if in multithreaded application both threads recquire an instance to the class, one should acquire a real one while the other acquire a false one..... hope this helps Ask not what your application can do for you, Ask what you can do for your application

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups