|
Access to Shared Data |
Top Previous Next |
|
If multiple threads in a task are operating on shared data, say a buffer held in static storage or on the heap, they must synchronize their access to the data. This is because the compilers used to generate the executing code know nothing about multiple threads; they operate with the assumption that code is completely sequential.
Note that variables shared between threads may need to be declared as volatile to make sure that changes made by one thread are visible to other threads. Consider what would happen if the compiled code of a particular thread function were to keep a shared variable in a register; changes made by other threads that access the variable in memory would be invisible. Using volatile prevents the compiler from keeping a variable in a register.
Threads can synchronize their operations using either semaphores or channels.
|