473,624 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help with first multi-thread app pls?

This is my first attempt at multi-thread programming, and I'm encountering a
program-logic problem that I can't quite put my finger on.

The program is pretty simple. I'm trying to validate a large list of email
addresses. Since the actual validation can take some time, I'm spawning new
threads, up to a predetermined maximum value, to process each new address.

In my main program I have the following:

Do Until (intThreadCount er < intMaxThreads) 'Wait
until a thread is available

Sleep(500) '1000 milliseconds = 1 second

Loop

oValidate = New validateEmail

'Acquire a Write lock on the resource.

rwl.AcquireWrit erLock(Timeout. Infinite)

Try

intThreadCounte r = intThreadCounte r + 1

Finally

'Release the lock.

t = New Thread(AddressO f
oValidate.Valid ateEmail)

oValidate.objEm ailInfo = objEmailInfo

t.Start()

rwl.ReleaseWrit erLock()

End Try

If I've already reached the maximum thread count, I wait until one is
released. I do not lock at this point, because I have a feeling that locking
that wait loop would incur a deadlock. In any case, this is the only place
where I INCREMENT the intThreadCounte r variable, so I don't think it's an
issue; that is by the time I lock the block to update intThreadCounte r,
there's no chance that it will have been incremented. I start the new thread
inside the lock block because I don't want a situation in which I have
changed the value, but not started the thread.

In my oValidate.Valid ateEmail, I complete my validation, then do:

RaiseEvent ThreadComplete( intReturnVal, objEmailInfo)

Then I have an event handler as follows:

Public Event ThreadComplete( ByVal intStatusReturn As Integer, ByVal
objEmailInfo As cEmailInfo)

Sub ValidateEventHa ndler(ByVal intStatusReturn As Integer, ByVal
objEmailInfo As cEmailInfo) _

Handles oValidate.Threa dComplete

...

'Acquire a Write lock on the resource.

rwl.AcquireWrit erLock(Timeout. Infinite)

Try

If (intThreadCount er > 0) Then

intThreadCounte r = intThreadCounte r - 1

End If

Finally

'Release the lock.

rwl.ReleaseWrit erLock()

Thread.CurrentT hread.Abort()

End Try

--So, I process the data provided by the thread, decrement the thread
counter, and kill the thread, all within the locked block.

Finally, In my main program, I have the following:

Do Until (intThreadCount er = 0)

Sleep(500) '1000 milliseconds = 1 second

Loop

Problem is, intThreadCounte r is never getting down to zero, and I can't
quite figure out why. Any pointers would be appreciated.

Thanks.

Joe


--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.c om<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
Mar 12 '06 #1
3 1463
Joe Befumo wrote:
This is my first attempt at multi-thread programming, and I'm encountering a
program-logic problem that I can't quite put my finger on.

The program is pretty simple. I'm trying to validate a large list of email
addresses. Since the actual validation can take some time, I'm spawning new
threads, up to a predetermined maximum value, to process each new address.

In my main program I have the following:

Do Until (intThreadCount er < intMaxThreads) 'Wait
until a thread is available

Sleep(500) '1000 milliseconds = 1 second

Loop

oValidate = New validateEmail

'Acquire a Write lock on the resource.

rwl.AcquireWrit erLock(Timeout. Infinite)

Try

intThreadCounte r = intThreadCounte r + 1

Finally

'Release the lock.

t = New Thread(AddressO f
oValidate.Valid ateEmail)

oValidate.objEm ailInfo = objEmailInfo

t.Start()

rwl.ReleaseWrit erLock()

End Try

If I've already reached the maximum thread count, I wait until one is
released. I do not lock at this point, because I have a feeling that locking
that wait loop would incur a deadlock. In any case, this is the only place
where I INCREMENT the intThreadCounte r variable, so I don't think it's an
issue; that is by the time I lock the block to update intThreadCounte r,
there's no chance that it will have been incremented. I start the new thread
inside the lock block because I don't want a situation in which I have
changed the value, but not started the thread.

In my oValidate.Valid ateEmail, I complete my validation, then do:

RaiseEvent ThreadComplete( intReturnVal, objEmailInfo)

Then I have an event handler as follows:

Public Event ThreadComplete( ByVal intStatusReturn As Integer, ByVal
objEmailInfo As cEmailInfo)

Sub ValidateEventHa ndler(ByVal intStatusReturn As Integer, ByVal
objEmailInfo As cEmailInfo) _

Handles oValidate.Threa dComplete

..

'Acquire a Write lock on the resource.

rwl.AcquireWrit erLock(Timeout. Infinite)

Try

If (intThreadCount er > 0) Then

intThreadCounte r = intThreadCounte r - 1

End If

Finally

'Release the lock.

rwl.ReleaseWrit erLock()

Thread.CurrentT hread.Abort()

End Try

--So, I process the data provided by the thread, decrement the thread
counter, and kill the thread, all within the locked block.

Finally, In my main program, I have the following:

Do Until (intThreadCount er = 0)

Sleep(500) '1000 milliseconds = 1 second

Loop

