Solved

Multi-threading Question?

Posted on 2000-04-21
4
225 Views
Last Modified: 2013-12-03
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
Comment
Question by:areams
  • 2
  • 2
4 Comments
 
LVL 3

Accepted Solution

by:
ufolk123 earned 100 total points
ID: 2739498
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
 
LVL 3

Expert Comment

by:ufolk123
ID: 2881959
are you there ?
0
 

Author Comment

by:areams
ID: 2931529
Answer accepted
0
 

Author Comment

by:areams
ID: 2931530
errrrr.... sorry it took me so long to respond.  I took a little hiatus to another country.  Thanks for the help!
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question