# threading and shared data class

## Recommended Posts

##### Share on other sites
lollan    144
Hi,

You might want to edit your post ^^

##### Share on other sites
Gage64    1235
I think there's a bug in the forum software that sometimes causes posts with certain code snippets to not display.

Try copying your code to a pastebin site (like this one) and posting a link to it here instead.

##### Share on other sites
EDIT: that was weird... thx for the tip

ive made a simple system baed on the producer/consumer pattern for my engine...

basicly i have 2 thread one update thread(producer) and a render thread(consumer)... the basic idea is to keep two mirrored copies of all data for each thread... so if the update thread updates an object the class makes a copy of the modified object and sends it over to the render thread...

its not very advanced and i think its pretty much self explanatory

source

my question is how i can make this faster?

the problem im specifically thinking about is that the objects are "memcpy"id twice which is unecssary... it would be better if the consumer could simply switch pointers with the objectpool...

|¤| = state data|#| = change data| | = reusable memory       THREAD 1       ATOMIC QUEUE      THREAD 2STEP 1: send data to consumer (copy to a memory pool)                ->  |#| ->  |#| ->   producer|¤|                     |¤| consumer                 STEP 2: switch pointers (right now im simply copying again)                        ->  |#| ->    producer|¤|                     |#| <-> |¤| consumer                 STEP 2: recycle data                    |#| ->  |#| ->    producer|¤|                     |¤| consumer                          <- | |  <-

it would probably only be possible if i switched pointer for the chunks.. this requires however that the entire chunk is copied in the producer which is alright even if not all objects are modified since memcpy on the entire chunk would be faster than copying objects one by one... and i can only tweak the chunk size depending on how likely it is that the data will be changed and has to be copied..

im unsure how i would implement this properly or if its even a good idea... any help or suggestions?

EDIT: switch placed between producer and consumer...

[Edited by - Dragon_Strike on August 16, 2008 2:47:02 PM]