MQTT: Stop publish thread properly on PLC stop
authorEdouard Tisserant <edouard@beremiz.fr>
Thu, 18 Jul 2024 12:05:26 +0200
changeset 3993 78f9abfb32a0
parent 3992 056657cd1484
child 3994 c399fe412dbd
MQTT: Stop publish thread properly on PLC stop
mqtt/mqtt_client_gen.py
--- a/mqtt/mqtt_client_gen.py	Thu Jul 18 11:42:08 2024 +0200
+++ b/mqtt/mqtt_client_gen.py	Thu Jul 18 12:05:26 2024 +0200
@@ -393,7 +393,14 @@
 {{
     int rc;
 
-    /* TODO stop publish thread */
+    /* stop publish thread */
+    MQTT_stop_thread = 1;
+    if (pthread_mutex_trylock(&MQTT_mutex) == 0){{
+        /* unblock publish thread so that it can stop normally */
+        pthread_cond_signal(&MQTT_new_data);
+        pthread_mutex_unlock(&MQTT_mutex);
+    }}
+    pthread_join(publishThread, NULL);
 
 #ifdef USE_MQTT_5
     if (rc = MQTTClient_disconnect5(client, 5000, MQTTREASONCODE_SUCCESS, NULL) != MQTTCLIENT_SUCCESS)
@@ -539,6 +546,8 @@
         }}
 
         pthread_mutex_unlock(&MQTT_mutex);
+
+        if(MQTT_stop_thread) break;
     }}
 
     if(!MQTT_stop_thread){{