Solved

Multi-threading Question?

Posted on 2000-04-21
4
224 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 Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
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…

863 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

22 Experts available now in Live!

Get 1:1 Help Now