473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

reuse of threads (urgent)

Hi,
I would like to know how to reuse an object of a thread (if it is possible)
in Csharp? I have the following program:

using System;
using System.Threading;
using System.Collections;

public class A
{
private Thread thread;
private static Queue q =new Queue();
public A()
{
thread = new Thread(new ThreadStart(this.Run));
}

private static void getThread(A a)
{
if (a.thread != null)
return;
else
if(q.count > 0)
// there are available threads
a.thread = (Thread) q.Dequeue();
// ** Here i have to tell a.thread to run a.Run method when
it starts **
// I do not know how to do that.
else
// make a new thread object
a.thread = new Thread(new ThreadStart(a.Run));
}

private void Run()
{
// do some work
thread.Sleep(1000);
//insert the thread objekt into a queue
q.Enqueue(thread);
thread = null;
}
} // A

This implementation does not work, because when a thread is assigned to a
new object of class A it has to know which method to execute. I know that I
could use a threadpool, but I do not want to because my program ( a
simulation) uses sometimes millions of threads. Threadpool allows only 25
threads by default. It is some how difficult to change this number in
Csharp.

Many thanks in advance :)

Regards from
Kovan
Nov 15 '05 #1
34 10716
Kovan Akrei <ko****@ifi.uio.no> wrote:
I would like to know how to reuse an object of a thread (if it is possible)
in Csharp?


Write a system where each thread knows about a list of work items, and
takes items off the list until the list is empty, then waits for the
list to have work items added to it. This is basically what the built-
in ThreadPool does anyway.

You should be aware that when you say you're going to use "millions of
threads" that's not actually going to happen - and even if it did,
performance would be excruciating.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
It should also be noted that the ThreadPool will allow you to create as
many ^requests^ as you wish. It just won't execute more than 25 threads
(this is the number currently assigned per processor, but is subject to
change).

So, in this situation, the ThreadPool will actually suit the original
poster's needs, as it will queue up all the work, and then execute when it
can. Like Jon said, there is no way you are going to get one million
threads to run at the same time. If anything, I bet the performance using
the thread pool would destroy the performance when creating one million
threads. I would think the context switches alone would kill you.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Kovan Akrei <ko****@ifi.uio.no> wrote:
I would like to know how to reuse an object of a thread (if it is possible) in Csharp?


Write a system where each thread knows about a list of work items, and
takes items off the list until the list is empty, then waits for the
list to have work items added to it. This is basically what the built-
in ThreadPool does anyway.

You should be aware that when you say you're going to use "millions of
threads" that's not actually going to happen - and even if it did,
performance would be excruciating.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #3
Thanks for replying so fast :)

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Kovan Akrei <ko****@ifi.uio.no> wrote:
I would like to know how to reuse an object of a thread (if it is possible) in Csharp?
Write a system where each thread knows about a list of work items, and
takes items off the list until the list is empty, then waits for the
list to have work items added to it. This is basically what the built-
in ThreadPool does anyway.

The list you are talking about will get empty very fast in a simulation
program. Im trying to avoid creating new threads each time a new process
object in the simulation is about to be created. By doing so I'll avoid
allot of overhead. In my simulation program (discrete event simulation) I
get sometimes up to one hundred thousen threads running, but not all of them
are active at the same time. Some of them are in WaitSleep state, while
others are in running state (using cpu power). There are almost alyways more
than 25 threads in a running state. The problem with a thread pool is that
when a program has exhausted the pool other process which has to be executed
at once has to wait until there is a thread available in threadpool.
You should be aware that when you say you're going to use "millions of
threads" that's not actually going to happen - and even if it did,
performance would be excruciating.

I know. As I stated earlier. Not all of them are in a rrunning state, but
many of them are.

Kovan
Nov 15 '05 #4
Read the poster i send to John :). The reply there is relevant to your
comments too .

Kovan

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:en**************@TK2MSFTNGP12.phx.gbl...
It should also be noted that the ThreadPool will allow you to create as many ^requests^ as you wish. It just won't execute more than 25 threads
(this is the number currently assigned per processor, but is subject to
change).

So, in this situation, the ThreadPool will actually suit the original
poster's needs, as it will queue up all the work, and then execute when it
can. Like Jon said, there is no way you are going to get one million
threads to run at the same time. If anything, I bet the performance using
the thread pool would destroy the performance when creating one million
threads. I would think the context switches alone would kill you.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Kovan Akrei <ko****@ifi.uio.no> wrote:
I would like to know how to reuse an object of a thread (if it is possible) in Csharp?


Write a system where each thread knows about a list of work items, and
takes items off the list until the list is empty, then waits for the
list to have work items added to it. This is basically what the built-
in ThreadPool does anyway.

