threadpool

  • Thread starter Thread starter memememe
  • Start date Start date
M

memememe

We are queueing stuff on the thread pool and what we are queueing gets done
pretty quick (the method returns fine) but yet its only allowed to run
around 25 times, is there anything I need to do to tell the threadpool that
my method is done running and the thread is to be disposed of?
 
memememe,

What do you mean that it is only allowed to run 25 times? Do you mean
that you can only have 25 concurrent activities at the same time?

The thread pool has a limit on the number of threads that can run at one
time from it. The max is determined by the number of processors, for each
processor on the machine, 25 threads are allowed in the thread pool.

Also, when your method is done, it will cause the thread pool thread to
be sent back to the pool, to be reused later. It should not die. This is
done to improve performance and reduce the overhead from initializing new
threads.

Hope this helps.
 
Nicholas Paldino said:
memememe,

What do you mean that it is only allowed to run 25 times? Do you mean
that you can only have 25 concurrent activities at the same time?

The thread pool has a limit on the number of threads that can run at one
time from it. The max is determined by the number of processors, for each
processor on the machine, 25 threads are allowed in the thread pool.

Also, when your method is done, it will cause the thread pool thread to
be sent back to the pool, to be reused later. It should not die. This is
done to improve performance and reduce the overhead from initializing new
threads.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


memememe said:
We are queueing stuff on the thread pool and what we are queueing gets done
pretty quick (the method returns fine) but yet its only allowed to run
around 25 times, is there anything I need to do to tell the threadpool that
my method is done running and the thread is to be disposed of?


sorry I should have been clearer.
We queue up a lot of work, more than 25, lets say 1k. We see the first 25
threads get executed, then they end, once they end no more work on the queue
gets executed, the threads in use are 25, the available threads are 0, but
our method ends and returns, so I am not sure what else we have to do to
tell the thread pool to go ahead and reuse the thread.
 
memememe,

You don't have to tell the ThreadPool class anything when you are done,
it will know when you are done.

Can you post your code, and we can take a look at it?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

memememe said:
in message news:[email protected]...
memememe,

How are you determining that the threads are not being reused? Once the
work item is complete, the thread should go back to the pool.

Also, the thread pool is used by other parts of the framework, and it
doesn't just run the threads whenever. It also takes other factors into
consideration when deciding which items to perform work on.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

memememe said:
in message memememe,

What do you mean that it is only allowed to run 25 times? Do
you
mean
that you can only have 25 concurrent activities at the same time?

The thread pool has a limit on the number of threads that can
run
at
one
time from it. The max is determined by the number of processors,
for
each
processor on the machine, 25 threads are allowed in the thread pool.

Also, when your method is done, it will cause the thread pool thread
to
be sent back to the pool, to be reused later. It should not die.
This
is
done to improve performance and reduce the overhead from
initializing
new
threads.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


We are queueing stuff on the thread pool and what we are queueing gets
done
pretty quick (the method returns fine) but yet its only allowed to run
around 25 times, is there anything I need to do to tell the threadpool
that
my method is done running and the thread is to be disposed of?






sorry I should have been clearer.
We queue up a lot of work, more than 25, lets say 1k. We see the first 25
threads get executed, then they end, once they end no more work on the queue
gets executed, the threads in use are 25, the available threads are 0, but
our method ends and returns, so I am not sure what else we have to do to
tell the thread pool to go ahead and reuse the thread.

well its a very simple program.
It queues up stuff to do on the threadpool (way more than 25) then it does
the stuff, I print out at the end of the method when its done processing,
and it exits fine, this happens for 25 times. There is nothing else running
on the system, its just a simple simple program, but I imagine once the
method is done running it should trhow the thread back on the queue? or do I
need to tell the thread pool that im done running that method?
 
Nicholas Paldino said:
memememe,

How are you determining that the threads are not being reused? Once the
work item is complete, the thread should go back to the pool.

Also, the thread pool is used by other parts of the framework, and it
doesn't just run the threads whenever. It also takes other factors into
consideration when deciding which items to perform work on.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

memememe said:
in message news:[email protected]... at
one thread
to
This
is
done to improve performance and reduce the overhead from initializing new
threads.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


We are queueing stuff on the thread pool and what we are queueing gets
done
pretty quick (the method returns fine) but yet its only allowed to run
around 25 times, is there anything I need to do to tell the threadpool
that
my method is done running and the thread is to be disposed of?


