Queue dosent remove items in the list after try_get

I have a setup of
1-thread to reply,
2-Thread to receive,
3-main thread that treat the message.
2 queue (queueRX, queueTX)
Now, up to 64 (which is the size of the queue) the communications works. After getting more than 64 msg, the queue becomes full and the communications constantly fails.
I am sure that I take out the messages in the main thread, using (try_get) but stills the queueRX is full.
It seems that the pointers to std::string remains in the queueRX even if i take them using try_get and I treat the messages I have.
I even delete the memory and put NULL to the variable just to make it sure that we don’t have anything left. But still the queue is full.
This must be a bug in the Mbed OS.

in the main : I have


	if (queueRx.try_get(&cmd)) {
			std::string command(*cmd);
			delete cmd;
			cmd = NULL;
....}

in the thread that fails I have this part

					std::cout << "msg was =" << rxBuf << std::endl;
					std::string* str = new std::string(rxBuf);
					while (queueRx.try_put(str) != true) {
						//This might be bad, it should wait
						// to the time it can put the data to the queue
						ThisThread::sleep_for(1ms);
						if (queueRx.full()) {
                            printf("queueRX size%i\n",queueRx.count());
							printf("I am full\n");
							ThisThread::sleep_for(10ms);
						}
					}

I get in the console after 64 messages (64 and I am full)
Any suggestion please?
BR
Mariwan

Hello,

try to get size of queue via count method before and after try_get method. When the queue is not cleaned after try_get, then the size must be same or your read method have to return still one same msg over and over.

Maybe more context about the communication could be handy. Because according to this it seems your read method is too much slow and your write method is too fast.

BR, Jan

HI
Strange,
I checked that. It seems that Main thread which starts the other threads are not running. I have sleep in both other threads, but still main is not working after first run.
If it is crashing, it should crash both other threads.
But it seems that it doesn’t run at all.

	while (1) {
		results.clear();
		if (queueRx.full()) {
			printf("I have data - full\n");
		    }
        
        if(queueRx.count()>0){
            printf("queueRx was =%i\n",queueRx.count());
            if (queueRx.try_get(&cmd)) {
                printf("queueRx after was =%i\n",queueRx.count());
                std::string command(*cmd);
                delete cmd;
                cmd = NULL;
                val = convertToEnum(command);
                std::cout << "Value is = " << std::to_string(val) << " cmd=" << command << "\n";
                switch (val) {
 
                    case TEST_24V: {
                        DUTtest->Test24V();
                    }break;
                    case TEST_12V: {
                        DUTtest->Test12V();
                    }break;
                    ...
                    case TEST_END: {
                        DUTtest->TestEnd();
                    }break;
                }
                if (DUTtest->partialResults.size()>0){
                    results = DUTtest->partialResults;
                    if (results.size() > 0) {
                        //We have results to send.
                        std::string* tempStr = new std::string(results);
                        queueTx.try_put(tempStr);
                    }
                }
		    }
        }
		ThisThread::sleep_for(10ms);
	}

	//Just in case, we go out from while(1)
	ThisThread::sleep_for(10ms);
	clientSocket->close();
	clientSocket = NULL;
	printf("Close-main thread\n");

BR/Mariwan

HI again,
I know what was happening now.
In the main thread, I test different peripherals, some digital and 5 UART:s
This function which should send a message to the UART was not getting the job done as there is no devices connected yet to the UART:s.

#define SLEEP_FOR(x)  rtos::ThisThread::sleep_for(chrono::milliseconds(x));
...
bool func(){
testTarget->write(testName.c_str(), testName.size());
	SLEEP_FOR(30);   //Sleep for
	char buf[512];
	testTarget->read(&buf, 511);
	std::string result = buf;
	...
	return false;
}

But it was never getting out of this function and was blocking main thread to run.
by putting return false in the beginning of the function, the failure is not happening.
here is the link for the blocking parameter

https://os.mbed.com/docs/mbed-os/v6.16/mbed-os-api-doxy/classmbed_1_1_file_handle.html#aadb624fc379dbf725a2cb132cc37929f

UART Blocking /unblocking should be used I guess.

Thanks.
BR /Mariwan