Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_vb_dotnet thread: Re: Multi-Threading


Message #1 by "Kevin Anthony" <kanthony@t...> on Wed, 12 Jun 2002 14:43:03
From my understanding, the thread pool has by default 25 threads(point to 
note timers use the thread pool) that you can use. After all these threads 
are used up the next request will wait until one of the threads becomes 
free. The Pool is created the first time you 

A) a timer is registered 
B) you invoke the Threadpool.queueuserworkitem 

To use the threadpool you need to call the Threadpool.queueuserworkitem 
 with a callback delegate with an Object argument that will process your 
work.

check out the .net SDK for more info 


ms-help://MS.NETFrameworkSDK/cpguidenf/html/cpconthreadpooling.htm


> I saw somethings about the thread pool, but What is it? And How does it 
work?

> Seth
>>  the easiest way is to use a Syslock(object) ... end syslock block in 
which 
y> > ou update shared variables.

> > As a side note have you thought about using the thread pool instead of 
a> > llowing the users to set the thread limit(if the users choose a large 
n> > umber of threads then the application will spend more time context 
s> > witching that actually processing the threads). 

> > > > I made my break through last week. As of right now, my program 
will open 
a> > t> > ext file, divide the letters evenly between all the threads (The 
last
t> > > hread gets the remaining letters if it can't be done evenly). Each 
t> > hread
w> > > ill count their portion of the letters and store the result in an 
array.
T> > > he fun thing about it is the speed. I tried this project before 
using 
V> > B6,
s> > > ingle thread program. It took over a day to count 7000 letters 
(give or
t> > > ake a few). I figured it would take close to six months to count my 
text
f> > > ile. Using VB.Net, It will count it in less then a minute. I just 
have 
o> > ne
p> > > roblem with it.

> > > The user can specify how many threads to count with. I'll use four 
for 
t> > his
e> > > xample.

> > > All 4 threads access the same public variable. How can I be sure 
that if
e> > > ach thread counts the letter "a" and adds it to the variable, that 
the
v> > > ariable is four?

> > > sample code:
P> > > ublic LetA as integer

> > > private sub count
d> > > im i as integer
d> > > im tmpLet as string
f> > > or i = 1 to myText.Length
 > > >  tmpLet = mid$(myText.Text, i, 1)
 > > >  if tmpLet = "a" then LetA = LetA + 1
n> > > ext i
e> > > nd sub

> > > Think of 4 different threads running that code at the same time. Is 
there
a> > > ny way it could get confused and come up with a different number?

> > > Seth

> > > > I don't believe that you will be able to create a multi-threaded 
a> > > > pplication to count letters in a file. The reason being is that 
the 
f> > > > irst thread started will probably lock that file when it opens 
it. 
W> > hy 
d> > > > oes this have to be multi-threaded? Multi-threading can bring 
about a 
l> > > > ot of problems if you're not careful. You have to make sure that 
m> > ethods 
a> > > > nd properties are thread-safe in order for you application to 
work 
p> > > > roperly. Hope this was some help. 

> > > > Kevin

  Return to Index