mutex to protect read?

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

mutex to protect read?

Post by mattallen37 »

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.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: mutex to protect read?

Post by afanofosc »

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
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: mutex to protect read?

Post by mattallen37 »

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)?
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: mutex to protect read?

Post by afanofosc »

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
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 2 guests