You should be aware that when you say you're going to use "millions of
threads" that's not actually going to happen - and even if it did,
performance would be excruciating.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 15 '05 #5
Kovan Akrei <ko****@ifi.uio.no> wrote:
Write a system where each thread knows about a list of work items, and
takes items off the list until the list is empty, then waits for the
list to have work items added to it. This is basically what the built-
in ThreadPool does anyway.
The list you are talking about will get empty very fast in a simulation
program.
And that's fine.
Im trying to avoid creating new threads each time a new process
object in the simulation is about to be created.
And the above does exactly that - note that the thread waits until a
new work item has been added to the list, it doesn't just terminate
when the list is exhausted.
By doing so I'll avoid
allot of overhead. In my simulation program (discrete event simulation) I
get sometimes up to one hundred thousen threads running, but not all of them
are active at the same time.
I'd be surprised if you got to that many *real* threads. (See later.)
Some of them are in WaitSleep state, while
others are in running state (using cpu power). There are almost alyways more
than 25 threads in a running state.
Do you have 25 processors? If not, it's unlikely that it'll be working
very efficiently - the context switches will be hurting you.
The problem with a thread pool is that
when a program has exhausted the pool other process which has to be executed
at once has to wait until there is a thread available in threadpool.
Well, you could certainly write a threadpool which allowed items to be
added in "emergency" mode which made sure that if no threads were
available, a new one was created and added to the pool.

There are various reasons why the default threadpool implementation may
not suit you, but it's a good starting model. Consider how it works,
and what extra features you need - then write your own.
You should be aware that when you say you're going to use "millions of
threads" that's not actually going to happen - and even if it did,
performance would be excruciating.

I know. As I stated earlier. Not all of them are in a rrunning state, but
many of them are.


Even so, just having a million threads *at all* is unlikely to get you
anywhere. You'll run out of handles long before that. Try the program
below:

using System;
using System.Threading;

class Test
{
static void Main()
{
for (int i=0; i < 1000000; i++)
{
if (i%100==0)
Console.WriteLine (i);
new Thread (new ThreadStart(Sleep)).Start();
}
}

static void Sleep()
{
Thread.Sleep (100000);
}
}

On my box, that terminates with an OutOfMemoryException somewhere
between 1800 and 1900 threads. What does it do on your box?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #6
Kovan,

I saw your reply to Jon's response, and I still think that you have too
many threads running at the same time. Have you actually tried the
ThreadPool to see what the difference in performance is? I still think that
the context switches on that many threads (as well as the resources required
to maintain that many threads, active or inactive, on the system).

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Kovan Akrei" <ko****@ifi.uio.no> wrote in message
news:O8**************@TK2MSFTNGP12.phx.gbl...
Read the poster i send to John :). The reply there is relevant to your
comments too .

Kovan

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in message news:en**************@TK2MSFTNGP12.phx.gbl...
It should also be noted that the ThreadPool will allow you to create

as
many ^requests^ as you wish. It just won't execute more than 25 threads
(this is the number currently assigned per processor, but is subject to
change).

So, in this situation, the ThreadPool will actually suit the original poster's needs, as it will queue up all the work, and then execute when it can. Like Jon said, there is no way you are going to get one million
threads to run at the same time. If anything, I bet the performance using the thread pool would destroy the performance when creating one million
threads. I would think the context switches alone would kill you.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Kovan Akrei <ko****@ifi.uio.no> wrote:
> I would like to know how to reuse an object of a thread (if it is

possible)
> in Csharp?

Write a system where each thread knows about a list of work items, and
takes items off the list until the list is empty, then waits for the
list to have work items added to it. This is basically what the built-
in ThreadPool does anyway.

You should be aware that when you say you're going to use "millions of
threads" that's not actually going to happen - and even if it did,
performance would be excruciating.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



Nov 15 '05 #7
As Jon and others have said, you are counter productive with more then a few
threads in most cases. Threads can help you if they can actually do
different work while waiting for IO or if you need to give interactive feel
to a GUI or need to support state and interactivity for multiple clients at
once. Context switches are expensive and all threads will compete for the
execution path. Its like having 10 cooks in the kitchen, they all end up
running into each other and much less work is done then if one or two cooks
did things in an organized fashion. Lets take an example. Say you want to
send 100 different DNS queries and process the replies (assume one udp
request=one udp reply.) So you fire up 100 threads to send the requests
(lets look at just the request side for now.) So thread1 puts together a
packet and before it gets to send, thread2 gets the cpu and puts together a
packet, then thread3, etc. thread1 gets the cpu back and actually does udp
send, and thread2 does the same, at various times other threads wake up and
block waiting for the socket to free to post another send. This does not
help us with performance. We are switching away from thread1 before it can
finish just to start another request that may or may not get to post that
send and then switch back to post the first send. So I did not end up
posting either send in two context switches in that case (could be less,
could be *more.) That is like trying to paint two rooms at once - one
stroke on wall one, then run over to second room and do a stroke on that
wall and repeat. In the end, it would have been much faster to finish room1
and then move on to room2. So starting up 100 threads to send 100 requests
is not the answer, you up doing partial work on each for awhile and spending
a lot of time to context switches and waiting on IO (contension for the IP
stack.) The IP stack has a queue to, so you still end up in a hurry-up and
wait situation anyway at some point - many threads does not help here. A
better design would be to have one thread send requests (retreived from your
queue) asyncronously as fast as possible, one after the other in a tight
loop. The reply side has a similar story. There is no sense having 10
threads waiting for replies as each thread can only process so much work
before it gives up the cpu for another thread to process a section of code.
So your adding overhead without actually doing more work. So in your case,
your not trying to give an interactive "feel" to multiple clients (as in a
multi-threaded server service) your trying to go from point A to point B as
fast as possible (I assume from your text.) Two very good books on this
subject are:
- Win32 System Programming: A Windows(R) 2000 Application Developer's Guide
(2nd Edition)
- Multithreading Applications in Win32 : The Complete Guide to Threads
--
William Stacey, MVP