sorry I should have been clearer.
We queue up a lot of work, more than 25, lets say 1k. We see the first 25
threads get executed, then they end, once they end no more work on the queue
gets executed, the threads in use are 25, the available threads are 0, but
our method ends and returns, so I am not sure what else we have to do to
tell the thread pool to go ahead and reuse the thread.

well its a very simple program.
It queues up stuff to do on the threadpool (way more than 25) then it does
the stuff, I print out at the end of the method when its done processing,
and it exits fine, this happens for 25 times. There is nothing else running
on the system, its just a simple simple program, but I imagine once the
method is done running it should trhow the thread back on the queue? or do I
need to tell the thread pool that im done running that method?
 
Nicholas Paldino said:
memememe,

You don't have to tell the ThreadPool class anything when you are done,
it will know when you are done.

Can you post your code, and we can take a look at it?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

memememe said:
in message news:[email protected]...
memememe,

How are you determining that the threads are not being reused?
Once
the
work item is complete, the thread should go back to the pool.

Also, the thread pool is used by other parts of the framework, and it
doesn't just run the threads whenever. It also takes other factors into
consideration when deciding which items to perform work on.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


"Nicholas Paldino [.NET/C# MVP]"
wrote
in message memememe,

What do you mean that it is only allowed to run 25 times? Do you
mean
that you can only have 25 concurrent activities at the same time?

The thread pool has a limit on the number of threads that can
run
at
one
time from it. The max is determined by the number of processors, for
each
processor on the machine, 25 threads are allowed in the thread pool.

Also, when your method is done, it will cause the thread pool thread
to
be sent back to the pool, to be reused later. It should not die. This
is
done to improve performance and reduce the overhead from initializing
new
threads.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


We are queueing stuff on the thread pool and what we are
queueing
gets
done
pretty quick (the method returns fine) but yet its only allowed
to
run
around 25 times, is there anything I need to do to tell the threadpool
that
my method is done running and the thread is to be disposed of?






sorry I should have been clearer.
We queue up a lot of work, more than 25, lets say 1k. We see the
first
25
threads get executed, then they end, once they end no more work on the
queue
gets executed, the threads in use are 25, the available threads are
0,
but
our method ends and returns, so I am not sure what else we have to
do
to

well its a very simple program.
It queues up stuff to do on the threadpool (way more than 25) then it does
the stuff, I print out at the end of the method when its done processing,
and it exits fine, this happens for 25 times. There is nothing else running
on the system, its just a simple simple program, but I imagine once the
method is done running it should trhow the thread back on the queue? or
do
I
need to tell the thread pool that im done running that method?

thanks for the help, i have a feeling there is something hanging the code
somewhere else, its not my code (bosses) so I cant post, nor do I know where
the problem might be, the thread thing I posted was the first thing I
noticed, I will check a little further. Thanks again
 
Nicholas Paldino said:
memememe,

How are you determining that the threads are not being reused? Once the
work item is complete, the thread should go back to the pool.

Also, the thread pool is used by other parts of the framework, and it
doesn't just run the threads whenever. It also takes other factors into
consideration when deciding which items to perform work on.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

memememe said:
in message memememe,

What do you mean that it is only allowed to run 25 times? Do you mean
that you can only have 25 concurrent activities at the same time?

The thread pool has a limit on the number of threads that can run at
one
time from it. The max is determined by the number of processors, for each
processor on the machine, 25 threads are allowed in the thread pool.

Also, when your method is done, it will cause the thread pool thread
to
be sent back to the pool, to be reused later. It should not die.
This
is
done to improve performance and reduce the overhead from initializing new
threads.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


We are queueing stuff on the thread pool and what we are queueing gets
done
pretty quick (the method returns fine) but yet its only allowed to run
around 25 times, is there anything I need to do to tell the threadpool
that
my method is done running and the thread is to be disposed of?






sorry I should have been clearer.
We queue up a lot of work, more than 25, lets say 1k. We see the first 25
threads get executed, then they end, once they end no more work on the queue
gets executed, the threads in use are 25, the available threads are 0, but
our method ends and returns, so I am not sure what else we have to do to
tell the thread pool to go ahead and reuse the thread.

