1

I am trying to write a program that will continuously take reading from a sensor that will monitor water level. Then after every (10-15 mins) it will need to take soil moisture readings from other sensors. I have never used POSIX pthreads before. This is what I have so far as a concept of how it may work.

It seems to be working the way I want it to, but is it a correct way to implement this. Is there anything else I need to do?

void *soilMoisture(void *vargp)
{
 sleep(10);
 funct();
 return NULL;
}

int main()
{
 pthread_t pt;
 int k=1;
 pthread_create(&pt, NULL, soilMoisture, NULL);
 while(k>0)
 {
   printf("This is the main thread (Measuring the water level) : %d\n", k);
   sleep(1);
 }
 return 0;
}

void funct()
{
 printf("******(Measuring soil moisture after sleeping for   10SEC)***********\n");
 pthread_t ptk;
 pthread_create(&ptk, NULL, soilMoisture, NULL);
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
hamadkh
  • 359
  • 1
  • 16

1 Answers1

3

It is not clear why you create a new thread every 10 seconds rather than just letting the original continue. Since the original thread exits, you aren't directly accumulating threads, but you aren't waiting for any of them, so there are some resources unreleased. You also aren't error checking, so you won't know when anything does go wrong; monitoring will simply stop.

You will eventually run out of space, one way or another. You have a couple of options.

  1. Don't create a new thread every 10 seconds. Leave the thread running by making a loop in the soilMoisture() function and do away with funct() — or at least the pthread_create() call in it.
  2. If you must create new threads, make them detached. You'll need to create a non-default pthread_attr_t using the functions outlined and linked to in When pthread_attr_t is not NULL.

There are a myriad issues you've not yet dealt with, notably synchronization between the two threads. If you don't have any such synchronization, you'd be better off with two separate programs — the Unix mantra of "each program does one job but does it well" still applies. You'd have one program to do the soil moisture reading, and the other to do the water level reading. You'll need to decide whether data is stored in a database or otherwise logged, and for how log such data is kept. You'll need to think about rotating logs. What should happen if sensors go off-line? How can you restart threads or processes? How can you detect when threads or processes lock up unexpectedly or exit unexpectedly? Etc.

I assume the discrepancy between 10-15 minutes mentioned in the question and 10 seconds in the code is strictly for practical testing rather than a misunderstanding of the POSIX sleep() function.

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • thanks, I did remove the pthread_create() and added a loop in the soilMoisture(). I will eventually need to communicate between two threads so thats why I dont want to write a separate c program. Also you brought up other really good points that I will keep in mind. And yes the this is just for testing purposes I am aware of how POSIX sleep() funtions work. – hamadkh Mar 31 '17 at 14:19