"Kovan Akrei" <ko****@ifi.uio.no> wrote in message
news:uE**************@tk2msftngp13.phx.gbl...
Thanks for replying so fast :)

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Kovan Akrei <ko****@ifi.uio.no> wrote:
I would like to know how to reuse an object of a thread (if it is possible) in Csharp?
Write a system where each thread knows about a list of work items, and
takes items off the list until the list is empty, then waits for the
list to have work items added to it. This is basically what the built-
in ThreadPool does anyway.

The list you are talking about will get empty very fast in a simulation
program. Im trying to avoid creating new threads each time a new process
object in the simulation is about to be created. By doing so I'll avoid
allot of overhead. In my simulation program (discrete event simulation) I
get sometimes up to one hundred thousen threads running, but not all of

them are active at the same time. Some of them are in WaitSleep state, while
others are in running state (using cpu power). There are almost alyways more than 25 threads in a running state. The problem with a thread pool is that
when a program has exhausted the pool other process which has to be executed at once has to wait until there is a thread available in threadpool.
You should be aware that when you say you're going to use "millions of
threads" that's not actually going to happen - and even if it did,
performance would be excruciating.

I know. As I stated earlier. Not all of them are in a rrunning state, but
many of them are.

Kovan

Nov 15 '05 #8
Dan
For anyone who is interested here is an interesting
article on building your own thread pool using the Win32
API. I have worked through the article and have tested
using the pool in "real" applications. It has worked great
for me.

http://www.devarticles.com/art/1/508

-----Original Message-----
Hi,
I would like to know how to reuse an object of a thread (if it is possible)in Csharp? I have the following program:

using System;
using System.Threading;
using System.Collections;

public class A
{
private Thread thread;
private static Queue q =new Queue();
public A()
{
thread = new Thread(new ThreadStart(this.Run));
}

private static void getThread(A a)
{
if (a.thread != null)
return;
else
if(q.count > 0)
// there are available threads
a.thread = (Thread) q.Dequeue();
// ** Here i have to tell a.thread to run a.Run method whenit starts **
// I do not know how to do that.
else
// make a new thread object
a.thread = new Thread(new ThreadStart (a.Run)); }

private void Run()
{
// do some work
thread.Sleep(1000);
//insert the thread objekt into a queue
q.Enqueue(thread);
thread = null;
}
} // A

This implementation does not work, because when a thread is assigned to anew object of class A it has to know which method to execute. I know that Icould use a threadpool, but I do not want to because my program ( asimulation) uses sometimes millions of threads. Threadpool allows only 25threads by default. It is some how difficult to change this number inCsharp.

Many thanks in advance :)

Regards from
Kovan
.

Nov 15 '05 #9
Why build youre own when its already done in .net

You think you can do better? If you need more , just edit the .h file from
the default of 25 or add more CPUs :D

"Dan" <dd*****@fnisolutions.com> wrote in message
news:04****************************@phx.gbl...
For anyone who is interested here is an interesting
article on building your own thread pool using the Win32
API. I have worked through the article and have tested
using the pool in "real" applications. It has worked great
for me.

http://www.devarticles.com/art/1/508

-----Original Message-----
Hi,
I would like to know how to reuse an object of a thread

(if it is possible)
in Csharp? I have the following program:

using System;
using System.Threading;
using System.Collections;

public class A
{
private Thread thread;
private static Queue q =new Queue();
public A()
{
thread = new Thread(new ThreadStart(this.Run));
}

private static void getThread(A a)
{
if (a.thread != null)
return;
else
if(q.count > 0)
// there are available threads
a.thread = (Thread) q.Dequeue();
// ** Here i have to tell a.thread to run

a.Run method when
it starts **
// I do not know how to do that.
else
// make a new thread object
a.thread = new Thread(new ThreadStart

(a.Run));
}

private void Run()
{
// do some work
thread.Sleep(1000);
//insert the thread objekt into a queue
q.Enqueue(thread);
thread = null;
}
} // A

This implementation does not work, because when a thread

is assigned to a
new object of class A it has to know which method to

execute. I know that I
could use a threadpool, but I do not want to because my

program ( a
simulation) uses sometimes millions of threads.

Threadpool allows only 25
threads by default. It is some how difficult to change

this number in
Csharp.

Many thanks in advance :)

Regards from
Kovan
.

Nov 15 '05 #10
Mr.Tickle <Mr******@mrmen.com> wrote:
Why build youre own when its already done in .net


Because there are various problems with the existing threadpool:

o No control over when threads are created
o No control over how many threads are created
o No control over when/if threads are terminated
o Thread pool is shared between all applications (I think - not
absolutely sure on this one)
o Thread pool is also used by other classes in the .NET framework
which can cause problems

