Solved

Single Threading Vs MultiThreading

Posted on 2002-03-31
8
477 Views
Last Modified: 2008-02-01
When should I use single threading over multithreading and vice versa?

I understand the performance issues caused the the need for marhaling while using both single and multithreading but not when I should choose one over the other outside of this.
0
Comment
Question by:Lisp
8 Comments
 
LVL 2

Expert Comment

by:nahumd
Comment Utility
There is no straight forward way of implementing multithreading in vb (unless you're talking about .net).
Therefore, unless you have a very special need that will make it worth the big effort - you should use single threading.
0
 

Author Comment

by:Lisp
Comment Utility
OK but can you give me an example of such a big need?  The question was asking when do I need to use each of these.  I am attempting to prepare for an exam and need to know the answer.

Thanks.
0
 
LVL 17

Expert Comment

by:inthedark
Comment Utility
This is not really a VB issue as it applies to any language.   So if you were asking, “why would you want to use multi-threaded tasks?” there are hundreds of examples.  In simple terms: “multithreading should be used to increase throughput.”

Here are the issues to consider:

1) Handling of a multithreaded environment requires additional processing and storage resources.  If a task can complete in less time than the management overhead of the multithreading then throughput would be reduced. In otherworlds don’t use a sledgehammer to crack a nut.
2) Multithreading should be considered to reduce bottlenecks where slow processes are linking to faster processes.
3) There are always exceptions to every rule.
4) The multithreading control should always be aware of its processing limitations (or bandwidth limitations).
5) Multithreading can take better take advantage of resource pools so a slow process can be re-tasked to a different server.

Examples:


1) Bulk Email – Use Multi-threading

A bulk email program can send an email every six seconds.  But when it hits an address where the responding server is slow or just not there, network retires could mean that it could take 30 seconds to decide that and address was bad. By multi-threading the process better throughput can be achieved.  But the cute trick is to monitor the bandwidth so before opening up another thread the thread management should ping an up-line gateway to gauge the response time.  If the response is greater than a preset limit the management should reduce currently operating threads.  So by running perhaps 20 threads you could send perhaps 3 emails per second and the odd slow connection would not stop the ball rolling. The exception - in a LAN environment, where all recipients are local, a multi-threading bulk email may not be so desirable as it may bring the entire LAN to a standstill until the job was complete.

2) Web Server – Use Multi-threading

Reading from a resource is an ideal case for multi-threading.

3) Record Updates – Caution

In a data update situation there is a much more reduced case for multithreading as each thread needs a one-to-one relationship with the resource. Lock the data – update the record – free the lock.

Hope this helps…..

P.S. In VB you can achieve multi-threading in VB6 by using separate exe processes, controlled by a process control exe.  But VB6 is not so good for creating com+ services that run on a server, in this case to would need to use C++ or VB.Net.
0
 
LVL 100

Expert Comment

by:mlmcc
Comment Utility
listening
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Lisp
Comment Utility
Thanks InTheDark.  The points are yours.  For an extra 100 points will you consider answering this for me?


If I have an application that has multiple add-ins connecting to it should the core application be multi-threaded?

Should the addins also be multithreaded to avoid marshaling or will I achieve a 1 to many relationship between add-ins and the core program by processing the addins in a single threadspace withing the core application if I have single threaded add-ins accompanied by a mulithreaded core program?
0
 
LVL 17

Expert Comment

by:inthedark
Comment Utility
>If I have an application that has multiple add-ins connecting to it should the core application be multi-threaded?

Again this depends on the task. Suggest you start by following these links, then we can discuss them later:

http://support.microsoft.com/default.aspx?scid=kb;EN-US;q243548

http://www.desaware.com/articles/threadingL3.htm
0
 
LVL 17

Accepted Solution

by:
inthedark earned 200 total points
Comment Utility
>If I have an application that has multiple add-ins connecting to it should the core application be
multi-threaded?

On the face of it would be tempting to say yes. But remember the overhead of threading so the answer is it depends on the tasks.  If the tasks are mainly CPU bound operations then multi-threading will be a waste of time.  If some of the tasks will be resource bound (waiting for a database/another server to bring back data for example), then multi-threading could be an option. Furthermore, if a job is waiting for another operation to complete any marshalling overheads less relevant.
0
 
LVL 17

Expert Comment

by:inthedark
Comment Utility
>Should the addins also be multithreaded to avoid marshaling

If you read the dessaware article I think that it proposes that marshaling takes place when an object is defined on one thread but passed to another thread.  Before the object can be processed the calling thread has to halt the first source thread, pass orver the parameters, execute the function, then copy the results/object back to the calling thread.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now