Использование C/Pthreads: совместно используемые переменные должны быть энергозависимы?

Ориентация черепахи может быть определена по производной вашей функции в текущей позиции.

Если у вас есть функция как функция sympy, вы можете попросить Python провести дифференцирование. Или вы можете просто сделать это самостоятельно. Если ваша функция

y = x^2

, то производная равна

dy = 2 * x

Учитывая, что производная в текущей позиции, ее арктангенс дает вам направление черепахи:

t.setheading(math.atan(dy))

Убедитесь, что угол поворота черепахи установлен в радианах или переведите их в градусы

t.setheading(math.degrees(math.atan(dy)))
31
задан fuad 17 September 2008 в 00:03
поделиться

2 ответа

The answer is absolutely, unequivocally, NO. You do not need to use 'volatile' in addition to proper synchronization primitives. Everything that needs to be done are done by these primitives.

The use of 'volatile' is neither necessary nor sufficient. It's not necessary because the proper synchronization primitives are sufficient. It's not sufficient because it only disables some optimizations, not all of the ones that might bite you. For example, it does not guarantee either atomicity or visibility on another CPU.

But unless you use volatile, the compiler is free to cache the shared data in a register for any length of time... if you want your data to be written to be predictably written to actual memory and not just cached in a register by the compiler at its discretion, you will need to mark it as volatile. Alternatively, if you only access the shared data after you have left a function modifying it, you might be fine. But I would suggest not relying on blind luck to make sure that values are written back from registers to memory.

Right, but even if you do use volatile, the CPU is free to cache the shared data in a write posting buffer for any length of time. The set of optimizations that can bite you is not precisely the same as the set of optimizations that 'volatile' disables. So if you use 'volatile', you are relying on blind luck.

On the other hand, if you use sychronization primitives with defined multi-threaded semantics, you are guaranteed that things will work. As a plus, you don't take the huge performance hit of 'volatile'. So why not do things that way?

10
ответ дан 27 November 2019 в 22:32
поделиться

POSIX 7 гарантирует, что функции такой как pthread_lock также синхронизируют память

https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_11 "4.12 Синхронизации Памяти," говорит:

следующие функции синхронизируют память относительно других потоков:

pthread_barrier_wait()
pthread_cond_broadcast()
pthread_cond_signal()
pthread_cond_timedwait()
pthread_cond_wait()
pthread_create()
pthread_join()
pthread_mutex_lock()
pthread_mutex_timedlock()
pthread_mutex_trylock()
pthread_mutex_unlock()
pthread_spin_lock()
pthread_spin_trylock()
pthread_spin_unlock()
pthread_rwlock_rdlock()
pthread_rwlock_timedrdlock()
pthread_rwlock_timedwrlock()
pthread_rwlock_tryrdlock()
pthread_rwlock_trywrlock()
pthread_rwlock_unlock()
pthread_rwlock_wrlock()
sem_post()
sem_timedwait()
sem_trywait()
sem_wait()
semctl()
semop()
wait()
waitpid()

Поэтому, если Вашу переменную охраняют между pthread_mutex_lock и pthread_mutex_unlock затем, ей не нужна дальнейшая синхронизация, поскольку Вы могли бы попытаться предоставить volatile.

Связанные вопросы:

0
ответ дан 27 November 2019 в 22:32
поделиться
Другие вопросы по тегам:

Похожие вопросы: