Solved

Multi-threading Question?

Posted on 2000-04-21
4
226 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

827 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