|
-
July 22nd, 2010, 07:58 AM
#1
Mutex used in Thread calling recv (TCP connection)
Hi,
I was wondering if someone could help me figure this out as I am quite stuck.
I have threads called NetworkConnection that connect to servers and constantly receive data via the recv function. Once they receive a message, they store it in their buffers. There's another thread called NetworkController which manages all the NetworkConnection threads and processes messages whenever there's something in their buffers.
I would like to have the NetworkController thread use WaitForMultipleObjects where I pass an array of all the Mutex handles of the NetworkConnection threads. NetworkConnection threads should only release their Mutex when there's something in their buffer.
My problem is that once a NetworkConnection thread is in the recv funtion and the Mutex is owned by NetworkController because there's still messages in the buffer, once the NetworkController processes all messages in the buffer, NetworkConnection won't be able to lock the Mutex because it's stuck in the recv function until there's new data. As a result, the NetworkController will end up constantly polling the buffer while there's no more data to process.
If a Mutex could be used in a way where it doesn't matter which thread locks it or releases it, it would solve my problem (ie NetworkController locks it whenever there's no more data in the buffer and NetworkConnection releases it whenever there's new data in the buffer), however, as I understand that's not possible. A thread should only lock a Mutex once and always be the one to release it.
Does any of you have any ideas how I can go around this problem?
Many thanks,
Alex
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|