Problem is, intThreadCounte r is never getting down to zero, and I can't
quite figure out why. Any pointers would be appreciated.

Thanks.

Joe


--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.c om<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access


You may want to rethink how you are going about the process. Spawning a
thread is not a quick process. You may want to have one UI thread an
one worker thread that processes all your address. If you think about
how the system actually uses these threads, only one can be processing
on the processor at one time, unless you have a multi-processor system.
So the time to create the thread stack, transfer control to the new
thread takes extra time with each new thread. If you process all the
addresses in one worker thread it won't have to swap the overhead for
the thread and it will keep your system logic simpler.

Hope this helps.
Chris
Mar 12 '06 #2
Thanks Chris,
I believe I understand what you're saying.

Joe
"Chris" <no@spam.com> wrote in message
news:ev******** ******@TK2MSFTN GP10.phx.gbl...
Joe Befumo wrote:
This is my first attempt at multi-thread programming, and I'm
encountering a
program-logic problem that I can't quite put my finger on.

The program is pretty simple. I'm trying to validate a large list of
email
addresses. Since the actual validation can take some time, I'm spawning
new
threads, up to a predetermined maximum value, to process each new
address.

In my main program I have the following:

Do Until (intThreadCount er < intMaxThreads) 'Wait
until a thread is available

Sleep(500) '1000 milliseconds = 1 second

Loop

oValidate = New validateEmail

'Acquire a Write lock on the resource.

rwl.AcquireWrit erLock(Timeout. Infinite)

Try

intThreadCounte r = intThreadCounte r + 1

Finally

'Release the lock.

t = New Thread(AddressO f
oValidate.Valid ateEmail)

oValidate.objEm ailInfo = objEmailInfo

t.Start()

rwl.ReleaseWrit erLock()

End Try

If I've already reached the maximum thread count, I wait until one is
released. I do not lock at this point, because I have a feeling that
locking
that wait loop would incur a deadlock. In any case, this is the only
place
where I INCREMENT the intThreadCounte r variable, so I don't think it's an
issue; that is by the time I lock the block to update intThreadCounte r,
there's no chance that it will have been incremented. I start the new
thread
inside the lock block because I don't want a situation in which I have
changed the value, but not started the thread.

In my oValidate.Valid ateEmail, I complete my validation, then do:

RaiseEvent ThreadComplete( intReturnVal, objEmailInfo)

Then I have an event handler as follows:

Public Event ThreadComplete( ByVal intStatusReturn As Integer, ByVal
objEmailInfo As cEmailInfo)

Sub ValidateEventHa ndler(ByVal intStatusReturn As Integer, ByVal
objEmailInfo As cEmailInfo) _

Handles oValidate.Threa dComplete

..

'Acquire a Write lock on the resource.

rwl.AcquireWrit erLock(Timeout. Infinite)

Try

If (intThreadCount er > 0) Then

intThreadCounte r = intThreadCounte r - 1

End If

Finally

'Release the lock.

rwl.ReleaseWrit erLock()

Thread.CurrentT hread.Abort()

End Try

--So, I process the data provided by the thread, decrement the thread
counter, and kill the thread, all within the locked block.

Finally, In my main program, I have the following:

Do Until (intThreadCount er = 0)

Sleep(500) '1000 milliseconds = 1 second

Loop

Problem is, intThreadCounte r is never getting down to zero, and I can't
quite figure out why. Any pointers would be appreciated.

Thanks.

Joe


--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.c om<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access


You may want to rethink how you are going about the process. Spawning a
thread is not a quick process. You may want to have one UI thread an one
worker thread that processes all your address. If you think about how the
system actually uses these threads, only one can be processing on the
processor at one time, unless you have a multi-processor system. So the
time to create the thread stack, transfer control to the new thread takes
extra time with each new thread. If you process all the addresses in one
worker thread it won't have to swap the overhead for the thread and it
will keep your system logic simpler.

Hope this helps.
Chris


--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.c om<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
Mar 12 '06 #3
See:

Resources about asynchronous operations
http://www.mztools.com/resources_net...nousOperations

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio
You can code, design and document much faster:
http://www.mztools.com
"Joe Befumo" <jo*@befumo.com > escribió en el mensaje
news:44******** *************** @news.newsdemon .com...
This is my first attempt at multi-thread programming, and I'm encountering
a
program-logic problem that I can't quite put my finger on.

The program is pretty simple. I'm trying to validate a large list of email
addresses. Since the actual validation can take some time, I'm spawning
new
threads, up to a predetermined maximum value, to process each new address.

In my main program I have the following:

Do Until (intThreadCount er < intMaxThreads) 'Wait
until a thread is available

Sleep(500) '1000 milliseconds = 1 second

Loop

oValidate = New validateEmail

'Acquire a Write lock on the resource.

rwl.AcquireWrit erLock(Timeout. Infinite)

Try

intThreadCounte r = intThreadCounte r + 1

Finally

'Release the lock.

t = New Thread(AddressO f
oValidate.Valid ateEmail)

oValidate.objEm ailInfo = objEmailInfo

