|
-
April 3rd, 2009, 08:43 PM
#21
Re: ::std::list::push_back()
 Originally Posted by Lindley
STL vectors work in the obvious way, really. They double their capacity and copy each time they run out of space. That's pretty much the entire algorithm.
Good.
But how does the algorithm work when I remove an object which is in the middle of the collection?
Anyway, to be EXACT as some folks asks:
It's a TCP server program.
On each new connection, an object (let's call it class "Client") is being pulled out of the pre-allocated pool, and assigned to the new connection.
Therefore, when a client disconnects, I do not "delete" the object but I memset() it to 0x00, and I put it back in the pool.
This is just about it when I say I want to avoid memory fragmentation.
Now, besides that I have a worker thread that checks for scheduled tasks concerning the server's operation (it's a game server).
What kind of operations?
For example a time-out operation. A client has X seconds to act (in a certain state of the game), otherwise, if he didn't react, he's out of the game.
So when the game advances, a new checking task is being submitted to the time-out-thread-checker.
This thread iterates between the tasks.
When X seconds has passed and the thread iterated to this task, it checks for this and that.
If the conditions are met, the task should be removed from the collection.
Very simple to understand I think, and hope.
I'm really trying to cooperate. 
So, I understand that vector will resize itself when I add new tasks.
But what happens when I erase a task? Would shrink, ever? Would it copy all the following tasks in the array to a new position?
I'm looking for as less as possible operations of relocation to be made because as I said, it might be few hundreds of tasks each iterations.
By the way:
Each task is actually a class which describes that task.
This is why inside the collection I'll only store pointers, because as in the Client objects, I allocate many task object at the start of the program and re-use each one.
I really hope I'm clear on that now.
I not then god mercy
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|