Software > Software

ideas for multithreading using c

(1/2) > >>

paulstreats:
being used to using java quite a bit just lately, you get used to being able to setup more than 1 programming thread ie.
this is almost like having 2 totally seperate programs running at the same time but being able to interact with each other.

Having thought about how multi threading is implemented, I assume that on a basic level that during compilation, the threads are put into seperate functions or subroutines and then the single program thread calls a line from one function then a line from the other funtion then the second line from the first function, a bit like this:-


--- Code: ---//psuedo code

void thread1(void)
{
        printf("thread1 1st line")
        printf("thread1 2nd line")
}

void thread2(void)
{
        printf("thread2 1st line")
        printf("thread2 2nd line")
}

void main(void)
{
        int lines = 1;

        while()
        {
                thread1(lines)
                thread2(lines)
                [color=red]lines[/color]++;
        }
}


--- End code ---
 

This code would produce the following output:-

thread1 1st line
thread2 1st line
thread1 2nd line
thread2 2nd line

P.s. i understand that by using a system like this would reduce the running speed of the individual programs but in some applications being able to run more than 1 program on an mcu would be a big enough advantage to sacrifice some speed.

The part i am having trouble with is how to run the functions 1 line at a time, instead of running through the whole function, I do have a sort of idea but it means incrementing another int and using "if's" to compare which would just slow everything down too much.

If anybody can tell me how to overcome this or another way of running more than 1 program at once i would appreciate it if you can let me know.

I know that you can get javalin and oopics etc.. but i would prefer not use these if i can help it.

cybcode:
Multithreading is generally handled by the OS, not the parent program. Multithreading can be easily used in C by calling special functions that start new threads. But I'm not sure multithreading can be done in all microcontrollers. What OS are you using? What CPU? Is it a microcontroller? What are you trying to do? Maybe there's a way to do it without multithreading.

dunk:
[cybcode posted before me so appologies for overlap.]

hmm,
it is a more complicated than it at first appears.
microcontrollers can indeed be forced to jump to code sections out of sequence.
the problem is that you then end up with the wrong information in the control registers.
you have to do some work saving the control registers state before you leave thread1 and loading the control registers state relevant to thread2.

you essentially want a scheduler similar to what you would find in a RTOS (Real Time Operating System).

rather than try to explain a complex subject i'm going to refer you on to some documentation that explains it better than i can:
http://www.freertos.org/implementation/index.html

dunk.

paulstreats:

--- Quote ---Multithreading is generally handled by the OS, not the parent program

--- End quote ---
I understand this part easy enough as the os has to control its own threads plus start a new thread for programs that are run, I Thought the example was a bit more illustrative to what i wanted


--- Quote ---Multithreading can be easily used in C by calling special functions that start new threads.

--- End quote ---

I didnt know C itself had this functionality (i thought it came in with c+) I'll have to investigate further ;D


--- Quote ---What OS are you using? What CPU? Is it a microcontroller? What are you trying to do? Maybe there's a way to do it without multithreading.

--- End quote ---

I am using microcontrollers, i have a few different pic's and avr's to choose from. Basically what i am doing is trying to keep evereything modular based using a small 8 pin pic to control motor h bridges - an avr to control an ir range finder, the scanning servo its mounted on and a sonar range finder, Ive also had to use a pic to control an lcd screen as the one im using is not serial or i2c compatable and needs either 4bit or 8bit interface, so this pic is acting as an i2c slave device and then relays the data to the lcd in 4 bit mode.

 These are all controlled by a central processing pic16f877a(for now) also the pic with the lcd i have given i2c functions that allow its excess ram to be by the central processing pic for extra datastorage.

 I am soon to be installing another ir rangefinder and servo onto the avr so there would be 2 of each. I know i can just code these in as normal but i would rather try to use multithreading type abilities (just to see if they work) since i am designing an ai architecture thats running on my simulator that needs multithreading, this will eventually be added as an advanced daughter board.

paulstreats:

--- Quote ---
you essentially want a scheduler similar to what you would find in a RTOS (Real Time Operating System).

rather than try to explain a complex subject i'm going to refer you on to some documentation that explains it better than i can:
http://www.freertos.org/implementation/index.html


--- End quote ---

I think this may take a bit of studying before i get to use threading :( It doesnt seem easily controllable with a higher level language on an mcu

Navigation

[0] Message Index

[#] Next page

Go to full version