I have a problem where two threads could be inter-locking in Windows API
I have a io_service running in a separate thread, reading data from tcp and posting the data to the window listview (calling SendMessage(WM_SETITEM)), which send the message back to the message queue running under the main thread.
However this main thread also supports manual actions such as clicking a button, which access the data stored in the listview (which is stored internally in a map<string,map<string,string>>). There must be some locking between tcp receiving data and accessing data, else there could be memory access issues with race condition.
The catch is, if I use mutex locking at
(1) tcp receiving and process message
(2) user hits a button
Now if tcp is busy processing and posting message, then the main thread running (2) is blocked. However posting message requires the main thread to run, which results in a deadlock.
I need (2) to return an actual value regardless of locking, though it could wait a little bit (0.1 sec?). Is there a way to get around this?