LONGJA
Posts: 23
Joined: Wed Mar 28, 2018 4:04 pm

Running multiple functions at the same time

Sun Apr 08, 2018 10:49 am

Hello,

I'm building a data logging system (in C) that sends data from a series of sensors via GSM but also has a 16x2 LCD screen. The data logging program loops every minute and but I would like the LCD program to loop much faster so it can respond quickly to changes. For example, if an extra device was plugged into the load, you would see the information on the screen react immediately, rather than having to wait a minute for the rest of the program to loop. I've read about a number of ways to run several programs at the same time including pipe, fork and pthread. pthread appears to be the one favoured by most but I wanted to check that it was still suitable for an application of this kind.

So my question is, is pthread a suitable way to achieve the outcome mentioned above? If not, perhaps you could suggest an alternative to look into.

Any thoughts greatly appreciated, thanks.

ghans
Posts: 7882
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: Running multiple functions at the same time

Sun Apr 08, 2018 11:09 am

You want to write a multithreaded program. So yes, you need to use pthreads.

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

User avatar
PeterO
Posts: 5717
Joined: Sun Jul 22, 2012 4:14 pm

Re: Running multiple functions at the same time

Sun Apr 08, 2018 11:44 am

LONGJA wrote:
Sun Apr 08, 2018 10:49 am
Hello,

I'm building a data logging system (in C) that sends data from a series of sensors via GSM but also has a 16x2 LCD screen. The data logging program loops every minute and but I would like the LCD program to loop much faster so it can respond quickly to changes. For example, if an extra device was plugged into the load, you would see the information on the screen react immediately, rather than having to wait a minute for the rest of the program to loop. I've read about a number of ways to run several programs at the same time including pipe, fork and pthread. pthread appears to be the one favoured by most but I wanted to check that it was still suitable for an application of this kind.

So my question is, is pthread a suitable way to achieve the outcome mentioned above? If not, perhaps you could suggest an alternative to look into.

Any thoughts greatly appreciated, thanks.
While threading is a possibility, it sounds like overkill for your application. If you want the the LCD to respond say every ten seconds, and the GSM once a minute then...

Code: Select all


forever
	do six times
		wait 10 seconds
		do LCD code
		
	do GSM code 	

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

LONGJA
Posts: 23
Joined: Wed Mar 28, 2018 4:04 pm

Re: Running multiple functions at the same time

Mon Apr 09, 2018 8:45 am

PeterO - I assume the 'do' function here will run both simultaneously? Both programs, LCD and GSM query an AtD at some point, will this be a problem if the two try to query the AtD at the same time?

jahboater
Posts: 5421
Joined: Wed Feb 04, 2015 6:38 pm
Location: West Dorset

Re: Running multiple functions at the same time

Mon Apr 09, 2018 9:07 am

LONGJA wrote:
Mon Apr 09, 2018 8:45 am
PeterO - I assume the 'do' function here will run both simultaneously? Both programs, LCD and GSM query an AtD at some point, will this be a problem if the two try to query the AtD at the same time?
That is pseudo code ...
In a C like syntax ...

Code: Select all

for(;;)
{
   for( int i = 0; i < 6; ++i )
   {
      sleep(10);
      LCD_code(); 
   }		
   GSM_code();
}

User avatar
PeterO
Posts: 5717
Joined: Sun Jul 22, 2012 4:14 pm

Re: Running multiple functions at the same time

Mon Apr 09, 2018 9:13 am

jahboater wrote:
Mon Apr 09, 2018 9:07 am
In a C like syntax ...

Code: Select all

for(;;)
{
   for( int i = 0; i < 6; ++i )
   {
      sleep(10);
      LCD_code(); 
   }		
   GSM_code();
}
Exactly...

The point is you don't have to actually run things together or in parallel for them to appear to be running together or in parallel.

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Return to “C/C++”