Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[mosquitto-dev] Can one process have multiple user threads, each with its own mosquitto client connection and own loop_start()-created network IO thread?
  • From: "Costescu, Nick P Collins" <Nicolae.Costescu@xxxxxxxxxxx>
  • Date: Tue, 23 Apr 2024 02:35:40 +0000
  • Accept-language: en-US
  • Arc-authentication-results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=collins.com; dmarc=pass action=none header.from=collins.com; dkim=pass header.d=collins.com; arc=none
  • Arc-message-signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector5401; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=KMJHLwXJG+95qjVS/q4bbk2O8rxy458IB5JoYFUVNCU=; b=ubyKz+LI+wbjQA5sSrYpC/QXg+u9M7QTzljwncWbYbycpLvuBV7JKARI14bV56nlZuHr+mpczx6apRVqkqDJyzTMJop26/NsBP7NebnzbNhbVBr+hnb5WP5RRJ3hQleFfoXBhg23iL2zPXpTU43LB61ObKzRO6WPAjB1axlSvGrC5+gIgWBAb6N+S0+wTQATbpz9tgg0/MgDN4BhrcVtRXI2iAAX2iE1S98sUEO10MHqetfZQz8Zhnl7kpIUjdRRX8WS0oy3E/HFzXUyORvShRtajMsNu2IIv5u9DqkzG7sR3h5nGYPdj8mFfdJo4k2ukEfPzsy1iVEn1sZWejK0Ew==
  • Arc-seal: i=1; a=rsa-sha256; s=arcselector5401; d=microsoft.com; cv=none; b=US3+5C4Jm8edwmu38G+EJfLrTgGSNyOvY1Pbtx8KnY4pgJ5nYesmZrpboCQ5Krfn8TfKW7jTvNEfYwMKckhF3kQczEK9rPJ51kQ7y+8JXBq2WGuca0CzhW2RGtZl+5B0oCKTVbVDB3I2e3Bbr67sO0ZOSnviUyEPkTGViT6YF7K4kPKEDQyhSIE7Q89rBUI2Tiau5eWlb5E5QiML0gd7ZtiB38NIyPVLJ4MhyPihcSKJbV7E0aMBgUr8ew6TLG3wsHaQiOby/hWNQb2uuos2QByH3ZZokFJN5WdLZCDDwOU7E8Ptst1TeDlDToEfHOuo5xjt+rfwe5Q2Ht16vZVQhA==
  • Delivered-to: mosquitto-dev@xxxxxxxxxxx
  • List-archive: <https://www.eclipse.org/mailman/private/mosquitto-dev/>
  • List-help: <mailto:mosquitto-dev-request@eclipse.org?subject=help>
  • List-subscribe: <https://www.eclipse.org/mailman/listinfo/mosquitto-dev>, <mailto:mosquitto-dev-request@eclipse.org?subject=subscribe>
  • List-unsubscribe: <https://www.eclipse.org/mailman/options/mosquitto-dev>, <mailto:mosquitto-dev-request@eclipse.org?subject=unsubscribe>
  • Thread-index: AdqVJrbGXzkZgI0kSqCwT15XJBnzvg==
  • Thread-topic: Can one process have multiple user threads, each with its own mosquitto client connection and own loop_start()-created network IO thread?

Hi Everyone,

 

Thank you for creating mosquitto, I’ve really enjoyed using it.

 

We’ve built the threaded version of 2.0.18 x64 for Windows with pthreads 2.9.1. Seems to work fine with loop_start()/loop_stop() for the most part, but I get some unexpected fail to publish errors, so I would very much appreciate a sanity check of my assumptions (including order of operations).

 

Each instance of my MqttClient class has its own struct mosquitto * which it allocates and uses. This is the order of operations:

  • oneTimeInit() (see below – call mosquitto_lib_init() only if this is the first MqttClient instance (thread) to call this). Increment a ref count which tracks the # of times this function has been called.
  • mosquitto_new() & set options (use v5, disable Nagle’s)
  • set callbacks
  • connect
  • loop_start()
  • do work until we’re done
  • disconnect
  • loop_stop()
  • oneTimeShutdown() (see below – call mosquitto_lib_cleanup() only if this is the last MqttClient instance (thread) to call this. Determine by seeing if the ref count set above is down to 0)

 

I don’t use mosquitto_threaded_set() because I’m using loop_start().

 

I’m using 2 of these MqttClient objects and I’ve verified that each has its own separate network IO thread started by loop_start() by printing the thread ID from the on message callback.

 

My questions are:

  1. Is this order of operations correct?
  2. Is libmosquitto designed so that you can have multiple threads, each with their own client connection (own struct mosquito* independently created and connected and own loop_start()) in one process?  
  3. What is the correct way to do the one-time library init and cleanup in this situation?

Thanks!

Nick

 

Here are my oneTimeInit() and oneTimeShutdown() functions, for reference:

 

void MqttClient::oneTimeInit()

{

   lock_guard<Lock> m(s_libraryLock);

   if (s_libraryInitialized == 0)

   {

      if (mosquitto_lib_init() != MOSQ_ERR_SUCCESS)

      {

         throw runtime_error("mosquitto_lib_init failed.");

      }

   }

   s_libraryInitialized++;

}

 

void MqttClient::oneTimeShutdown()

{

   lock_guard<Lock> m(s_libraryLock);

   s_libraryInitialized--;

   if (s_libraryInitialized == 0)

   {

      mosquitto_lib_cleanup();

   }

}

----------

Nicolae P. Costescu, Ph.D. | Pr. Software Engineer | Mission Systems

COLLINS AEROSPACE

22640 Davis Dr., MS 291-100, Sterling, VA 20164 U.S.A

Tel: +1 703 880 5648

nicolae.costescu@xxxxxxxxxxx | collinsaerospace.com

 


Back to the top