简体   繁体   English

在C中使用pthread?

[英]using pthread in C?

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. 然后在每次(10-15分钟)后,需要从其他传感器获取土壤湿度读数。 I have never used POSIX pthreads before. 我之前从未使用POSIX pthreads。 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);
}

It is not clear why you create a new thread every 10 seconds rather than just letting the original continue. 目前尚不清楚为什么每隔10秒创建一个新线程而不是让原始版本继续。 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. 不要每10秒创建一个新线程。 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. 通过在soilMoisture()函数中循环来保持线程运行,并取消funct() - 或者至少调用pthread_create()
  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 . pthread_attr_t不为NULL时,您需要使用概述和链接的函数创建非默认的pthread_attr_t

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. 如果你没有任何这样的同步,你最好使用两个独立的程序 - “每个程序完成一项工作,但做得好”的Unix口头禅仍然适用。 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. 我假设问题中提到的10-15分钟与代码中的10秒之间的差异严格用于实际测试而不是对POSIX sleep()函数的误解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM