473,326 Members | 2,095 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,326 software developers and data experts.

Approach to Implement a Thread Pump?

DC
Hi,

I need the functionality to take a workitem from a stack, execute it in
a different thread, get the next item, execute it asynchronously too,
and so on. But: there should never be more than 5 threads processing
workitems. As soon as the number of threads gets below five, more
workitems can be queued. This goes on until the stack of workitems is
fully processed. Not sure if that is what one would call a "Thread
Pump".

I am thinking about doing this with a .BeginInvoke() approach and a
static thread counter, but I wanted to check if somebody knows a common
pattern for my task? Also, with my approach I would do a sleep in the
main thread and periodically check if the static counter fell below 5,
but that seems clumsy to me. I don't know how to design a sync object
that can trigger program continuation whenver one of the threads
signaled that its execution finished.

On a sidenote: I am about to (finally) convert this project to
framework 2.0. Will that give me more threading options? I tested the
program (which works for years with framework 1.1) with framework 2.0
btw, and it produced a ton of "out of memory" errors (on the same
machine where it worked fine with 1.1).

Thanks for any hint in advance,
Regards
DC

Jan 16 '07 #1
5 2079
DC wrote:
Hi,

I need the functionality to take a workitem from a stack, execute it in
a different thread, get the next item, execute it asynchronously too,
and so on. But: there should never be more than 5 threads processing
workitems. As soon as the number of threads gets below five, more
workitems can be queued. This goes on until the stack of workitems is
fully processed. Not sure if that is what one would call a "Thread
Pump".
No, not really. I'd call it a Producer-Consumer Queue (with 5 Consumers
in this case). And I'd use Jon Skeet's implementation:

<http://www.yoda.arachsys.com/csharp/threads/deadlocks.shtml>

>
I am thinking about doing this with a .BeginInvoke() approach and a
static thread counter, but I wanted to check if somebody knows a common
pattern for my task? Also, with my approach I would do a sleep in the
main thread and periodically check if the static counter fell below 5,
but that seems clumsy to me. I don't know how to design a sync object
that can trigger program continuation whenver one of the threads
signaled that its execution finished.

On a sidenote: I am about to (finally) convert this project to
framework 2.0. Will that give me more threading options? I tested the
program (which works for years with framework 1.1) with framework 2.0
btw, and it produced a ton of "out of memory" errors (on the same
machine where it worked fine with 1.1).
Don't think there are any feature changes to multithreading in 2.0 - I
believe it's stricter about cross-thread no-no's in WinForms, but that's
about it.
--
Larry Lard
la*******@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
Jan 16 '07 #2
Hi,

Adding to the response of Larry,

| I need the functionality to take a workitem from a stack,

Are you sure you want to use a Stack and not a Queue?
Also make sure that the Stack (or Queue) is sync'ed . Both classes support
this already.

| On a sidenote: I am about to (finally) convert this project to
| framework 2.0. Will that give me more threading options? I tested the
| program (which works for years with framework 1.1) with framework 2.0
| btw, and it produced a ton of "out of memory" errors (on the same
| machine where it worked fine with 1.1).

I cannot think of anything different, not in such a way that would provoke a
"out of memory", are you using recursion?

--
Ignacio Machin
machin AT laceupsolutions com
Jan 16 '07 #3
DC
Thank you, Larry. A good read!

Larry Lard schrieb:
DC wrote:
Hi,

I need the functionality to take a workitem from a stack, execute it in
a different thread, get the next item, execute it asynchronously too,
and so on. But: there should never be more than 5 threads processing
workitems. As soon as the number of threads gets below five, more
workitems can be queued. This goes on until the stack of workitems is
fully processed. Not sure if that is what one would call a "Thread
Pump".

No, not really. I'd call it a Producer-Consumer Queue (with 5 Consumers
in this case). And I'd use Jon Skeet's implementation:

<http://www.yoda.arachsys.com/csharp/threads/deadlocks.shtml>


