7 years, 6 months ago.

Can we pass two objects from rtos thread?

I want to pass two objects from thread. For example

thread_send.start(&client1, send_message);

Here, client1 is the object that i want to pass to function send_message. If i want to pass another object client2 to function send_message, How can i achieve this?

1 Answer

7 years, 6 months ago.

Assuming you are using mbed-os, the thread.start syntax has been deprecated to use a Callback object as its parameter. The callback can have the structure:

callback(static function, arg)

or

callback(object, function)

So, your listed example would look like:

thread_send.start(callback(send_message, &client1));

Unfortunately, you cannot pass another parameter. Maybe you could make a struct that stores two objects, then you could make that the argument to send_message.

Hi Sarah,

I have a worst case scenario. I am using MQTT protocol. I need to send two objects, one is client and another is ipstack in thread but the client needs to be initialized, it cannot just be declared.(or i need to change the whole library which is a painstaking task). Also, the initialization of the client is only possible after the cellular connection is established which needs to be done first so defining structure globally wont work for me.

Also could you please tell me what causes hard fault error?

posted by Amod Amatya 01 Jun 2017

What hard fault? It's hard to say without seeing the code.

You would declare the struct. Then, after the connection is established, store the objects in the struct. Then, you would start the thread execution passing the struct to the method.

Structs are kind of like classes, you don't have to store anything in them when you declare them - http://www.cplusplus.com/doc/tutorial/structures/.

posted by Sarah Marsh 01 Jun 2017

MQTT::Client<MQTTSocket, Countdown> client1 = MQTT::Client<MQTTSocket, Countdown>(ipstack);

Here client1 is an object of class Client of namespace MQTT. What I am saying is, the object needs to be initialized like I have done above, which needs to be done only after a cellular connection is established. I would have absolutely no problem if I could just declare it. Whenever i just declare it without initializing it in the same line, I get the error like

Quote:

no matching function for call to 'MQTT::Client<MQTTSocket, Countdown>::Client()

posted by Amod Amatya 05 Jun 2017

Hi Amod,

Unfortunately C++ gets a bit messy when classes don't have a default constructor. In this case you can create a class with a constructor that will call the constructor of it's members (I'm going to guess a bit on the variable names):

Using a class to pass multiple arguments

// Declare the class
class MessageThreadArgs {
public:
    Thing1 client1;
    Thing2 client2;
    MessageThreadArgs(NetworkStack *stack, int somethingelse)
        _client1(stack), _client2(somethingelse) {
    }
};

// Instantiate the class
MessageThreadArgs args(stack, 42);

// Start the thread
thread_send.start(callback(send_message, &args));

The above is probably your best bet for passing multiple arguments with constructors. There may be an alternative with placement new, but it will probably be even more ugly (and hacky).

Let us know if this doesn't work for you

posted by Christopher Haster 05 Jun 2017

Hi Christopher, Thanks for the reply, I had managed to solve the issue by setting flag values.

I have a new problem now, I need to disconnect from TCP to get the GPS value in MTS Dragonfly (https://developer.mbed.org/platforms/MTS-Dragonfly/) as both utilize the same socket. My program runs smoothly even after disconnecting and reconnecting MQTT when used without SSL. But whenever I implemement SSL in my code, I get the error as Hard fault when reconnecting TCP.

MQTTSocket ipstack;
ipstack.connect(config.hostname, config.port, ssl_certificates, 5000)

Here, If I pass a third argument as NULL and connect with a broker which does not require SSL, It works smoothly, but as I need to connect to Azure, SSL is required which gives me hard fault when it tries to reconnect. Note - On the first ipstack.connect it works fine. I have asked a new question about this issue here https://developer.mbed.org/questions/78213/Hard-Fault-when-reconnecting-with-SSL/

posted by Amod Amatya 06 Jun 2017