I wonder wether pthreads can share variables safely, without using mutex, in the following special cases:
(1)
a structure or array is shared but each single element of the array, or field of the structure, is not shared.
Probably it is also necessary the assumption that these data are extern or static
Example :
pthread 1 uses only Struct[0].field1 , Struct[0].field2 and Struct[1].field3
pthread 2 uses only Struct[1].field1 and Struct[0].field3
(2)
from a certain moment on I am sure that some data are only read by (and not written by) all threads
Probably it is also necessary the assumption that these data are extern or static
Example:
Struct[whatever].fieldAny can be read by any thread but no one will write it
(3)
Other special question:
can pthreads share a mutex directly or do they have to access it via separate pointers?
Example:
pthread1 calls pthread_mutex_lock(&mutex) ... pthread_mutex_unlock(&mutex)
pthread2 calls pthread_mutex_lock(&mutex) ... pthread_mutex_unlock(&mutex)
or is it necessary that
main thread runs mutex1=&mutex mutex2=&mutex then creates thread1 and thread2 where
pthread1 calls pthread_mutex_lock(mutex1) ... pthread_mutex_unlock(mutex1)
pthread2 calls pthread_mutex_lock(mutex2) ... pthread_mutex_unlock(mutex2)
Thank you for help