473,695 Members | 2,951 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Threadin g;
using System.Collecti ons;

public class A
{
private Thread thread;
private static Queue q =new Queue();
public A()
{
thread = new Thread(new ThreadStart(thi s.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.R un));
}

private void Run()
{
// do some work
thread.Sleep(10 00);
//insert the thread objekt into a queue
q.Enqueue(threa d);
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 10789
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.co m>
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.co m

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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.c om> wrote in
message news:en******** ******@TK2MSFTN GP12.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.co m

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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.co m>
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.Threadin g;

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

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

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

--
Jon Skeet - <sk***@pobox.co m>
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.co m

"Kovan Akrei" <ko****@ifi.uio .no> wrote in message
news:O8******** ******@TK2MSFTN GP12.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.c om> wrote in message news:en******** ******@TK2MSFTN GP12.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.co m

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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.co m>
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******** ******@tk2msftn gp13.phx.gbl...
Thanks for replying so fast :)

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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.Threadin g;
using System.Collecti ons;

public class A
{
private Thread thread;
private static Queue q =new Queue();
public A()
{
thread = new Thread(new ThreadStart(thi s.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(10 00);
//insert the thread objekt into a queue
q.Enqueue(threa d);
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*****@fnisol utions.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.Threadin g;
using System.Collecti ons;

public class A
{
private Thread thread;
private static Queue q =new Queue();
public A()
{
thread = new Thread(new ThreadStart(thi s.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(10 00);
//insert the thread objekt into a queue
q.Enqueue(threa d);
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

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

Similar topics

7
7867
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 then run it again. Thanks you very much Sayoyo
1
1367
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 of reading. Now i want to create a files and threads at runtime to avoid these errors. How can i create mutiple files and threads for reading data from that files at runtime.Is there any way?
3
1559
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 of unable to stop and reuse the threads pls help me...
4
5799
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 contains .Net functions wrapped in C++ (as described by Paul DiLascia -- see http://msdn.microsoft.com/msdnmag/issues/06/06/CAtWork/default.aspx). Both DLLs specify 'Use MFC in a shared DLL'. Since we have an old VC 6 application (large) that we...
3
2302
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 are made and then all put in the threadpool at once, which is well within the limits of the threadpool. However, what happens if you want to do 10,000 Fibonacci calculations
0
8619
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9112
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8971
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8817
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6484
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4336
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2994
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2258
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.