I am thinking about doing this with a .BeginInvoke() approach and a
static thread counter, but I wanted to check if somebody knows a common
pattern for my task? Also, with my approach I would do a sleep in the
main thread and periodically check if the static counter fell below 5,
but that seems clumsy to me. I don't know how to design a sync object
that can trigger program continuation whenver one of the threads
signaled that its execution finished.

On a sidenote: I am about to (finally) convert this project to
framework 2.0. Will that give me more threading options? I tested the
program (which works for years with framework 1.1) with framework 2.0
btw, and it produced a ton of "out of memory" errors (on the same
machine where it worked fine with 1.1).

Don't think there are any feature changes to multithreading in 2.0 - I
believe it's stricter about cross-thread no-no's in WinForms, but that's
about it.
--
Larry Lard
la*******@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
Jan 17 '07 #4
DC
Are you sure you want to use a Stack and not a Queue?

Yes, a queueue, "stack" was just easier to type.

I am not using recursion. My main memory consumer is a huge hashtable.
I think this object consumes more memory than it did in framework 1.1
(but in lack of an easy sizeof() operator I am unable to say how many
bytes the hashtable really consumes).

Ignacio Machin ( .NET/ C# MVP ) schrieb:
Hi,

Adding to the response of Larry,

| I need the functionality to take a workitem from a stack,

Are you sure you want to use a Stack and not a Queue?
Also make sure that the Stack (or Queue) is sync'ed . Both classes support
this already.

| On a sidenote: I am about to (finally) convert this project to
| framework 2.0. Will that give me more threading options? I tested the
| program (which works for years with framework 1.1) with framework 2.0
| btw, and it produced a ton of "out of memory" errors (on the same
| machine where it worked fine with 1.1).

I cannot think of anything different, not in such a way that would provoke a
"out of memory", are you using recursion?

--
Ignacio Machin
machin AT laceupsolutions com
Jan 17 '07 #5
Hi,
| On a sidenote: I am about to (finally) convert this project to
| framework 2.0. Will that give me more threading options?

Frankly I do not see what can have 2.0 of new regarding threading that may
be useful for you., if the process was working in 1.1 it should work the
same in 2.0, but of course only a test could prove this.
Jan 17 '07 #6

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

Similar topics

8
by: Maloney | last post by:
Exit thread in C#CF. I'm createing a thread and setting a class object for when to exit. Before creating the thread i check if the thread is running, if so set the flag to exit the thread. ...
11
by: Nenad Dobrilovic | last post by:
Hi, I have big problem. I made generic form which can be rendered, and as a result of that action, I get System.Windows.Forms.Form object. Rendering must be done in GUI thread (one which has...
3
by: Andrew Baker | last post by:
OK this has me perplexed, puzzled and bamboozled! I have a remoting service which I displayed a message box in. I then wondered what would happen if a client made a call to the service while the...
13
by: Paul | last post by:
Hi, How do I wait until a thread is finished his job then continue to the original thread? public void main(string args) { Thread t = new Thread(new ThreadStart(DoWork)); t.Start();
6
by: Brian Gideon | last post by:
How have you handled the finalization of thread-specific unmanaged resources? My question pertains specifically to using the DDEML which is a thread-specific API. In other words, every call to...
0
by: puff | last post by:
When interfacing to a COM object, is it possible to pump messages in a thread? I'm working on an application that automates IE and needs to monitor IE events (yes I know about Pamie). I'm able...
17
by: nicolas.hilaire | last post by:
Hi all, i'm doing a quite long treatment in a function, and i'm showing the progress of the treatment with a progressbar. The code is something like this : for each(entry in entries) // about...
14
by: Joe | last post by:
Does anyone know the difference, in practical terms, between Thread.Sleep (10000) and Thread.CurrentThread.Join (10000)?? The MSDN says that with Join, standard COM and SendMessage pumping...
14
by: shark | last post by:
Hi, Does Form.ShowDialog() start new thread ? If yes how is solved cross-thread operations? Thx
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.