473,387 Members | 1,485 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,387 software developers and data experts.

Make multithreading do only X threads at a time.

I have a long function that needs to be done 1000 times. I'm
multithreading it, but I don't want to load them up all at once, instead
load them 10 at a time.

So far the only way I can get it to work is by creating a dummy form
with a timer. On the timer function I test if the number of threads are
less than 10 then run the remaining ones. This is working fine, but I
would like to know how to do it without the form.

This is the code I'm trying to use to accomplish the feat without the form:

Module mdlAny()
Public Sub Main()
Do
FunctionThatCalls10Threads()

PauseThisThing() 'Replace this with
'either of the bottom functions
Loop
End Sub

Private Sub PauseThisThingThatWorks()
'It works with this code:
'On the Timer event, I call
'the FunctionThatChecksTheNumberOfThreads()
'to see if I can close the form.
'(and hence continue with the program)

Dim wf As WaitForm = New WaitForm
wf.ShowDialog()
End Sub

Private Sub PauseThisThingThatDOESNOTWork()
'It doesn't work with this code for waiting

Dim RetValue as Boolean
Do
System.Threading.Thread.CurrentThread.Sleep(1000)
RetValue = FunctionThatChecksTheNumberOfThreads()
Loop Until RetValue
End Sub
End Module

.................................................. ...............
Posted via TITANnews - Uncensored Newsgroups Access
at http://www.TitanNews.com <<<<

-=Every Newsgroup - Anonymous, UNCENSORED, BROADBAND Downloads=-

Nov 20 '05 #1
4 1707
On Thu, 27 May 2004 21:40:15 -0400, Manuel wrote:
I have a long function that needs to be done 1000 times. I'm
multithreading it, but I don't want to load them up all at once, instead
load them 10 at a time.

So far the only way I can get it to work is by creating a dummy form
with a timer. On the timer function I test if the number of threads are
less than 10 then run the remaining ones. This is working fine, but I
would like to know how to do it without the form.

This is the code I'm trying to use to accomplish the feat without the form:

Module mdlAny()
Public Sub Main()
Do
FunctionThatCalls10Threads()

PauseThisThing() 'Replace this with
'either of the bottom functions
Loop
End Sub

Private Sub PauseThisThingThatWorks()
'It works with this code:
'On the Timer event, I call
'the FunctionThatChecksTheNumberOfThreads()
'to see if I can close the form.
'(and hence continue with the program)

Dim wf As WaitForm = New WaitForm
wf.ShowDialog()
End Sub

Private Sub PauseThisThingThatDOESNOTWork()
'It doesn't work with this code for waiting

Dim RetValue as Boolean
Do
System.Threading.Thread.CurrentThread.Sleep(1000)
RetValue = FunctionThatChecksTheNumberOfThreads()
Loop Until RetValue
End Sub
End Module

.................................................. ..............
Posted via TITANnews - Uncensored Newsgroups Access
>>>> at http://www.TitanNews.com <<<<

-=Every Newsgroup - Anonymous, UNCENSORED, BROADBAND Downloads=-


' Dummy code, in a fictious Console application
Option Strict On
Option Explicit On

Imports System
Imports System.Threading

Module modMain
Private Const MAX_THREADS As Integer = 9
Private threads(MAX_THREADS) As Thread

' just so we can get the system thread handle :)
Private Declare Function GetCurrentThreadId Lib "kernel32" () As IntPtr

Public Sub Main()
Dim rnd As New Random

For i As Integer = 0 To MAX_THREADS
threads(i) = New Thread(AddressOf Run)

' randomize the wait...
Thread.Sleep(rnd.Next(100, 1000))

threads(i).Start()
Next

Console.ReadLine()

For i As Integer = 0 To MAX_THREADS
threads(i).Abort()
Next

Console.ReadLine()
End Sub

Private Sub Run()
Dim i As Integer = 0
Dim id As Integer = GetCurrentThreadId().ToInt32()

Try
Do
i += 1
Console.WriteLine("ThreadId {0}: i={1}", _
id, i)
Thread.Sleep(1000)
Loop
Catch ex As ThreadAbortException
Console.WriteLine("ThreadID {0} Aborted!", id)
End Try
End Sub
End Module

I'm not sure if this is what you wanted... But maybe it will help you :)
--
Tom Shelton [MVP]
Nov 20 '05 #2
Tom Shelton wrote:

I'm not sure if this is what you wanted... But maybe it will help you :)


Not quite. What you are doing is starting the threads every X amount of
time, where X is a random number.

I want to be able to run 10 threads and then wait until less than 10 are
finished and load 10 more, rinse and repeat...

Thanks

.................................................. ...............
Posted via TITANnews - Uncensored Newsgroups Access
at http://www.TitanNews.com <<<<

-=Every Newsgroup - Anonymous, UNCENSORED, BROADBAND Downloads=-

Nov 20 '05 #3
Manuel,
It sounds like you simply want to use the System.Threading.ThreadPool or
roll your own Thread Pool.

System.Threading.ThreadPool allows by default 25 threads per processor. To
use it you would simply use ThreadPool.QueueUserWorkItem 1000 times with the
same function address... See System.Threading.ThreadPool for details...

Alternatively if you want to limit it to 10 threads, you will need to write
your own Thread Pool. I would create a new Thread Pool class that started 10
Threads. My Thread Pool class would have a System.Collections.Queue object
that represented the requests. Each thread would dequeue an item and work on
it. I would have a special request or other mechanism available to tell each
thread that it is time to exit. I would also include a single
AutoResetEvent, that each thread would wait on to see if an item is in the
Queue. Plus there should be a padlock object (I normally use "New Object")
that you can SyncLock on to ensure that reading & writing to the queue is
properly synchronized.

