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. How to make the Memory Pool thread safe ? [modified][solved]

How to make the Memory Pool thread safe ? [modified][solved]

Scheduled Pinned Locked Moved C / C++ / MFC
performancetutorialquestioncode-review
6 Posts 5 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.
  • Y Offline
    Y Offline
    yu jian
    wrote on last edited by
    #1

    I hear that the memory pool can improve the performance. It means that allocate a block of memory and then new the class object in this memory block, but how to make the thread safe and proceeding safe?

    modified on Wednesday, September 22, 2010 11:10 AM

    A B B 3 Replies Last reply
    0
    • Y yu jian

      I hear that the memory pool can improve the performance. It means that allocate a block of memory and then new the class object in this memory block, but how to make the thread safe and proceeding safe?

      modified on Wednesday, September 22, 2010 11:10 AM

      A Offline
      A Offline
      Aescleal
      wrote on last edited by
      #2

      A memory pool (implemented as a custom allocator or overiding new/delete for a class) can improve speed a lot. HOWEVER before you do make sure that your app is actually eating up loads of execution time in the ordinary allocator before doing anything about it. I've seen plenty of people mess about and spend ages optimising allocators and get nothing concrete out of it. If you decide to use a custom allocator then you make it thread safe the way you do any other object - use a synchronisation object and lock it when doing any operations on the object. Incidentally I've seen the cost of synchronisation wipe out any benefit a custom allocator gives you, so again make sure you're just not replacing a runtime library bottleneck with a carefully crafted homemade one. There are ways you can reduce locking bottlenecks from an allocator but you start needing to use multiple arenas to allocate from and switch between them on each allocation. This can then start messing up locality of reference (unless you manage interleaved arenas) and again slow everything down again. Anyway, the point I'm really making here is measure how long your app spends in the allocator. If it's taking an appreciable length of time consider using a third party custom allocator or implementation of new and delete and only write your own if it's still not fast enough. Another idea would be to use a garbage collector - if you google "Hans" "Boehm" and "garbage collection" you can find a decent one (if you like that sort of thing) that's written by one of the luminaries of the C++ standards committee. Garbage collection has the advantage that when you delete objects the memory is not immediately freed and is recovered in one fell swoop later by the garbage collector reducing contention for the allocator when releasing objects. Cheers, Ash

      modified on Monday, September 20, 2010 7:05 AM

      M 1 Reply Last reply
      0
      • A Aescleal

        A memory pool (implemented as a custom allocator or overiding new/delete for a class) can improve speed a lot. HOWEVER before you do make sure that your app is actually eating up loads of execution time in the ordinary allocator before doing anything about it. I've seen plenty of people mess about and spend ages optimising allocators and get nothing concrete out of it. If you decide to use a custom allocator then you make it thread safe the way you do any other object - use a synchronisation object and lock it when doing any operations on the object. Incidentally I've seen the cost of synchronisation wipe out any benefit a custom allocator gives you, so again make sure you're just not replacing a runtime library bottleneck with a carefully crafted homemade one. There are ways you can reduce locking bottlenecks from an allocator but you start needing to use multiple arenas to allocate from and switch between them on each allocation. This can then start messing up locality of reference (unless you manage interleaved arenas) and again slow everything down again. Anyway, the point I'm really making here is measure how long your app spends in the allocator. If it's taking an appreciable length of time consider using a third party custom allocator or implementation of new and delete and only write your own if it's still not fast enough. Another idea would be to use a garbage collector - if you google "Hans" "Boehm" and "garbage collection" you can find a decent one (if you like that sort of thing) that's written by one of the luminaries of the C++ standards committee. Garbage collection has the advantage that when you delete objects the memory is not immediately freed and is recovered in one fell swoop later by the garbage collector reducing contention for the allocator when releasing objects. Cheers, Ash

        modified on Monday, September 20, 2010 7:05 AM

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #3

        good answer. I've bumped (5-ved) the question also.

        Watched code never compiles.

        A 1 Reply Last reply
        0
        • M Maximilien

          good answer. I've bumped (5-ved) the question also.

          Watched code never compiles.

          A Offline
          A Offline
          Aescleal
          wrote on last edited by
          #4

          Thanks for the reply. I've given the questioner a five as well - normally I wouldn't but in this case his question didn't deserve the one someone else gave him so I'm attempting to be karmic. Cheers, Ash

          1 Reply Last reply
          0
          • Y yu jian

            I hear that the memory pool can improve the performance. It means that allocate a block of memory and then new the class object in this memory block, but how to make the thread safe and proceeding safe?

            modified on Wednesday, September 22, 2010 11:10 AM

            B Offline
            B Offline
            basementman
            wrote on last edited by
            #5

            Override the new/delete operators and use "placement new" syntax to alloc in a predetermined location, managed by you.

            onwards and upwards...

            1 Reply Last reply
            0
            • Y yu jian

              I hear that the memory pool can improve the performance. It means that allocate a block of memory and then new the class object in this memory block, but how to make the thread safe and proceeding safe?

              modified on Wednesday, September 22, 2010 11:10 AM

              B Offline
              B Offline
              Blake Miller
              wrote on last edited by
              #6

              If you take the memory from your own Win32 Heap, then it is already thread-safe. See HeapCreate.

              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