Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 232
  • Last Modified:

Multi-threading Question?

Hello,
This is probably an easy question for all the thread masters out there, but I'm not sure of the best way to implement this being a thread newbie.

1) Basically, I have a master thread that receives TCPIP messages from a socket.  
2) Then I also have several client threads that are blocked waiting for an event(WaitForSingleObject).
3) When the master thread receives a complete message, it stores the message in a global buffer and fires an event. (PulseEvent).
4) This releases each of the client threads and they read the global buffer and store/process it locally.

The problem is that sometimes I get a burst of TCPIP messages faster than the client threads can process each message.  This results in messages getting dropped.  

So what I would like to do is block the master thread until all of the client threads have completed each message.  

Does anybody have any suggestions as to what the best implementation is to accomplish this while avoiding race conditions, deadlock, etc?  Is it possible to use a semaphore that only signals when a certain number is reached?

TIA everyone!
0
areams
Asked:
areams
  • 2
  • 2
1 Solution
 
ufolk123Commented:
Instead of using a semaphore you can use the following:

1) a Critical section.
2) an automatic event say notification.


Now it will work like this.Keep a global variable showing the number of entries in the global buffer.
Now when the master thread get a message do the following:

Enter critical section :
Wait on notification event with 0 timeout ( initially set).
if wait successful then
   globalnum+=num of new entries
   Leave CS
else
   Leave CS
   Wait on notification event
   Enter CS
   globalnum+=num of new entries
   Leave CS
endif

Each thread will do the following:
After processing a single entry
Enter CS
globalnum--;
if globalnum==0 Set(notifcation event)
Leave CS


I think this should solve your problem.
Get back for any issues.

0
 
ufolk123Commented:
are you there ?
0
 
areamsAuthor Commented:
Answer accepted
0
 
areamsAuthor Commented:
errrrr.... sorry it took me so long to respond.  I took a little hiatus to another country.  Thanks for the help!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now