It's fine for many things, but not ideal for everyone.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #11
Hi folks,
Thank you all for your contribution. In this email I'll try to explain why I
need to develop a simple threadpool manager (regardles of OS) of my own and
not use the built in ThreadPool class.
I'm a master degree student who is trying to write a discrete event
simulation package in C#. In this process I have to develop a coroutine
package (as stated in the programing language Simula) using .Net threads. I
have done this job and I'm trying now to optimize the package.

For those of you who are not familier with coroutines:
Coroutines are a light version threads introduced by Simula in the 60. They
are light threads in the sence that they work in quasi parallell. Many
coroutine can run at the same time, but ONLY ONE coroutine will be executing
at a time regardless of the number of CPUs.
Unlike threads, coroutines are able to pass the control explicitly to each
other. For example a coroutine A is able to pass the controll explicitly to
another coroutine B by the means of resume(B) or call(B) methods (Simula
keywords). Sence you cant do this with threads, we have to controll threads
some how and make sure that thread A passes the control to thread B and only
B. To do so , we have to syncronise threads by means of lock, notify and
wait statements (keep track of their execution state) and by doing so you
cant use threadpool sence you do not have any control of the threads running
in a threadpool.

Making and administrating threads requires allot of overhead which slows
down my package. Now I'm trying some how to economize the thread usage by
reusing threads that already has been made available by means of keyword
new.

I was thinking that when a thread is made it executes a certain coroutine
and remains executing the same coroutine until the coroutine is finished.
After that the thread inserts itself into a list of free threads. Next time
a coroutine objekt wants to be executed it uses a thread from among the
free/available threads on the list. If there were no free threads then it
should make a new thread. This way I'll keep the number of threads made to
the minimum reducing the memory usage and improve performance.

Many regards
Kovan
"Kovan Akrei" <ko****@ifi.uio.no> wrote in message
news:#X**************@TK2MSFTNGP10.phx.gbl...
Hi,
I would like to know how to reuse an object of a thread (if it is possible) in Csharp? I have the following program:

using System;
using System.Threading;
using System.Collections;

public class A
{
private Thread thread;
private static Queue q =new Queue();
public A()
{
thread = new Thread(new ThreadStart(this.Run));
}

private static void getThread(A a)
{
if (a.thread != null)
return;
else
if(q.count > 0)
// there are available threads
a.thread = (Thread) q.Dequeue();
// ** Here i have to tell a.thread to run a.Run method when it starts **
// I do not know how to do that.
else
// make a new thread object
a.thread = new Thread(new ThreadStart(a.Run));
}

private void Run()
{
// do some work
thread.Sleep(1000);
//insert the thread objekt into a queue
q.Enqueue(thread);
thread = null;
}
} // A

This implementation does not work, because when a thread is assigned to a
new object of class A it has to know which method to execute. I know that I could use a threadpool, but I do not want to because my program ( a
simulation) uses sometimes millions of threads. Threadpool allows only 25
threads by default. It is some how difficult to change this number in
Csharp.

Many thanks in advance :)

Regards from
Kovan

Nov 15 '05 #12
Why build youre own when its already done in .net
Because there are various problems with the existing threadpool:

o No control over when threads are created
o No control over how many threads are created
o No control over when/if threads are terminated
o Thread pool is shared between all applications (I think - not
absolutely sure on this one)

FYI: each Win32 process (application) that hosts the CLR is completely
separate so each would get its own thread pool. The thread pool is shared
amongst all the separate appdomains within a single win32 process.
o Thread pool is also used by other classes in the .NET framework
which can cause problems

Other potential issues:
* A single thread pool
* All threads run at the same priority.
* Cannot reserve a block of threadpool threads for specific purposes (e.g.
out of band data that must be serviced ASAP)
* Custom security settings

It's fine for many things, but not ideal for everyone.

Agreed. It's fine for lots of general purpose uses.
Nov 15 '05 #13
Dave <no****************@wi.rr.com> wrote:
Why build youre own when its already done in .net
Because there are various problems with the existing threadpool:

o No control over when threads are created
o No control over how many threads are created
o No control over when/if threads are terminated
o Thread pool is shared between all applications (I think - not
absolutely sure on this one) FYI: each Win32 process (application) that hosts the CLR is completely
separate so each would get its own thread pool. The thread pool is shared
amongst all the separate appdomains within a single win32 process.


Sorry, yes - that's what I meant. There could be multiple applications
(such as webapps) running within the same CLR. Sorry not to be clearer
- applications wasn't a good word to use.
o Thread pool is also used by other classes in the .NET framework
which can cause problems


Other potential issues:
* A single thread pool
* All threads run at the same priority.
* Cannot reserve a block of threadpool threads for specific purposes (e.g.
out of band data that must be serviced ASAP)
* Custom security settings


Yup.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #14
FYI: each Win32 process (application) that hosts the CLR is completely
separate so each would get its own thread pool. The thread pool is shared amongst all the separate appdomains within a single win32 process.


Sorry, yes - that's what I meant. There could be multiple applications
(such as webapps) running within the same CLR. Sorry not to be clearer
- applications wasn't a good word to use.

Yup. And congrats on the MVP status.
Nov 15 '05 #15
> Why build youre own when its already done in .net
, well the built in does not fit my needs.
You think you can do better? If you need more , just edit the .h file from
the default of 25 or add more CPUs :D
That is not the issue. What I need is have to work with my implementation.
If you read my extra notes to my original post you might get a better view
of what I'm looking for.

