Thread Synchronisation 101

前端 未结 6 528
深忆病人
深忆病人 2021-02-01 06:55

Previously I\'ve written some very simple multithreaded code, and I\'ve always been aware that at any time there could be a context switch right in the middle of what I\'m doing

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-01 07:05

    Q1: Using the "volatile" keyword

    In VS2005 and later the C++ compiler surrounds access to this variable using memory barriers, ensuring that the variable is always completely written/read to the main system memory before using it.

    Exactly. If you are not creating portable code, Visual Studio implements it exactly this way. If you want to be portable, your options are currently "limited". Until C++0x there is no portable way how to specify atomic operations with guaranteed read/write ordering and you need to implement per-platform solutions. That said, boost already did the dirty job for you, and you can use its atomic primitives.

    Q2: Variable needs to be 4-byte/8-byte aligned?

    If you do keep them aligned, you are safe. If you do not, rules are complicated (cache lines, ...), therefore the safest way is to keep them aligned, as this is easy to achieve.

    Q3: Should this code be rewritten to use mutexes?

    Critical section is a lightweight mutex. Unless you need to synchronize between processes, use critical sections.

    Q4: Where are critical sections/mutexes/semaphores/spinlocks best suited?

    Critical sections can even do spin waits for you.

    Q5: Spinlocks should not be used in a single-core

    Spin lock uses the fact that while the waiting CPU is spinning, another CPU may release the lock. This cannot happen with one CPU only, therefore it is only a waste of time there. On multi-CPU spin locks can be good idea, but it depends on how often the spin wait will be successful. The idea is waiting for a short while is a lot faster then doing context switch there and back again, therefore if the wait it likely to be short, it is better to wait.

提交回复
热议问题