Writing your own thread pool will not be as easy as I am making it sound,
however it is fairly easy! The above should be enough to get you started,
alternatively you could search google for a sample. Tom's code may help you
get started...

Note I've done the above & posted to the newsgroup my alternative above
using a single thread.

Hope this helps
Jay
"Manuel" <Ma*@may.co> wrote in message
news:40***********************@news.titannews.com. ..
I have a long function that needs to be done 1000 times. I'm
multithreading it, but I don't want to load them up all at once, instead
load them 10 at a time.

So far the only way I can get it to work is by creating a dummy form
with a timer. On the timer function I test if the number of threads are
less than 10 then run the remaining ones. This is working fine, but I
would like to know how to do it without the form.

This is the code I'm trying to use to accomplish the feat without the form:
Module mdlAny()
Public Sub Main()
Do
FunctionThatCalls10Threads()

PauseThisThing() 'Replace this with
'either of the bottom functions
Loop
End Sub

Private Sub PauseThisThingThatWorks()
'It works with this code:
'On the Timer event, I call
'the FunctionThatChecksTheNumberOfThreads()
'to see if I can close the form.
'(and hence continue with the program)

Dim wf As WaitForm = New WaitForm
wf.ShowDialog()
End Sub

Private Sub PauseThisThingThatDOESNOTWork()
'It doesn't work with this code for waiting

Dim RetValue as Boolean
Do
System.Threading.Thread.CurrentThread.Sleep(1000)
RetValue = FunctionThatChecksTheNumberOfThreads()
Loop Until RetValue
End Sub
End Module

.................................................. ..............
Posted via TITANnews - Uncensored Newsgroups Access
>>>> at http://www.TitanNews.com <<<<

-=Every Newsgroup - Anonymous, UNCENSORED, BROADBAND Downloads=-

Nov 20 '05 #4
Manuel,
In addition to my other comments.

The reason I suggested a thread pool (either the built in one, or roll your
own).

Is that starting & stopping 1000 threads, 10 at a time is expensive. It's
generally better to start 10 threads and let each thread process 100
requests. A Thread pool normally has a single queue of requests when a
thread is done working on a request it simply gets the next request in order
& processes it...

Using the ISynchronizeInvoke interface you could setup the ThreadPool so
that it was able to raise an event on your main Thread (your UI thread) to
notify it when all the requests were done... (The thread pool object would
have an ISynchronizeInvoke variable, when the last request finished it would
use ISynchronizeInvoke.Invoke to invoke a delegate, that raised an event.
The ISynchronizeInvoke variable would hold an instance of your form, causing
ISynchronizeInvoke.Invoke to run on the UI thread. Using ISynchronizeInvoke
to raise the event would mean the main form would not need to poll to see if
all the requests were done or not...

Hope this helps
Jay

"Manuel" <Ma*@may.co> wrote in message
news:40***********************@news.titannews.com. ..
Tom Shelton wrote:

I'm not sure if this is what you wanted... But maybe it will help you :)


Not quite. What you are doing is starting the threads every X amount of
time, where X is a random number.

I want to be able to run 10 threads and then wait until less than 10 are
finished and load 10 more, rinse and repeat...

Thanks

.................................................. ..............
Posted via TITANnews - Uncensored Newsgroups Access
>>>> at http://www.TitanNews.com <<<<

-=Every Newsgroup - Anonymous, UNCENSORED, BROADBAND Downloads=-

Nov 20 '05 #5

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

Similar topics

10
by: gianguz | last post by:
The question is about the possible use of inlining to improve performance in a heavy multithreading environment (200+ threads). If we have to work with applications in which threads aren't I/O...
47
by: mihai | last post by:
What does the standard say about those two? Is any assurance that the use of STL is thread safe? Have a nice day, Mihai.
11
by: Mark Yudkin | last post by:
The documentation is unclear (at least to me) on the permissibility of accessing DB2 (8.1.5) concurrently on and from Windows 2000 / XP / 2003, with separate transactions scope, from separate...
16
by: Robert Zurer | last post by:
Can anyone suggest the best book or part of a book on this subject. I'm looking for an in-depth treatment with examples in C# TIA Robert Zurer robert@zurer.com
5
by: Yuliaan | last post by:
Hi everyone! In program I wrote, I try to run 300 threads, when only 10 is running in the same time. I use array to create 300 threads, but first I start only 10, after that I add one thread when...
5
by: sarge | last post by:
I would like to know how to perform simple multithreading. I had created a simple form to test out if I was multithreading properly, but got buggy results. Sometime the whole thig would lock up...
16
by: who be dat? | last post by:
Consider the following code which enables multithreading in an ASP.Net application I'm writing: Code in global.asx Application_start subroutine, Threadnotify is declared Globally as a new thread...
2
by: Pradnya Patil | last post by:
hi , I am trying to draw ' html div-tag ' on the screen which will resemble a rectangle through vb.net code. I want it to be drawn faster...so I introduced multithreading using Threadpool. I...
0
luke14free
by: luke14free | last post by:
Hello, I was trying to run a multithreading example with python on my phone that lets me to record and play at the same time.I've found an example somewhere but, as expected it doesnt work,,,could...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
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...

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.