Kovan
"Dan" <dd*****@fnisolutions.com> wrote in message
news:04****************************@phx.gbl...
For anyone who is interested here is an interesting
article on building your own thread pool using the Win32
API. I have worked through the article and have tested
using the pool in "real" applications. It has worked great
for me.

http://www.devarticles.com/art/1/508

-----Original Message-----
Hi,
I would like to know how to reuse an object of a thread

(if it is possible)
in Csharp? I have the following program:

using System;
using System.Threading;
using System.Collections;

public class A
{
private Thread thread;
private static Queue q =new Queue();
public A()
{
thread = new Thread(new ThreadStart(this.Run));
}

private static void getThread(A a)
{
if (a.thread != null)
return;
else
if(q.count > 0)
// there are available threads
a.thread = (Thread) q.Dequeue();
// ** Here i have to tell a.thread to run

a.Run method when
it starts **
// I do not know how to do that.
else
// make a new thread object
a.thread = new Thread(new ThreadStart

(a.Run));
}

private void Run()
{
// do some work
thread.Sleep(1000);
//insert the thread objekt into a queue
q.Enqueue(thread);
thread = null;
}
} // A

This implementation does not work, because when a thread

is assigned to a
new object of class A it has to know which method to

execute. I know that I
could use a threadpool, but I do not want to because my

program ( a
simulation) uses sometimes millions of threads.

Threadpool allows only 25
threads by default. It is some how difficult to change

this number in
Csharp.

Many thanks in advance :)

Regards from
Kovan
.


Nov 15 '05 #16
Hi,
I have posted a reply to my original post where I explain my problemes a
little more in depth. I would appreciate if you (and everybody else) could
read that and write som comments.

Kovan

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:#b**************@tk2msftngp13.phx.gbl...
Kovan,

I saw your reply to Jon's response, and I still think that you have too many threads running at the same time. Have you actually tried the
ThreadPool to see what the difference in performance is? I still think that the context switches on that many threads (as well as the resources required to maintain that many threads, active or inactive, on the system).

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Kovan Akrei" <ko****@ifi.uio.no> wrote in message
news:O8**************@TK2MSFTNGP12.phx.gbl...
Read the poster i send to John :). The reply there is relevant to your
comments too .

Kovan

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:en**************@TK2MSFTNGP12.phx.gbl...
It should also be noted that the ThreadPool will allow you to create
as
many ^requests^ as you wish. It just won't execute more than 25
threads (this is the number currently assigned per processor, but is subject to change).

So, in this situation, the ThreadPool will actually suit the

original poster's needs, as it will queue up all the work, and then execute when it
can. Like Jon said, there is no way you are going to get one million
threads to run at the same time. If anything, I bet the performance using the thread pool would destroy the performance when creating one

million threads. I would think the context switches alone would kill you.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
> Kovan Akrei <ko****@ifi.uio.no> wrote:
> > I would like to know how to reuse an object of a thread (if it is
possible)
> > in Csharp?
>
> Write a system where each thread knows about a list of work items, and > takes items off the list until the list is empty, then waits for the
> list to have work items added to it. This is basically what the built- > in ThreadPool does anyway.
>
> You should be aware that when you say you're going to use "millions of > threads" that's not actually going to happen - and even if it did,
> performance would be excruciating.
>
> --
> Jon Skeet - <sk***@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too



Nov 15 '05 #17
<snip>

On my box, that terminates with an OutOfMemoryException somewhere
between 1800 and 1900 threads. What does it do on your box?


re: millions of threads....that's simply not possible on the standard
windows OS. Each thread is given a default stack size of 1Mbyte, and the max
amount of virtual address space per win32 process is 2Gbyte, hence the upper
limit of about 2000 threads. Thus the out of memory exception you see at
that number of threads. The original creator of this thread was simply
mistaken about his creating hundreds of thousands of threads. He may have
that many work items but not threads.
Nov 15 '05 #18
you normally get about 800 to 900 per 1gb machine.
Thats the experience I had, so if you get millions, how much ram do you
have??? the mind boggles.
"Dave" <no****************@wi.rr.com> wrote in message
news:#4**************@TK2MSFTNGP11.phx.gbl...
<snip>

On my box, that terminates with an OutOfMemoryException somewhere
between 1800 and 1900 threads. What does it do on your box?

re: millions of threads....that's simply not possible on the standard
windows OS. Each thread is given a default stack size of 1Mbyte, and the

max amount of virtual address space per win32 process is 2Gbyte, hence the upper limit of about 2000 threads. Thus the out of memory exception you see at
that number of threads. The original creator of this thread was simply
mistaken about his creating hundreds of thousands of threads. He may have
that many work items but not threads.

Nov 15 '05 #19
> re: millions of threads....that's simply not possible on the standard
I ment in theory.
windows OS. Each thread is given a default stack size of 1Mbyte, and the max amount of virtual address space per win32 process is 2Gbyte, hence the upper limit of about 2000 threads. Thus the out of memory exception you see at
that number of threads. The original creator of this thread was simply
mistaken about his creating hundreds of thousands of threads. He may have
that many work items but not threads.

