Answers.Body: <p>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. </p>
  
  <p>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. </p>
  
  <p>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. </p>
  
  <p>A simple solution is to create multiple copies of the offending MyBlock and have each thread run their own copy.  </p>
  
  <p>(DavidL's answer was fine.  I just offer this for more completeness.  Happy programming!)</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-21T18:30:27.310Z
Answers.LastActivityDate: 2015-11-16T13:34:23.267Z
Answers.LastEditDate: 2015-11-16T13:34:23.267Z
Answers.UserDisplayName: mstahl
Answers.Body: <p>I could not find this in the help file, but according to <a href="http://forums.usfirst.org/showthread.php?20838-Spawning-background-threads-in-EV3-switch-blocks&amp;p=78008#post78008">this thread</a> "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."</p>
  
  <p>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.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-21T19:50:33.363Z
Answers.Text: Well, that would certainly explain it. Nicely found @David, thanks :-)
Answers.UserDisplayName: freefaller
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-21T19:27:00.830Z
Answers.LastActivityDate: 2014-07-21T19:27:00.830Z
Answers.UserDisplayName: David Lechner
Body: <p>TL;DR = Is there any obvious reason why a MyBlock, added twice in parallel to a program, will only run one of them?</p>
  
  <hr>
  
  <p>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.)</p>
  
  <p>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.</p>
  
  <p>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.)</p>
  
  <p>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.</p>
  
  <p>I have also replicated (at great length) the blocks in brand new programs, and they work without issue.</p>
  
  <p>Why would two blocks running in parallel not work, when the same code replicated on the program does?</p>
  
  <hr>
  
  <p><strong>UPDATE</strong></p>
  
  <p>As requested (and I should have done originally).</p>
  
  <p>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</p>
  
  <p><img src="https://i.sstatic.net/r27hP.png" alt="MyBlock part 1"></p>
  
  <p>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.</p>
  
  <p><img src="https://i.sstatic.net/1BwMi.png" alt="MyBlock part 2"></p>
  
  <p>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.</p>
  
  <p><img src="https://i.sstatic.net/oID6t.png" alt="MyBlock part 3"></p>
  
  <p>And in this case if the value is above the "offset" value.</p>
  
  <p><img src="https://i.sstatic.net/p89SD.png" alt="MyBlock part 4"></p>
  
  <p>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.</p>
  
  <p><img src="https://i.sstatic.net/gZGyy.png" alt="MyBlock part 5">  <img src="https://i.sstatic.net/VFQXr.png" alt="MyBlock part 6"></p>
  
  <p>But, if I run the following, <strong>only the last MyBlock to be wired works</strong>.  There is literally movement from just one direction motor.</p>
  
  <p><img src="https://i.sstatic.net/MLfvp.png" alt="MyBlock part 7"></p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-07-20T19:33:53.533Z
Comments.Text: Please, a screenshot or two.
Comments.UserDisplayName: dfrevert
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-07-21T08:26:19.737Z
Comments.Text: 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
Comments.UserDisplayName: freefaller
Tags: <mindstorms><ev3><programming>
Title: EV3 parallel MyBlocks, but only one working
UserDisplayName: freefaller
ThreadText: # EV3 parallel MyBlocks, but only one working
  
  (Asked by: username_0 on 2014-07-20)
  
  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?
  
  ---
  
  **UPDATE**
  
  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](https://i.sstatic.net/r27hP.png)
  
  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](https://i.sstatic.net/1BwMi.png)
  
  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](https://i.sstatic.net/oID6t.png)
  
  And in this case if the value is above the "offset" value.
  
  ![MyBlock part 4](https://i.sstatic.net/p89SD.png)
  
  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](https://i.sstatic.net/gZGyy.png) ![MyBlock part 6](https://i.sstatic.net/VFQXr.png)
  
  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](https://i.sstatic.net/MLfvp.png)
  
  **Tags:** mindstorms, ev3, programming
  > **Comments:**
  > **username_1**: Please, a screenshot or two.  
  > **username_0**: 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  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_3 on 2014-07-21. Score: 7 | Accepted answer
  
  I could not find this in the help file, but according to [this thread](http://forums.usfirst.org/showthread.php?20838-Spawning-background-threads-in-EV3-switch-blocks&p=78008#post78008) "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.
  
  > **Comments:**
  > **username_0**: Well, that would certainly explain it. Nicely found @David, thanks :-)  
  
  ---
  
  ### Answer by username_2 on 2015-07-21. Score: 3
  
  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!)
  
  
  ---
  
  