Register for your free account! | Forgot your password?

Go Back   elitepvpers > MMORPGs > Conquer Online 2 > CO2 Programming
You last visited: Today at 13:46

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[Discussion] - LockFree ThreadSafe Dynamic Memory Allocation

Discussion on [Discussion] - LockFree ThreadSafe Dynamic Memory Allocation within the CO2 Programming forum part of the Conquer Online 2 category.

Reply
 
Old   #1
 
clintonselke's Avatar
 
elite*gold: 0
Join Date: Feb 2007
Posts: 348
Received Thanks: 2,175
Lightbulb [Discussion] - LockFree ThreadSafe Dynamic Memory Allocation

I thought this might be an interesting discussion,

Has anyone heard of this being possible?, (from the title "LockFree ThreadSafe Dynamic Memory Allocation")

I only just learned recently that dynamic memory allocation isn't thread safe (for native code anyways). Have not really been a big fan of using threads until recently, starts to change as computers get more CPUs. If you only have 1 CPU, then its more efficient to find a way to do something with just 1 thread (if possible).

One thing I did find that was very interesting was a LockFree Queue, which allows 1 reader thread, and 1 writer thread to work on the one queue simultaneously without corruption. It improves performance over locking a regular queue by over a factor of 10 times (for just reading/writing to/from queue, w/o doing work on the data itself). Using Mutexes generally causes bottlenecks in programs, making threads compete for the one resource... where as, using lockfree algorithms by the use of atomic operations, the structs are never in an invalid state during modification which means multiple threads can use them at the same time w/o causing corruption.

Imagine a intersection on the road with traffic lights... Locking is the use of the traffic lights to allow cars to share the resource (the section of road in the middle of the intersection, to reach the other side).

Now what Lock-Free algorithms using atomic operations introduces is, a new road where cars don't have to stop, they can just pass through each other like ghosts... spooky xD, but freaky cool, and improves performance so much.

Now Time For Some Inspiration the LockFree Queue



Now next thing is, I believe this can also be done with a Heap Algorithm, through the use of atomic operations. To have multiple threads allocating / de-allocating memory without the need to lock.

Sure a LockFree Queue is only 1 reader and 1 writer but...

If every thread had its own lockfree queue, then you can mimic the effect of a single LockFree Queue (by join several queues in parallel) to allow many writers and 1 reader (I've done that b4 and it works well).

Now for those that don't know already, a Heap structure is one that efficiently stores values by some size, and its very efficent to get the smallest or largest value (or object by some key).

What I'd want for the Heap structure is multiple readers and multiple writers (I think, still working it out). Every allocation of memory (malloc / new) would do a read on the heap object. Every deallocation of memory (free / delete) would do a write to the heap object. The heap object would essentially hold blocks of free space.

Joining back together two adjacent blocks of free space into 1 chunk, I'm really not concerned about at this state, as each bit of memory allocated is roughly the same size. And if i need smaller memory than available, I can always pass over a chunk that is slightly larger than required, then when its freed, that chunk goes back on the heap again.

This plan is only to work around the Dynamic Memory Allocation not being thread safe... last resort is locking, sigh :P

If anyone has anything to add, or anything they've discovered online or somewhere, feel free to post it below.
clintonselke is offline  
Thanks
1 User
Old 09/18/2010, 09:50   #2
 
InfamousNoone's Avatar
 
elite*gold: 20
Join Date: Jan 2008
Posts: 2,012
Received Thanks: 2,882
Me and Saint both face-palmed at this.
InfamousNoone is offline  
Old 09/18/2010, 13:06   #3
 
clintonselke's Avatar
 
elite*gold: 0
Join Date: Feb 2007
Posts: 348
Received Thanks: 2,175
Quote:
Originally Posted by InfamousNoone View Post
Me and Saint both face-palmed at this.
Feel like writing me a lock free memory pool?
clintonselke is offline  
Old 09/19/2010, 19:49   #4
 
gabrola's Avatar
 
elite*gold: 0
Join Date: Dec 2006
Posts: 1,039
Received Thanks: 1,335
Ever heard of pthreads?
gabrola is offline  
Old 09/19/2010, 23:56   #5
 
elite*gold: 0
Join Date: Sep 2008
Posts: 694
Received Thanks: 207
Quote:
Originally Posted by gabrola View Post
Ever heard of pthreads?
***** threads 0.o?
DragonHeart is offline  
Old 09/20/2010, 13:15   #6
 
bone-you's Avatar
 
elite*gold: 20
Join Date: Mar 2006
Posts: 1,491
Received Thanks: 536
From skimming it, it looks like what I did in my socket system for c++.
bone-you is offline  
Old 09/20/2010, 18:23   #7
 
gabrola's Avatar
 
elite*gold: 0
Join Date: Dec 2006
Posts: 1,039
Received Thanks: 1,335

gabrola is offline  
Thanks
1 User
Old 09/21/2010, 15:00   #8
 
clintonselke's Avatar
 
elite*gold: 0
Join Date: Feb 2007
Posts: 348
Received Thanks: 2,175
Quote:
Originally Posted by gabrola View Post

lol, exactly what im after, thx.
clintonselke is offline  
Reply

Tags
atomic operations, lock free, memory allocation, threadsafe


Similar Threads Similar Threads
2nd Reborn ninja best attribute allocation for pking and general advice
07/12/2010 - Conquer Online 2 - 14 Replies
What is the best attribute allocation is for a ninja (130) --> ninja (130) --> ninja (120 ) would be? should i get the required amount of agility and then spam strength for damage or what? or spam vitality for health? Also looking for tips on gear, equip bonuses and just anything to get a rookie off the ground and into the pk'ing world of Co. all opinions are valued. Thanks.
Bei Neuer Quest (allocation failed error)!!! Bitte helft mir
05/30/2010 - Metin2 Private Server - 0 Replies
Ich finde den Fehler nicht pls help
AoCheat survives the patch with dynamic memory
05/28/2008 - General Gaming Discussion - 1 Replies
A principle feature of AoCheat is that it located AgeOfConan's memory dynamically (except for structure offsets). This means that AoCheat will break only if AgeOfConan's internal code changes significantly. This eliminates almost all of the downtime that other programs experience between updates. ;) additionally: Age of Conan Forums
[TUT REQUEST] Dynamic Memory Editing via dll injection
01/09/2008 - General Coding - 2 Replies
Hi there im trying to edit the memory of a game via dll injection .. but the adresses arent static, they are dynamic, what means that they change every restart.. and i dont know how to edit dynamic adresses with a dll injection.. could anyone post a tutorial how i could do that?? thanks beforehand greetz eMale
Reborn Troj Stat Allocation Bug
09/21/2005 - Conquer Online 2 - 4 Replies
http://bbs.co2.91.com/showthread.php?s=&thread id=3605 read down a little bit and some guy refers to a bug allowing trojans to have lot more hp, does anyone know how to get that much hp?



All times are GMT +2. The time now is 13:46.


Powered by vBulletin®
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2024 elitepvpers All Rights Reserved.