Not all of them are active at the same time. May be I was not that clear in
my original posting. I didn't mean that you should run millions of threads.
I ment that the system (my system) could generate millions of threads where
many (thousends??) of them could be active at the same time (read my extra
notes to my original post).

Kovan
Nov 15 '05 #20
Kovan Akrei <ko****@ifi.uio.no> wrote:
I was thinking that when a thread is made it executes a certain coroutine
and remains executing the same coroutine until the coroutine is finished.
After that the thread inserts itself into a list of free threads. Next time
a coroutine objekt wants to be executed it uses a thread from among the
free/available threads on the list. If there were no free threads then it
should make a new thread. This way I'll keep the number of threads made to
the minimum reducing the memory usage and improve performance.


And that sounds like a typical threadpool, as I described before, just
with the extra proviso of immediately creating a new thread if one
isn't available.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #21

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Kovan Akrei <ko****@ifi.uio.no> wrote:
I was thinking that when a thread is made it executes a certain coroutine and remains executing the same coroutine until the coroutine is finished. After that the thread inserts itself into a list of free threads. Next time a coroutine objekt wants to be executed it uses a thread from among the
free/available threads on the list. If there were no free threads then it should make a new thread. This way I'll keep the number of threads made to the minimum reducing the memory usage and improve performance.


And that sounds like a typical threadpool, as I described before, just
with the extra proviso of immediately creating a new thread if one
isn't available.


Yes right som how, but there is a fundemental differnce between those
threads I use to implement coroutines and threadpool threads. Threads in a
thread pool are great for doing some small jobs that terminates. In my case
the job that a thread has to do might be long and extensive. Threads might
(in many cases forced to) give up control to other threads and later have to
continue doing the job, give the control again and so on. There might be
hundreds of threads doing this.
I use threads to implement coroutines (explained in a previous post).When a
thread that is executing a coroutine A my have to be suspend for a while due
to a a call from this coroutine (coroutine A) to give up the control in
favour of another coroutine B. Coroutine B does some work and give the
controll to a coroutine C and so on. Some where in this ocean of threads a
coroutine X may then give the contol back to A. Then A should continue from
its activation point and continue its work. After that and long before its
finished with its task hase to give up control again in favour of another
coroutine R.
This means that every thread which is executing a coroutine should presserve
its execution state. By doing so its alive and "knows" where to continue
when it get the control back.
Let say that I have a simulation system that contains more than 25
coroutines (uses more that 25 threads) and we are using ThreadPool. The task
every coroutine does is very long and takes more than 5 minutes. Let say
that each coroutine does some work and before its finished it gives the
control to the next coroutine. All the first 25 coroutines does this without
a problem. All the 25 threads which are asosiated coroutines are a live.
What happens when coroutine number 25 wants to start/resume the execution of
coroutine number 26? In my openion the simulation system will stop.

Kovan
Nov 15 '05 #22
Kovan Akrei <ko****@ifi.uio.no> wrote:
Let say that I have a simulation system that contains more than 25
coroutines (uses more that 25 threads) and we are using ThreadPool.


I wasn't suggesting using the standard threadpool - I was suggesting
writing your own. However, you should still be aware that if you create
hundreds of threads, even if most of them are sleeping, you may run
into resource problems.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #23
I don't think a Cray could handle that either...

--
William Stacey, MVP

"bwahahahaha" <bw*******@bwahahaha.org.ie> wrote in message
news:#U**************@TK2MSFTNGP10.phx.gbl...
you normally get about 800 to 900 per 1gb machine.
Thats the experience I had, so if you get millions, how much ram do you
have??? the mind boggles.
"Dave" <no****************@wi.rr.com> wrote in message
news:#4**************@TK2MSFTNGP11.phx.gbl...
<snip>

On my box, that terminates with an OutOfMemoryException somewhere
between 1800 and 1900 threads. What does it do on your box?


re: millions of threads....that's simply not possible on the standard
windows OS. Each thread is given a default stack size of 1Mbyte, and the

max
amount of virtual address space per win32 process is 2Gbyte, hence the

upper
limit of about 2000 threads. Thus the out of memory exception you see at
that number of threads. The original creator of this thread was simply
mistaken about his creating hundreds of thousands of threads. He may have that many work items but not threads.


Nov 15 '05 #24
This sounds a little like a GOF State Pattern could be used here. Have a
controlling Class "Master" that has a private member called coRoutine of
Type CoRoutine. Create your CoRoutine class as an abstract class. Derive
coroutines from CoRoutine base class. Each derived type will have a "Run()"
method (for example.) So you have (as example) derived classes coRoutineA
and coRoutineB. Inside the "Run" method, you decide what coRoutine to run
next which changed the "state" object in the container class to what ever
coRoutine you need to run next.

--
William Stacey, MVP

"Kovan Akrei" <ko****@ifi.uio.no> wrote in message
news:OZ**************@TK2MSFTNGP10.phx.gbl...

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Kovan Akrei <ko****@ifi.uio.no> wrote:
I was thinking that when a thread is made it executes a certain coroutine and remains executing the same coroutine until the coroutine is finished. After that the thread inserts itself into a list of free threads. Next time a coroutine objekt wants to be executed it uses a thread from among the free/available threads on the list. If there were no free threads then it should make a new thread. This way I'll keep the number of threads
made
to the minimum reducing the memory usage and improve performance.
And that sounds like a typical threadpool, as I described before, just
with the extra proviso of immediately creating a new thread if one
isn't available.


