2

Author Topic: ideas for multithreading using c  (Read 3662 times)

0 Members and 1 Guest are viewing this topic.

Offline paulstreatsTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
ideas for multithreading using c
« on: August 26, 2007, 04:52:36 PM »
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: [Select]
//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]++;
        }
}

 

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.

Offline cybcode

  • Jr. Member
  • **
  • Posts: 18
  • Helpful? 0
Re: ideas for multithreading using c
« Reply #1 on: August 26, 2007, 05:26:08 PM »
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.

Offline dunk

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 1,086
  • Helpful? 21
Re: ideas for multithreading using c
« Reply #2 on: August 26, 2007, 05:37:29 PM »
[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.

Offline paulstreatsTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: ideas for multithreading using c
« Reply #3 on: August 26, 2007, 05:49:35 PM »
Quote
Multithreading is generally handled by the OS, not the parent program
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.

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.

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.

Offline paulstreatsTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: ideas for multithreading using c
« Reply #4 on: August 26, 2007, 06:03:53 PM »
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



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

Offline JesseWelling

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 707
  • Helpful? 0
  • Only You Can Build A Robot!
Re: ideas for multithreading using c
« Reply #5 on: August 26, 2007, 07:58:15 PM »
Its actually easier than you think if you already have a background in C. If you are using Linux and avr-gcc for your build environment I could zip up my project directory, I run an ATmega128, but the directory is somewhat large as I am using FreeRTOS && Procyon's AVRlib...

With threading you are going to have to get used to concurrency... two threads can't use
the same a2d at one time, it must be protected...
« Last Edit: August 26, 2007, 07:59:54 PM by JesseWelling »

Offline paulstreatsTopic starter

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: ideas for multithreading using c
« Reply #6 on: August 27, 2007, 03:48:50 AM »
Ive looked at rtos and am probably going to use it, or at least modify it for my purposes.
I really want it to work on pics cause i have a lot of pic24's to use up(which operate at a max of 3.3v so are better used for my logic than controlling devices) , there is a ported version for these available which i am going to try.

As with concurrency, I will most likely assign io ports exclusively to individual threads, rather than worry about cross talking.

Offline Ro-Bot-X

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,431
  • Helpful? 25
  • Store: RoBotXDesigns.ca
Re: ideas for multithreading using c
« Reply #7 on: August 29, 2007, 12:55:32 AM »
You may want to take a look at AVRx, a Real Time Multitasking Kernel for AVR micros, by Larry Barello.
Here is the link:
http://www.barello.net/avrx/index.htm
Check out the uBotino robot controller!

 


Get Your Ad Here

data_list