t.Start()

rwl.ReleaseWrit erLock()

End Try

If I've already reached the maximum thread count, I wait until one is
released. I do not lock at this point, because I have a feeling that
locking
that wait loop would incur a deadlock. In any case, this is the only place
where I INCREMENT the intThreadCounte r variable, so I don't think it's an
issue; that is by the time I lock the block to update intThreadCounte r,
there's no chance that it will have been incremented. I start the new
thread
inside the lock block because I don't want a situation in which I have
changed the value, but not started the thread.

In my oValidate.Valid ateEmail, I complete my validation, then do:

RaiseEvent ThreadComplete( intReturnVal, objEmailInfo)

Then I have an event handler as follows:

Public Event ThreadComplete( ByVal intStatusReturn As Integer, ByVal
objEmailInfo As cEmailInfo)

Sub ValidateEventHa ndler(ByVal intStatusReturn As Integer, ByVal
objEmailInfo As cEmailInfo) _

Handles oValidate.Threa dComplete

..

'Acquire a Write lock on the resource.

rwl.AcquireWrit erLock(Timeout. Infinite)

Try

If (intThreadCount er > 0) Then

intThreadCounte r = intThreadCounte r - 1

End If

Finally

'Release the lock.

rwl.ReleaseWrit erLock()

Thread.CurrentT hread.Abort()

End Try

--So, I process the data provided by the thread, decrement the thread
counter, and kill the thread, all within the locked block.

Finally, In my main program, I have the following:

Do Until (intThreadCount er = 0)

Sleep(500) '1000 milliseconds = 1 second

Loop

Problem is, intThreadCounte r is never getting down to zero, and I can't
quite figure out why. Any pointers would be appreciated.

Thanks.

Joe


--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.c om<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access

Mar 13 '06 #4

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

Similar topics

7
1895
by: Shane | last post by:
Hi, Thanks in advance for the help. I have been to many websites and tried several solutions to my problem, but have fixed part of it. It's time to come humbly to the newsgroups for help :-) By the way, please don't reply just to tell me that my code isn't optimized and that I should use a delimited database rather than a line by line record. If I had my way, I'd do it differently too.
3
3605
by: Paul T. Rong | last post by:
I have a listbox (of product names) control on my form. I want to pass the selected item (a product name) to a subform, and the product unitprice should apear automatically next to the product name in the subform. Is it possible? How do I do this? Thanks in advance. Paul from Slovakia
1
2721
by: Joe | last post by:
I have the following 3 tables: Clients, which has a numeric PK field called CLIENT_ID Languages, which has a numeric PK field called LANGUAGE_ID Client_Languages, which has a unique PK and foreign key fields CLIENT_ID and LANGUAGE_ID Clients and Languages each have a one-to-many relationship with
1
2055
by: Wade Beasley | last post by:
I have developed a Windows Forms application with C#. On the windows form I have a master list box with the names of people the user can select from and when selected it calls a Select in SQL server to gather the appropriate information. It is then populated onto a multi-tab portion on the right of the screen. On the 3rd tab I have 2 multi-select listboxes. The problem is on the first time I go to the tab for the first name the 2...
11
1416
by: MOOVBUFF | last post by:
I've been looking (wasting my time) for simple solutions such as Updating a database in VS 2005 and all I get is either no answer or another question. Obviously the people in the newsgroup can't read. At least the people in the VB 6.0 newsgroup answer without throwing in an insult. Good Ridance!
5
2520
by: Olly | last post by:
Hello Everyone! Could someone please have a look at my JS Form I posted below....Something wrong there, but I don't understand what's exactly. Many thanks. Olly =============================== <script language="JavaScript">
0
1522
by: Kevin McKinley | last post by:
Below i've put the code for a program that i wrote. I need help on lines 384-403. If you run this program you will notice on the first tab when have it produce an answer the $ is surrounded with {$}. How can i get rid of that? from Tkinter import * class MyApp: def __init__(self, parent): self.myparent = parent
0
1498
by: Guilherme Polo | last post by:
On Wed, Sep 3, 2008 at 8:57 PM, Kevin McKinley <kem1723@yahoo.comwrote: Come on.. "help on lines 384-403", that is not a good way to look for help. You are supposed to post some minimal code that demonstrates the problem. Anyway, this demonstrates what you are getting (independent of python version): import Tkinter
3
5044
JodiPhillips
by: JodiPhillips | last post by:
Hello everyone, there are many questions and answers relating to moving items between two listboxes here and on the net in general, however, none answer my specific problem. I have two listboxes on a form. The first listbox is populated according to command buttons (Command 14 & Command 15) that are clicked by the user (draws data via SQL statement - see code below). The second listbox is populated by user selection from the first listbox. I...
5
4932
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums there are, it crashes. There are currently 6 columns, and I only want 4. How do I remove the last two (discount and date)? Here is a link: http://www.jaredmoore.com/tablesorter/docs/salestable.html Here is some jquery js that I think...
0
8238
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
8680
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
8624
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...
1
8336
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6111
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
5565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4082
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
1786
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1485
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.