I was reading up on design patterns, to better my understandings of ones I know and reveal ones I didn't. One of the last ones I looks at was the Flyweight Pattern. It preaches about shared common function, and I understand this. Keeping with the game example (in the link), sharing functions for units in an RTS I'm sure would save on memory. But would this not sacrifice a great deal of [speed] performance?

Keeping with the RTS theme, suppose you have 300 units on the field, all sharing the same run function, would this not be an asynchronous nightmare? And if one was do perform this in-line (via maybe a step/render style engine), would this not bog the the CPU?

1 Answer 1


Just because code is shared over threads doesn't mean that it is not thread safe. Only when threads share mutable state does it need to be synchronized; note that code is immutable.

also the code for each function is stored once in memory (barring optimizations) even if there are hundreds of objects and can be used by multiple threads at the same time as long as the data it acts upon is different.

  • Ok, I see, that does make it make much more sense.
    – ahodder
    Commented Mar 13, 2012 at 21:45
  • @ratchetfreak: code is immutable in /most/ cases. There is such a thing as self modifying code.
    – jmoreno
    Commented Mar 14, 2012 at 3:29
  • @jmoreno Do you have an example of some? I think I know what you are talking about, but I would like to be sure.
    – ahodder
    Commented Mar 14, 2012 at 17:22
  • @AedonEtLIRA: I think the wikipedia article adequately covers it. Code can be modified while running, before running, when finishing running. You obviously need to get the new code right, but then again you want to get all of your code right...
    – jmoreno
    Commented Mar 15, 2012 at 3:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.