Page 1 of 1

Organization of C programs in the context of threads.

Posted: Wed May 03, 2023 1:19 am
by Alexanders
In the folder C Programs there are many examples with functions for (;;) and Delay_sec() and others, excluding the correct sequential execution of other functions. There are only 7 threads allocated for the user.
Please explain and give examples of simultaneous use of different C programs in the same stream in real time and without delay.
I also ask you to clarify in detail the operation and scope of the WaitNextTimeSlice() function.

Re: Organization of C programs in the context of threads.

Posted: Wed May 03, 2023 4:36 pm
by TomKerekes
In the folder C Programs there are many examples with functions for (;;) and Delay_sec() and others, excluding the correct sequential execution of other functions. There are only 7 threads allocated for the user.
Please explain and give examples of simultaneous use of different C programs in the same stream in real time and without delay.
Sorry I don't understand the question. What are you trying to do?

I also ask you to clarify in detail the operation and scope of the WaitNextTimeSlice() function.
This waits until the beginning of the Threads next Time Slice. See here. Useful when some code should be executed at some periodic rate (multiple of 90us sample times). Or to provide execution of ~40us of code without the possibility of interruptions.

Re: Organization of C programs in the context of threads.

Posted: Wed May 10, 2023 10:27 pm
by Alexanders
I understand how the WaitNextTimeSlice() function works.
How to apply it correctly? Before the start of the desired function or inside it or after it or inside the brackets { }? And also how many lines of code does it work on?

Re: Organization of C programs in the context of threads.

Posted: Thu May 11, 2023 10:08 am
by Alexanders
And two more questions.

1. What is the difference between the Halt and FeedHoldStop functions ?

2. I want to duplicate with an external CycleStart/Halt button. I understand how to pass button commands to KMotionCNC. But I don't know how to read the status of Halt and CycleStart from KMotionCNC ?

Re: Organization of C programs in the context of threads.

Posted: Thu May 11, 2023 5:47 pm
by TomKerekes
I understand how the WaitNextTimeSlice() function works.
How to apply it correctly? Before the start of the desired function or inside it or after it or inside the brackets { }?
Like any function it can be placed anywhere where code is executed.

And also how many lines of code does it work on?
The function doesn't operate on any lines of code. It waits till the beginning of the next time slice then returns. Please read my previous response.

What is the difference between the Halt and FeedHoldStop functions ?
What is 'FeedHoldStop '? To access the Help Manuals for a description of a button click 'Help' then click the button.

I want to duplicate with an external CycleStart/Halt button. I understand how to pass button commands to KMotionCNC. But I don't know how to read the status of Halt and CycleStart from KMotionCNC ?
There is a define in KFLOP called JOB_ACTIVE which can be used to determine if GCode is executing or not.

Re: Organization of C programs in the context of threads.

Posted: Fri May 12, 2023 9:31 am
by Alexanders
The function doesn't operate on any lines of code. It waits till the beginning of the next time slice then returns.
That is, this function loops code execution inside itself until the next time slice?
In other words, in the code:

Code: Select all

main()
{  
      for(;;)
            {
              function 1();
               WaitNextTimeSlice();
              function 2();
               WaitNextTimeSlice();
              function 3();
               WaitNextTimeSlice();
              function 4();
               WaitNextTimeSlice();
            }
}
Each function will be repeated at intervals = (# User Threads + 1) * TIMEBASE * 4 ?
Do I understand correctly?

Re: Organization of C programs in the context of threads.

Posted: Fri May 12, 2023 3:36 pm
by TomKerekes
That is, this function loops code execution inside itself until the next time slice?
Yes

Each function will be repeated at intervals = (# User Threads + 1) * TIMEBASE * 4 ?
Do I understand correctly?
Yes. Assuming none of those functions take longer than ~40us to execute.

You can check these things yourself using Time_sec() which has 20ns resolution. WaitNextTimeSlice also returns the time.

Function names can not contain spaces.

When showing your code its nice to indent your code in a consistent manner to make it more readable. See here. And Video: