TL;DR = Is there any obvious reason why a MyBlock, added twice in parallel to a program, will only run one of them?

In my EV3-G program, I have a MyBlock which uses the Proximity Heading to control a motor so that the heading is always straight ahead. (It has some added complexity, as it slows down when getting close, etc.)

My robot-arm has two infrared sensors, one mounted horizontally, one mounted vertically, so the arm can always point at the beacon... and the MyBlock has been setup with parameters, so that the corresponding port/motor numbers are passed in.

Is there any reason why two MyBlocks, running in parallel, would only work on one of those blocks, and not the other? (When debugging, both blocks are "shimmering", show they are both apparently running.)

I have double-tripple-quadruple checked that all the flow-control, sensor and actions are working with the correct parameter values, and running each block individually works perfectly.

I have also replicated (at great length) the blocks in brand new programs, and they work without issue.

Why would two blocks running in parallel not work, when the same code replicated on the program does?


As requested (and I should have done originally).

Here is the MyBlock. It starts with 3 parameters, the sensor port, the motor port and an "offset" - all numeric. The loop starts, and a direction heading reading is taken. It then uses the "offset" value, and passes "offet - 5" and "offet + 5" into the range

MyBlock part 1

Based on the boolean value from the range, if the current heading value is within the range, then it runs the motor at a "graded" speed, based on the heading value. Therefore as it gets closer to 0 the slower the motor is run.

MyBlock part 2

If the current heading value is outside the range, then the motor is run at a non-variable speed... in this case if the value is below the "offset" value.

MyBlock part 3

And in this case if the value is above the "offset" value.

MyBlock part 4

The program simply calls two instances of the MyBlock. If I run the left one, the horizontal detection and movement works without issue - if I run the right one, the vertical detection and movement works without issue.

MyBlock part 5 MyBlock part 6

But, if I run the following, only the last MyBlock to be wired works. There is literally movement from just one direction motor.

MyBlock part 7

  • Please, a screenshot or two. – dfrevert Jul 20 '14 at 19:33
  • Hi @dfrevert, of course - that is something I should have done up front - apologies. Hope the explanation is clear, please let me know any more clarification is needed – freefaller Jul 21 '14 at 8:26

I could not find this in the help file, but according to this thread "Another little known characteristic of MyBlocks is that they aren't re-entrant. No two copies of the same MyBlock can be running at the same time."

I tried a few simple programs in EV3-G and confirmed that this is true. The order in which they run appears to be random.

  • Well, that would certainly explain it. Nicely found @David, thanks :-) – freefaller Jul 21 '14 at 19:50

MyBlocks are not reentrant; only one thread can be using the block at a time. When a second thread tries to execute a MyBlock that is being used by another, the second thread blocks until the first thread exits the block.

This normally isn't a problem for MyBlocks that do simple calculations. They finish quickly and all threads make progress. But if a MyBlock contains a loop or wait, other threads that use the same MyBlock can be blocked indefinitely.

From the debugging view, a blocked MyBlock shows as active. This makes it very difficult to debug. You might try placing different sounds before and after the MyBlock, to see which thread is getting stuck.

A simple solution is to create multiple copies of the offending MyBlock and have each thread run their own copy.

(DavidL's answer was fine. I just offer this for more completeness. Happy programming!)

Your Answer

