Solved

Multi-threading Question?

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
This article describes how to programmatically preset the "Pages per Sheet" option that's available with most printer drivers.   This setting lets you do "n-Up" printing, where two, four, or more pages are printed on each sheet of paper. If your …
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now