?
Solved

VB6: Multithreading and Multiple Core CPU

Posted on 2008-02-07
5
Medium Priority
?
3,257 Views
Last Modified: 2013-11-25
Hi!

For some time now I've been working on implementing multiple threads on VB6 with a standard EXE.  I think I got it working right now, so I'm trying to get a little more performance out of my app.  I've noticed that both threads (only two right now) run on the same core, or at least it seems that way in the Performance tab of Task Manager.  Is there a way (API?) to control which core runs which thread?  Maybe this could be applied to a system with multiple CPUs too, but I'm not sure.

Thanks!
TCV
0
Comment
Question by:Taconvino
  • 3
5 Comments
 
LVL 15

Assisted Solution

by:bpmurray
bpmurray earned 800 total points
ID: 20842099
What you're looking for is a way to create a SMP app using VB. I doubt very much that you're going to be able to do that. While compilers are now available for C and C++ that can optimize for multiple processors (as you mentioned, this is exactly what multi-core processors are), doing this in VB in its current form is pretty difficult. Search around for articles and books on Parallel Computing for more information on this stuff.
0
 
LVL 6

Author Comment

by:Taconvino
ID: 20842218
Yeah, I think a compiler solution makes sense.  I wasn't really expecting it could be done, considering how old VB6 core is... I'll just leave this question hanging for a while to see if someone can come up with a hack or something.  It has happened before!  :-)

Thanks!
TCV
0
 
LVL 10

Accepted Solution

by:
3_S earned 1200 total points
ID: 20886066
I'm not familiar with using threads in VB6, but I put the complete program on to another cpu

There also a setthreadaffinitymask
So you could look into the api's getCurrentThread and SetThreadAffinityMask but for that you are on your own. I can't help you with that


Public Declare Function GetCurrentProcess Lib "kernel32.dll" () As Long
Public Declare Function SetProcessAffinityMask Lib "kernel32.dll" (ByVal hProcess As Long, ByVal dwProcessAffinityMask As Long) As Long
 
'*-*-*-*-*-*-
 
Call SetProcessAffinityMask(GetCurrentProcess(), &H1)  'complete program runs on cpu 1  
or
Call SetProcessAffinityMask(GetCurrentProcess(), &H2)  'complete runs on cpu 2

Open in new window

0
 
LVL 6

Author Comment

by:Taconvino
ID: 20886295
Sounds promising.  The thing is it applies to processes, not threads.  But that got me thinking: could there be a SetThreadAffinityMask?  And yes, there is one!  Reading about this function made me realize that maybe to "hand assign" a thread to run on a specific processor may not be as good as I thought.  From the MSDN article:

"Setting an affinity mask for a process or thread can result in threads receiving less processor time, as the system is restricted from running the threads on certain processors. In most cases, it is better to let the system select an available processor."

Not that good.  But there's yet another function, called SetThreadIdealProcessor.  The thing is, for what I can tell, I might as well let the OS choose the "ideal processor" for me instead of using this API.  So the thing is, I don't think it's such a good idea to hand-pick the processor after all.

This can still be open for discussion a little bit more, just in case.

Thanks!
TCV
0
 
LVL 6

Author Comment

by:Taconvino
ID: 20922445
OK, so there may be a way to do it, but it is hardly a solution for what I need.  This was a mistake from my part, and anyway, the information provided was excelent.

Thanks!
TCV
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

601 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