Running same algorithm simultaneously?

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
dsjove
Posts: 56
Joined: 22 Jan 2011, 23:22

Running same algorithm simultaneously?

Post by dsjove »

Hello,

I am using NXC to automate my trains. I want to apply the same functions/tasks to many components. I do not see a way to do this. With the sample code below only task1 is executed.

Code: Select all

void run(int ref)
{
	while (true)
	{
		NumOut(0, (ref == 0 ? LCD_LINE1 : LCD_LINE2), ref);
	}
}

task task0()
{
	run(0);
}

task task1()
{
	run(1);
}

task main()
{
	StartTask(task0);
	StartTask(task1);
}
hergipotter
Posts: 48
Joined: 12 Jan 2011, 18:40
Contact:

Re: Running same algorithm simultaneously?

Post by hergipotter »

I'm not sure if it helps, but did you try to inline the function?

Code: Select all

inline void run(int ref)
{
   while (true)
   {
      NumOut(0, (ref == 0 ? LCD_LINE1 : LCD_LINE2), ref);
   }
}
link to my youtube channel under my avatar!
m-goldberg
Posts: 73
Joined: 29 Sep 2010, 12:05

Re: Running same algorithm simultaneously?

Post by m-goldberg »

By using "inline" you don't actually share the function.

To share, the function that will be shared must be protected -- while one task is using it, the others must be made to wait until the first task is done. One way to do that is declare the function with "safecall" as shown in the following example.

Code: Select all

safecall void run(int ref)
{
   NumOut(0, (ref == 0 ? LCD_LINE1 : LCD_LINE2), ref);
}

task task0()
{
   while(true)
   {
      run(0);
      Yield();
   }
}

task task1()
{
   while(true)
   {
      run(1);
      Yield();
   }
}

task main()
{
   Precedes(task0, task1);
}
Regards, Morton
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Running same algorithm simultaneously?

Post by HaWe »

but if "the others" must wait until the first task is done...: will it run then really simultaneously...? ;)
hergipotter
Posts: 48
Joined: 12 Jan 2011, 18:40
Contact:

Re: Running same algorithm simultaneously?

Post by hergipotter »

On a 1-Processor Machine, nothing can run really simultaneously.
link to my youtube channel under my avatar!
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Running same algorithm simultaneously?

Post by HaWe »

I guess you know what I meant, don't you?
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Running same algorithm simultaneously?

Post by afanofosc »

Function calls in NXC are not reentrant/threadsafe so you cannot call the same function from two separate tasks safely unless you either force all function calls to be "safecall" via a compiler switch or you mark the specific functions that you want to call from multiple threads as safecall functions. Just like Morton demonstrated above. The alternative is to inline the function containing the code you want multiple tasks to execute. That would let you keep the while loop in the function but as Morton indicated it would actually emit a copy of the inline function at the call site as hergipotter described.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
dsjove
Posts: 56
Joined: 22 Jan 2011, 23:22

Re: Running same algorithm simultaneously?

Post by dsjove »

There have been multi-threads in single-core machines for decades! It is a scheduling/context-switching problem an OS hopefully has implemented. Reentrancy and (user-level) thread-safety are separate issues. Any shared resources (global data and IRLink) the functions to operate on I have protected by mutexes. The mutexes nicely fix the thread-safety problem. safecall is a brute-force mutex that is not isolated around the data it is trying to protect.

It sounds like to make a function reentrant (I am not even asking for recursion!) I have to make it and all its sub-routines inline?

What exactly happens to execution when you try to call a function that another task is executing?
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: Running same algorithm simultaneously?

Post by afanofosc »

The VM in the standard and enhanced firmwares requires that a function call consist of the function name followed by that function's return address variable.

subcall func, func_return_address

In the called function you return to the caller via the subret opcode which requires the function's return address variable.

subret func_return_address

All variables in the VM are globals. These two opcodes make it impossible to safely reenter a function while it is already executing on another thread. There is also no such thing as a call stack in the firmware VM. The resource that the mutex generated by the compiler when you use safecall is protecting is the function's global return address variable.

Fortunately, you can make your functions and the ones they call inline and not worry about the resulting code size or function call safety. Or you can write your functions in a way that works well with safecall (i.e., keep the long running loops outside the shared functions).

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Running same algorithm simultaneously?

Post by HaWe »

that's what I wanted to express:
I always use "inline" if I have functions (e.g. math functions: max, min, round,...) which might be accessed by different threads at the "same" time and which don't block other threads while being executed by 1 specific thread.
Post Reply

Who is online

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