Threading Question in Visual C

Hello Experts,

First, let me say that I am very new at this.  I have some code that someone else wrote (not sure I can post it).  It purpose is to measure performance on multiple hard disk drives attached to a PCI RAID controller.  The functions do work to control each drive, but measuring performance is driving me crazy.  They are using the Query Performance function and are launching the HDD access functions from a front end program so that each thread is accessing the same function in the back end (dll).  What this is doing is causing all the performance measurements to be exactly the same for each drive.  

One solution I came up with was to name each thread with the pci index number, for example (SeekTest1, SeekTest2,...).  Then in the back end, test for the name and have separate variable for each.  

if(thread->Name == "SeekTest1")
{
  Test and update the variables for pci slot 1 and return.
}

That works to get correct performance values for each slot.  But it sort of forces the access to each slot one by one.  What I really want to do is run the tests concurrently AND have the correct performance values returned.  Maybe that's too much to ask.

How do I launch the back end function as an individual thread that can keep track of the individual performance values and test each drive concurrently?

To be clear, the front end is launching a back end thread for each slot, which then launches the function based on the PCI card installed.

Front End
||||||||
SeekTest(pci) x 8
|
SeekTestHiPoint (pci) or SeekTestSiliconImage(pci)

I hope this makes sense.

Thank you.
LVL 1
Elena QuinnAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill BachPresident and Btrieve GuruCommented:
I've done similar code, and there are a few tricks to getting this to work.  First, create an array of data structures, one per thread, with all pertinent data in the structure.  The front-end app should initialize the structures for each thread before they are called, and then it should display the resulting data when all are done.  Then, you'll pass the thread ID into the child process so that it knows which structure copy to update.  The other thing you MAY need to do (depending on your memory model and other factors) is to leverage an OS semaphore.  The code might look like this:

HANDLE hThreadTrackMutex;
hThreadTrackMutex=CreateMutex(NULL,FALSE,NULL);

void UpdateThreadTrack(long ID, unsigned long Blocks, long Status)
{
	WaitForSingleObject(hThreadTrackMutex,INFINITE);
	ThreadTrack[ID].BlocksRead=Blocks;
	ThreadTrack[ID].ReturnCode=Status;
	ReleaseMutex(hThreadTrackMutex);
}

Open in new window

The purpose of the MutEx Semaphore is to ensure that only ONE of the threads is modifying the parent's data structure at a time.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Storage

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.