Is it necessary to protect a read of a global variable with a mutex? In one task I want to keep comparing a certain variable that another task is updating. Is this safe? It seems it should be, but what if it's an array value, or string?
I've been using mutexes for anything questionable, but I would like to know.
mutex to protect read?
-
- Posts: 1818
- Joined: 02 Oct 2010, 02:19
- Location: Michigan USA
- Contact:
mutex to protect read?
Matt
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
Re: mutex to protect read?
You do not need mutexes for either reads or writes of simple variables. Even structs, if you copy from one struct to another in a single operation. If you do something that involves multiple lines of code and you want (or need) to make sure that a read operation gets the wrong thing or a mangled thing (not really possible) then a mutex is probably called for. If more than one thread can update a queue, for example, and updating the queue involves replacing an element in an array as well as updating a top of queue index then you certainly should protect that process with a mutex. You can copy entire arrays or empty them or grow them in a single NBC opcode (mov, arrinit, or even arrbuild) so none of those type of operations require a mutex. As long as the operation is atomic (cannot be subdivided) then a mutex is not needed.
John Hansen
John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
http://bricxcc.sourceforge.net/
-
- Posts: 1818
- Joined: 02 Oct 2010, 02:19
- Location: Michigan USA
- Contact:
Re: mutex to protect read?
Ah, makes total sense. Thanks for the explanation!
So the VM task manager decides to go on to the next task after 20 NBC opcodes (regardless of what the FW needs to do to fulfill to opcode)?
So the VM task manager decides to go on to the next task after 20 NBC opcodes (regardless of what the FW needs to do to fulfill to opcode)?
Matt
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
http://mattallen37.wordpress.com/
I'm all for gun control... that's why I use both hands when shooting
Re: mutex to protect read?
If I understand you correctly then, yes, 20 opcodes per thread for as many threads as can execute 20 opcodes within a single millisecond (or until an opcode that causes the VM to switch to another thread, aka rotate the run queue).
Each opcode is atomic, whether it is a MOV that copies the entire contents of a 1000 element array of TLocationType structures to another array or a simple SET opcode that sets a byte variable to the value 10. Or an ARRBUILD opcode that takes 15 different array parameters and concatenates all of them into a single large output array. Or an ARRAYOP opcode that sorts a subset of an array into an output array. Or a MUL opcode that multiplies every element in a 500 element array of ints by an integer value. Each opcode is atomic. NXC code like array[5] = array2[5]; is not an atomic operation. Even x = x + 1; may not be atomic, depending on how the compiler optimizes this statement. ADD x, x, 1 is atomic.
John Hansen
Each opcode is atomic, whether it is a MOV that copies the entire contents of a 1000 element array of TLocationType structures to another array or a simple SET opcode that sets a byte variable to the value 10. Or an ARRBUILD opcode that takes 15 different array parameters and concatenates all of them into a single large output array. Or an ARRAYOP opcode that sorts a subset of an array into an output array. Or a MUL opcode that multiplies every element in a 500 element array of ints by an integer value. Each opcode is atomic. NXC code like array[5] = array2[5]; is not an atomic operation. Even x = x + 1; may not be atomic, depending on how the compiler optimizes this statement. ADD x, x, 1 is atomic.
John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
http://bricxcc.sourceforge.net/
Who is online
Users browsing this forum: No registered users and 4 guests