0

I know I am supposed to use mutexes but the way I currently use pthreads it would overly complicate the program...

anyway I basically have a variable which I use to denote if a thread is currently performing work or not. in the main thread I run over it in a while loop the check what threads are no longer busy. Now obviously my thread can write to this same variable once it is done.

Is it allowed to read and write from the same variable from 2 different threads, if 1 thread is ONLY reading and 1 thread is ONLY writing. reading of an old version is not of much concern since it will just read the correct once on the next iteration.

so is it safe to do something like that?

user3021085
  • 407
  • 2
  • 5
  • 16
  • This might be of interest: http://stackoverflow.com/questions/2125937/equivalent-of-interlockedincrement-in-linux-gcc – stevieg Mar 21 '14 at 01:31

1 Answers1

2

In general, NO.

The following article explains why:

http://www.domaigne.com/blog/computing/mutex-and-memory-visibility/

Here is a list of API functions that act as memory barriers:

http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_11

pat
  • 12,587
  • 1
  • 23
  • 52
  • thanks! However this doc from oracle does do something similiar to, it has 2 functions to write and read a variable. If I understand correctly though this still allows for writing and reading at the same time just not multiple amounts of reads or writes at the same time: http://docs.oracle.com/cd/E19455-01/806-5257/sync-12/index.html – user3021085 Mar 21 '14 at 02:13
  • 1
    that code is using `pthread_mutex`, which act as memory barriers. – pat Mar 21 '14 at 02:22
  • I thought mutex blocks a piece of code and the variable? the mutex that is set for the read also is valid for the write function? – user3021085 Mar 21 '14 at 02:27
  • 1
    Yes, the `pthread_mutex_unlock` serves to make sure that all prior writes have become globally visible. – pat Mar 21 '14 at 02:28
  • cool it is much easier than expected than. how does this work with arrays though. lets say I mutex lock an index of an array. now is only the index locked or the entire array? also pointers, what happens if I lock a pointer? can I still acces the memory pointed to it or not? – user3021085 Mar 21 '14 at 02:30
  • 1
    The mutex, and the memory that the mutex protects are separate. What the mutex actually protects is up to you to decide. You must enforce this by holding the correct mutex whenever you access the associated memory. Releasing a mutex guarantees that *all* prior writes have become globally visible; not just the ones that you intended to protect with the mutex. – pat Mar 21 '14 at 02:38
  • 1
    To clarify further, releasing a mutex guarantees that all prior writes will be visible to any thread that subsequently acquires the same mutex. – pat Mar 21 '14 at 02:49
  • thank I did find something interesting here: http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_11 . it says this at 4.11 "Applications may allow more than one thread of control to read a memory location simultaneously." so If I read from a variable it isn`t necassary to mutex it if global visibility isn`t of concern? – user3021085 Mar 23 '14 at 09:01