I am working on a embedded system. the modules work fine in one thread but as I decided to migrate to multithread in esp32 I face deadlock. Any idea or help to make the neckbottles clear and how to debug these problems ?
If you are able to use a debugger to determine where each thread is running when it gets stuck, then that will tell you where the problem is.
Thanks, According ro my debugs I found out that I lock a struct for reading and also writing. maybe splitting the read and write part fix my problem. I checked tokio streamer and found out that read and write parts are different. Do you have any suggestion ?
Trying to lock the same lock twice on a single thread will indeed result in a deadlock. How you can avoid this will depend on what operations you can make on the IO resource. Splitting reader and writer can be one solution.
What are other options ? I am looking for all solutions to choose the best.
Well, if you can read/write from it with
&self access, then you don't need a mutex at all.
Another option is to release the write lock before reading, and vice-versa. Or you could use a re-entrant lock.
How does re-enterant lock work ? I did not found any good resources in google.
It can be locked multiple times from a single thread. (But only from one thread at the time.)
That is indeed a diagram. Do you have any questions related to it?
Sorry, I forgot to share my question. As I said I am looking for all solutions and also some examples if exist. Here is What I found out and also you said. Any other solution or best practice will be appreciated.
- put data in buffer and return to open the lock. ( the code get messy, May be a best practice is needed)
- use channels
- split read and write parts
- re-entrant lock (I don't have much information, I would be glad if U share your knowledge)
- use async libraries to make sure locks wait ( not sure)
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.