well its a very simple program.
It queues up stuff to do on the threadpool (way more than 25) then it does
the stuff, I print out at the end of the method when its done processing,
and it exits fine, this happens for 25 times. There is nothing else running
on the system, its just a simple simple program, but I imagine once the
method is done running it should trhow the thread back on the queue? or do I
need to tell the thread pool that im done running that method?


My experience has been that if a thread is not available because they
are all in use, it throws some sort of exception (which may be handled
internally) and the request just goes away. It doesn't appear to
queue them up.

Of course, I could be wrong about that but that has been my
experience.

What I do is increment a counter each time I queue a threadpool
thread. When that thread finishes, I decrement the thread count. You
can use the Interlocked class to do this.

You get 25 threads per CPU, but some of them may be used by the
framework. So, before I queue up, I check to make sure I have several
threads left. If I don't, I go to sleep for a second or two, and keep
checking in a loop until I have enough threads available. When there
are enough, I queue up another thread.

Also, if you main thread ends while threadpool threads are running,
they will be aborted, so before I leave my routine, I make sure that
my threadcount is back to my start count. I just sleep again for a
few seconds in a loop and check the amount until it reaches zero.

The only catch is you have to make sure your threadpool thread doesn't
throw any exceptions that you don't handle. Otherwise, your
threadcount will not decrement and you'll "lose" threads.

Jonathan Schafer
 
Jonathan Schafer said:
in message news:[email protected]...
memememe,

How are you determining that the threads are not being reused?
Once
the
work item is complete, the thread should go back to the pool.

Also, the thread pool is used by other parts of the framework, and it
doesn't just run the threads whenever. It also takes other factors into
consideration when deciding which items to perform work on.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


"Nicholas Paldino [.NET/C# MVP]"
wrote
in message memememe,

What do you mean that it is only allowed to run 25 times? Do you
mean
that you can only have 25 concurrent activities at the same time?

The thread pool has a limit on the number of threads that can
run
at
one
time from it. The max is determined by the number of processors, for
each
processor on the machine, 25 threads are allowed in the thread pool.

Also, when your method is done, it will cause the thread pool thread
to
be sent back to the pool, to be reused later. It should not die. This
is
done to improve performance and reduce the overhead from initializing
new
threads.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


We are queueing stuff on the thread pool and what we are queueing gets
done
pretty quick (the method returns fine) but yet its only allowed
to
run
around 25 times, is there anything I need to do to tell the threadpool
that
my method is done running and the thread is to be disposed of?






sorry I should have been clearer.
We queue up a lot of work, more than 25, lets say 1k. We see the
first
25
threads get executed, then they end, once they end no more work on the
queue
gets executed, the threads in use are 25, the available threads are
0,
but
our method ends and returns, so I am not sure what else we have to do to
tell the thread pool to go ahead and reuse the thread.

well its a very simple program.
It queues up stuff to do on the threadpool (way more than 25) then it does
the stuff, I print out at the end of the method when its done processing,
and it exits fine, this happens for 25 times. There is nothing else running
on the system, its just a simple simple program, but I imagine once the
method is done running it should trhow the thread back on the queue? or do I
need to tell the thread pool that im done running that method?


My experience has been that if a thread is not available because they
are all in use, it throws some sort of exception (which may be handled
internally) and the request just goes away. It doesn't appear to
queue them up.

Of course, I could be wrong about that but that has been my
experience.

What I do is increment a counter each time I queue a threadpool
thread. When that thread finishes, I decrement the thread count. You
can use the Interlocked class to do this.

You get 25 threads per CPU, but some of them may be used by the
framework. So, before I queue up, I check to make sure I have several
threads left. If I don't, I go to sleep for a second or two, and keep
checking in a loop until I have enough threads available. When there
are enough, I queue up another thread.

Also, if you main thread ends while threadpool threads are running,
they will be aborted, so before I leave my routine, I make sure that
my threadcount is back to my start count. I just sleep again for a
few seconds in a loop and check the amount until it reaches zero.

The only catch is you have to make sure your threadpool thread doesn't
throw any exceptions that you don't handle. Otherwise, your
threadcount will not decrement and you'll "lose" threads.

Jonathan Schafer

god I had to read the whole code a few times before I noticed what he was
doing.
He was adding more threads to the queue inside of one of the threads that
was running and wasnt letting the thread exit until the threads that it
added were done, but they werent done until they could be executed but they
couldnt be executed because the 25 thread limit was already used. Simple
change fixed it all.
 
Back
Top