Yes right som how, but there is a fundemental differnce between those
threads I use to implement coroutines and threadpool threads. Threads in a
thread pool are great for doing some small jobs that terminates. In my

case the job that a thread has to do might be long and extensive. Threads might
(in many cases forced to) give up control to other threads and later have to continue doing the job, give the control again and so on. There might be
hundreds of threads doing this.
I use threads to implement coroutines (explained in a previous post).When a thread that is executing a coroutine A my have to be suspend for a while due to a a call from this coroutine (coroutine A) to give up the control in
favour of another coroutine B. Coroutine B does some work and give the
controll to a coroutine C and so on. Some where in this ocean of threads a
coroutine X may then give the contol back to A. Then A should continue from its activation point and continue its work. After that and long before its
finished with its task hase to give up control again in favour of another
coroutine R.
This means that every thread which is executing a coroutine should presserve its execution state. By doing so its alive and "knows" where to continue
when it get the control back.
Let say that I have a simulation system that contains more than 25
coroutines (uses more that 25 threads) and we are using ThreadPool. The task every coroutine does is very long and takes more than 5 minutes. Let say
that each coroutine does some work and before its finished it gives the
control to the next coroutine. All the first 25 coroutines does this without a problem. All the 25 threads which are asosiated coroutines are a live.
What happens when coroutine number 25 wants to start/resume the execution of coroutine number 26? In my openion the simulation system will stop.

Kovan

Nov 15 '05 #25
you normally get about 800 to 900 per 1gb machine.
Thats the experience I had, so if you get millions, how much ram do you
have??? the mind boggles.

I want one of those machines for Christmas...I'll load every book ever
written, every song ever sung, all into memory at the same time....
Nov 15 '05 #26

"Kovan Akrei" <ko****@ifi.uio.no> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
re: millions of threads....that's simply not possible on the standard I ment in theory.
windows OS. Each thread is given a default stack size of 1Mbyte, and the

max
amount of virtual address space per win32 process is 2Gbyte, hence the

upper
limit of about 2000 threads. Thus the out of memory exception you see at
that number of threads. The original creator of this thread was simply
mistaken about his creating hundreds of thousands of threads. He may have that many work items but not threads.

Not all of them are active at the same time. May be I was not that clear

in my original posting. I didn't mean that you should run millions of threads. I ment that the system (my system) could generate millions of threads where many (thousends??) of them could be active at the same time
It still doesn't matter. Whether they are what you call active or inactive
(I presume this means blocked/suspended versus executing) is irrelevant; the
most you can have in existence at any single time will be limited by the
amount of virtual address space available to your application. If your
design calls for this many threads to exist simultaneously, regardless of
its execution state, then I would revisit the design.

BTW, the absolutely most efficient design is one where there is a single
thread per processor in the ready to run queue - any more then that and you
start to lose efficiency due to the overhead of context switching. In
practice more then one is fine, but thousands that are all ready to run will
bog the machine down to a crawl.

It sounds like a custom thread pool will solve your problem. I know of
several that are freely available which you should be able to modify to suit
your needs.

(read my extra
notes to my original post).

I can't find them; pls repost them if they are relevant.

Nov 15 '05 #27
> It still doesn't matter. Whether they are what you call active or inactive
(I presume this means blocked/suspended versus executing) is irrelevant; the most you can have in existence at any single time will be limited by the
amount of virtual address space available to your application. If your
design calls for this many threads to exist simultaneously, regardless of
its execution state, then I would revisit the design.
That is what Im trying to do by economizing on thread usage . I'm aware of
the performance and memory problems. One should not think (due to my
simulation issue) of system (pv or os) boundries. Think in a general way
where you have enough memory and your os handles more that X number of
threads.
BTW, the absolutely most efficient design is one where there is a single
thread per processor in the ready to run queue - any more then that and you start to lose efficiency due to the overhead of context switching. In
practice more then one is fine, but thousands that are all ready to run will bog the machine down to a crawl.
That's right. We are talking about simulations and in a simulation you will
probably have many many processes (threads).
It sounds like a custom thread pool will solve your problem. I know of
several that are freely available which you should be able to modify to suit your needs.
May be. I was thinking of programing one by myself. That's why i start all
this by postin the original note. I was not that clear in that post :).
If you know of some custom threadpools would you please share it with me and
other users?
(read my extra
notes to my original post).

I can't find them; pls repost them if they are relevant.

I sent the extra notes as a reply to my original post. You will find there.

Kovan
Nov 15 '05 #28
If you know of some custom threadpools would you please share it with me and other users?


Here's one:
http://staff.develop.com/woodring/dotnet/#threadpool
Nov 15 '05 #29
thanks. I'll ut out very soon :).

"Dave" <no****************@wi.rr.com> wrote in message
news:e$**************@TK2MSFTNGP12.phx.gbl...
If you know of some custom threadpools would you please share it with me

and
other users?


