Main Site Documentation

Multi-threading vs optimisation?


#1

Hi,

I am at a decision point in an application, and was wondering if anyone had experience in the options available to me?

I have a program transmitting CAN on 2 channels very reliably. However, when I change certain data in the CAN frames, I also update a 16x2 display via I2C. This is done synchronously in the same thread, and takes typically 150ms or so, suspending CAN transmission during this time. As I have frame periods of 40ms and up, this obviously plays havoc with my accuracy!

I tried moving the CAN transmission loop to a separate thread using simple threading such as “new Thread(CANSendingLoop).Start();”. But this slowed down the CAN sending thread (adding 20ms or so to all frame periods), and the display update happened at 1 character a second!

I am new to threading, but is there a large overhead with implementing threading which will cause slowdown like this in a CPU-intensive scenario?

My other option is to optimise the (seemingly-slow) display update (only update those parts of the display that I need to each time), minimising the temporary delay to the main loop…

Thanks in advance for any thoughts on the best way to head that anyone has!

Nick


#2

Ooops! A key bit of info I forgot to add!

I am using a Fez Panda II.

Thanks


#3

what display are you using?
In general you will want to remember that you have two challenges on the Panda related to communications.
#1 - this isn’t a real time system, as such there is not a predictable response time for operations.
#2 - There is only one processing core. So spawning threads adds overhead and no performance improvement.

If possible share your code (throw it up in a public repository?). Focus on optimization. Clock speed is pretty fast on the Panda, so using an communications loop could be a possibility. For your I2C communications, is it buffered? I’m only familiar with serial communication on the Panda and it is buffered which helps with any intermittent processing latency. You may need to buffer your communication with your display so you don’t service all characters in one communications loop.

I developed a very basic example of communication between two panda’s using serial communication and working with a serial display board. It was able to run very fast with no garbage collection required by the .net runtime.
Thread where it was discussed
https://www.ghielectronics.com/community/forum/topic?id=4034&page=1
Code is here
https://www.ghielectronics.com/community/codeshare/entry/303

I never got back to this project, but perhaps it can give you some ideas.