Search code examples
multithreadingcachingsimulatorprocessorcpu-cores

Way to synchronize two cores in simulation


I have to build a dual-core processor simulator in C (it's actually a multilevel memory simulation, cache L1/L2, block substitution, etc). Thing is, I'm having a hard time figuring a way to synchronize the cores (which I'm programming as threads). Any ideas how I could do a global clock? Should I change from threads to child processes? Thanks in advance


Solution

  • So many options here: since you might end-up supporting complex interactions with multiple threads, you might want to consider using a "virtual clock" with a message passing "bus". This way, you'll have more time to focus on the core functionality instead of debugging the synchonization logic...

    Using this technique, you can build a state-machine (see here) per "actor" thread (worst case) and worry less about mutexes/conditions. Once you've got this base, you'll be able to handle cases that show up mid-stream (e.g. "I forgot about this detail... no worries, just add a state here... don't have to re-shuffle my mutexes).

    Also, since doing simulation is all about "virtual time" (since you can't run real-time !), then having a base architecture based on a "virtual clock" abstracts the problem to an appropriate level.