Here's one:
http://staff.develop.com/woodring/dotnet/#threadpool

Nov 15 '05 #30
Coroutines don't require threads (and were actually invented in the
1950s. There's a nice little history in Vol 1 of Knuth's Art of
Computer Programming.)

If context switching in Operating System level threads is a concern,
you can use an approach similar to how Posix threads were originally
implemented on many Unix based operating systems (and in early versions
of the Java VM?). As it sounds like you probably don't need a
preemptive multitasking environment, this sort of approach would seem
to minimize operating system overhead.

It might even extend to "millions" of (non OS) threads.

Mike Blake-Knox

Nov 15 '05 #31
Jon Dave,

FYI. A number of these issues will be addressed in V2.0.

Willy.

PS. Jon, Congrats on the MVP status, well deserved.
"Dave" <no****************@wi.rr.com> wrote in message news:O6**************@TK2MSFTNGP10.phx.gbl...
FYI: each Win32 process (application) that hosts the CLR is completely
separate so each would get its own thread pool. The thread pool is shared amongst all the separate appdomains within a single win32 process.


Sorry, yes - that's what I meant. There could be multiple applications
(such as webapps) running within the same CLR. Sorry not to be clearer
- applications wasn't a good word to use.

Yup. And congrats on the MVP status.

Nov 15 '05 #32
Kovan,
You could do as Jon suggest, write your own threadpool, but if you need that very high number of threads you should definitely
implement it using a combination of threads and fibers, otherwise you will run into CPU and memory resource problems.

Willy.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message news:MP************************@msnews.microsoft.c om...
Kovan Akrei <ko****@ifi.uio.no> wrote:
Let say that I have a simulation system that contains more than 25
coroutines (uses more that 25 threads) and we are using ThreadPool.


I wasn't suggesting using the standard threadpool - I was suggesting
writing your own. However, you should still be aware that if you create
hundreds of threads, even if most of them are sleeping, you may run
into resource problems.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 15 '05 #33
Hi Willy,
I would like to try out fibers, but I do not know (yet) what they are. I
have implemented my package using threads and it works ome how nice but I
would realy like to exepriment more nad fibers sound something new for me
:).

Kovan

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:Od**************@TK2MSFTNGP12.phx.gbl...
Kovan,
You could do as Jon suggest, write your own threadpool, but if you need that very high number of threads you should definitely implement it using a combination of threads and fibers, otherwise you will run into CPU and memory resource problems.
Willy.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message

news:MP************************@msnews.microsoft.c om...
Kovan Akrei <ko****@ifi.uio.no> wrote:
Let say that I have a simulation system that contains more than 25
coroutines (uses more that 25 threads) and we are using ThreadPool.


I wasn't suggesting using the standard threadpool - I was suggesting
writing your own. However, you should still be aware that if you create
hundreds of threads, even if most of them are sleeping, you may run
into resource problems.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 15 '05 #34
Read following article and the MSDN SDK documentation, but be warned, fibers are not that general purpose and are a lot more
complicated to use than regular kernel threads, you should to stay away from it, if you can
http://msdn.microsoft.com/msdnmag/is...T/default.aspx

Willy.

"Kovan Akrei" <ko****@ifi.uio.no> wrote in message news:O%****************@tk2msftngp13.phx.gbl...
Hi Willy,
I would like to try out fibers, but I do not know (yet) what they are. I
have implemented my package using threads and it works ome how nice but I
would realy like to exepriment more nad fibers sound something new for me
:).

Kovan

"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:Od**************@TK2MSFTNGP12.phx.gbl...
Kovan,
You could do as Jon suggest, write your own threadpool, but if you need

that very high number of threads you should definitely
implement it using a combination of threads and fibers, otherwise you will

run into CPU and memory resource problems.

Willy.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message

news:MP************************@msnews.microsoft.c om...
Kovan Akrei <ko****@ifi.uio.no> wrote:
> Let say that I have a simulation system that contains more than 25
> coroutines (uses more that 25 threads) and we are using ThreadPool.

I wasn't suggesting using the standard threadpool - I was suggesting
writing your own. However, you should still be aware that if you create
hundreds of threads, even if most of them are sleeping, you may run
into resource problems.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



Nov 15 '05 #35

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: sayoyo | last post by:
Hi, Is there some way that we can reuse a thread by replacing the runnable object of the thread? like a thread is not "alive" anymore, then we remove the runnable object(is it possible????) and...
1
by: Mamatha | last post by:
Hi friends, I have an application using mutithreads.In that application one threads writes the data in to a file. Another thread reads the data from same file. So some errors while at the time...
3
by: notregister | last post by:
i have written a program using thread. i have 8 threads, each doing their own printing. i wish to add a restart function which allow to do printing all over again, but i am now fcing the problem...
4
by: Al Norman | last post by:
We have two separate DLLs that do not interact (directly, at least). One is an MFC extension DLL that was built back in VC++ 6 days (but has been recompiled with VS2005). The other is a DLL that...
3
by: Kevin | last post by:
Using this: http://msdn2.microsoft.com/en-us/library/3dasc8as(VS.80).aspx as an example I have a question concerning the reuse of objects. In the example 10 instances of the Fibonacci class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.