Threading Design Question

G

Guest

Hello All,
I am working on introducing threading to my single threaded application to
take advantage of these multi-core processors.

I qurery for user data from file share and Active Directory with using two
methods

UserData data = GetFromFileShare(userName);
if(data == null)
{
data = GetFromAD(userName);
}
I want to do these two opertions in parallel to reduce the time for overall
operation incase data is in AD (which is 85% most of the time but we have tp
consider File share since file share data gets priority)

1. Execute two threads for these two operations.
2. Wait for File share method to finish. If it returns result, do not wait
for AD thread to finish and return.
3. If FileShare returns null, wait for AD to finish and return AD result.
4. If AD also returns null, throw UserDataNotFoundException

Can you experts provide me a code snippet to do this in .Net 2.0?

Thanks a million for this,
 
P

Peter Duniho

[...]
I want to do these two opertions in parallel to reduce the time for
overall
operation incase data is in AD (which is 85% most of the time but we
have tp
consider File share since file share data gets priority)

1. Execute two threads for these two operations.
2. Wait for File share method to finish. If it returns result, do not
wait
for AD thread to finish and return.
3. If FileShare returns null, wait for AD to finish and return AD result.
4. If AD also returns null, throw UserDataNotFoundException

Can you experts provide me a code snippet to do this in .Net 2.0?

Maybe something like this (uncompiled, untested):

UserData dataFileShare, dataAD, data;
Thread threadFileShare, threadAD;

threadFileShare = new Thread(new ThreadStart(delegate()
{ dataFileShare = GetFromFileShare(userName); }));
threadAD = new Thread(new ThreadStart(delegate() { dataAD =
GetFromAD(userName); }));

threadFileShare.Start();
threadAD.Start();

threadFileShare.Join();

if (dataFileShare != null)
{
data = dataFileShare;
}
else
{
threadAD.Join();
if (dataAD == null)
{
throw new UserDataNotFoundException();
}
data = dataAD;
}

Note that I'm still getting used to the anonymous delegate syntax and rely
on the compiler to tell me what exactly I need to do. The above might not
compile right off the bat, but if not hopefully the compiler error will
give you good direction. I think the basic idea is correct.

Pete
 
G

Guest

Thanks Peter. This is awesome. I wasn't sure how to wait for next thread but
you made my life easy. Thanks a milliton for this. I just added one small
line to abort AD thread if file share already returned.

threadFileShare.Join();

if (dataFileShare != null)
{
data = dataFileShare;
if (threadAD.IsAlive)
{
threadAD.Abort();//Abort AD thread since we dont need
this data so why to spend resources on it

}
} }

Do you think its a good enhancmeent and will not cuse any trouble?
I did limited testing and it works fine.

Salim
Peter Duniho said:
[...]
I want to do these two opertions in parallel to reduce the time for
overall
operation incase data is in AD (which is 85% most of the time but we
have tp
consider File share since file share data gets priority)

1. Execute two threads for these two operations.
2. Wait for File share method to finish. If it returns result, do not
wait
for AD thread to finish and return.
3. If FileShare returns null, wait for AD to finish and return AD result.
4. If AD also returns null, throw UserDataNotFoundException

Can you experts provide me a code snippet to do this in .Net 2.0?

Maybe something like this (uncompiled, untested):

UserData dataFileShare, dataAD, data;
Thread threadFileShare, threadAD;

threadFileShare = new Thread(new ThreadStart(delegate()
{ dataFileShare = GetFromFileShare(userName); }));
threadAD = new Thread(new ThreadStart(delegate() { dataAD =
GetFromAD(userName); }));

threadFileShare.Start();
threadAD.Start();

threadFileShare.Join();

if (dataFileShare != null)
{
data = dataFileShare;
}
else
{
threadAD.Join();
if (dataAD == null)
{
throw new UserDataNotFoundException();
}
data = dataAD;
}

Note that I'm still getting used to the anonymous delegate syntax and rely
on the compiler to tell me what exactly I need to do. The above might not
compile right off the bat, but if not hopefully the compiler error will
give you good direction. I think the basic idea is correct.

Pete
 
C

Chris Mullins [MVP]

Salim said:
I just added one small line to abort AD thread if file
share already returned.
threadAD.Abort();//Abort AD thread since we dont need
this data so why to spend resources on it

Aborting the thread like that will cause all sorts of problems. These
problems may introduce major instability into your application.

You're better off just letting the thread finish and go away. Aborting it is
much worse than letting it suck up a few AD resources.
 
P

Peter Duniho

[...]
Do you think its a good enhancmeent and will not cuse any trouble?
I did limited testing and it works fine.

As Chris says, aborting the thread is a very bad idea.

If you had complete control over all of the code executed within the
thread, then _maybe_ you could get away with aborting the thread, even
though doing so would still be poor design and likely to create
hard-to-track-down bugs.

But given that you are calling external code, you definitely should _not_
abort the thread. You have no way to ensure that the external code can
deal with this gracefully, and it's really not going to hurt anything to
allow the query to go on without you. You clearly can afford the
processing in the general case, and so you should easily be able to afford
it in all cases.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Threading Question 1
Application design 6
Threading question 3
Threading and WaitAny 1
Multi-Threading 9
Remoting/Threading 2
threading question: is this code going to work perfectly? 3
Threading question 5

Top