electroRF
Member
Hi,
I got 20 threads, which only 6 of them can be running at the same time.
Thread #1 has the lowest priority, Thread #20 has the highest priority (i.e. Thread #3 can't start if Thread #5 has started and not finished yet).
For example,
Each thread should have a stack of N Bytes.
Instead of dedicating a stack for each thread, i.e. 20 stacks, total of 20*N Bytes,
I'd like to dedicate a single global array for the stacks, of size 6 * N Bytes.
How would you handle the stacks allocation?
I thought of initializing an auxiliary Array with 6 Pointers, meaning:
Now, each time a thread starts / ends:
I'd love hearing your opinion
Thank you very much.
I got 20 threads, which only 6 of them can be running at the same time.
Thread #1 has the lowest priority, Thread #20 has the highest priority (i.e. Thread #3 can't start if Thread #5 has started and not finished yet).
For example,
Code:
Thread #1 Start -> Thread #6 Start-> Thread #12 Start-> Thread #14 Start -> Thread #17 Start -> Thread #19 Start ->
-> Thread #19 End-> Thread #17 End-> Thread #14 End-> Thread #12 End-> Thread #6 End-> Thread #1 End
Each thread should have a stack of N Bytes.
Instead of dedicating a stack for each thread, i.e. 20 stacks, total of 20*N Bytes,
I'd like to dedicate a single global array for the stacks, of size 6 * N Bytes.
How would you handle the stacks allocation?
I thought of initializing an auxiliary Array with 6 Pointers, meaning:
C:
BYTE GlobalStackArry[6*N];
BYTE* StackPointers[6] =
{
&GlobalStackArray[0],
&GlobalStackArray[N],
&GlobalStackArray[2*N],
&GlobalStackArray[3*N],
&GlobalStackArray[4*N],
&GlobalStackArray[5*N]
}
INT8 StackIndex = 0; //Points on the next available Stack, runs from <0-5> inclusive.
Now, each time a thread starts / ends:
C:
//Thread Starts
SP = StackPointers[StackIndex];
StackIndex++;
if (StackIndex > 5)
ErrorFunc();
//Thread Ends
StackIndex--;
if (StackIndex < 0)
ErrorFunc();
I'd love hearing your opinion
Thank you very much.