Page 2 of 2

Re: Running same algorithm simultaneously?

Posted: 31 Jan 2011, 22:33
by dsjove
In the code below...

Task 0 never appears to execute.
Task 1 does not wait the 4 seconds.
Program never exits.
Yes, calling the same function simultaneously is bad :twisted:

If I have any utility function then should I safecall or inline it so program doesn't go into this weird state if more than one task accesses it?

If I have a single function that needs to be run to control multiple items, should I either inline it or refactor it to handle all items in one function (essentially do my own poor man's scheduling)? Some of these functions are 60 lines of code. Inlining just seems wrong :mrgreen:

Code: Select all

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

task task0()
{
	run(0);
}

task task1()
{
	run(1);
}

task main()
{
	StartTask(task0);
	StartTask(task1);
}

Re: Running same algorithm simultaneously?

Posted: 01 Feb 2011, 02:23
by m-goldberg
afanofosc wrote: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).
Another way to think about the dichotomy of safecall and inline is that with safecall you share the function code during runtime, but with inline you share the code during compile time. To me, that makes inline rather like a preprocessor macro although it use has several advantages over preprocessor macros (e.g., the compiler checks arguments for type and local variables can be declared).

Which way is better? As usual, I would say there is no single rule -- it depends on what you are trying to accomplish and what your programming style favors.

Re: Running same algorithm simultaneously?

Posted: 01 Feb 2011, 02:51
by dsjove
Yes, inline functions are very handy. C++ inline functions, for the most part, replace the C macros for the same reason. You get the optimizations with all the function compile-time checks (type safety, etc).

If I need the function running simultaneous, I'll have to inline. safecall will serialize them.