Personally i am amzed of this topic: Multi-threading. Means, that you use multiple cores of a Multicore CPU.
This post should help you to get the biggest obstacles out of the way.
Besides my tips, any more tips from anybody who has tried this and has own experiences are welcome here.
Lets face the problems.
Imagine a company. You're the boss, you read the mail, you take the telephone.
This is a one man company. In software, its a single thread program or just 99% of all programs.
The Advantage: Everybody knows what everybody is doing. Because in this office you are Everybody.
You can handle things relatively fast.
Every action is done after the action before was finished. You don't do two things at once - can you?
But then things change. Everybody is you and ... more people!
In Software thats "Multi-Threading" - means "Multiple Actions at the same time".
First you have to get Employes, means: get some other people to work in your company, at the same time while you work.
All at the same time. But hopefully not at the same Jobs.
This will theoretically safe you work, but on the other hand there will be some questions to solve before you can start.
0. How do you get all those people into your office, to enable them to access the shared resources (Mails, telephone, customer files, products etc.).
Means: How do you tell the computer to share a Job between Threads?
1. Which of you all does what of the overall work?
Means: WHich Thread does what Job?
2. Who calls that customer and how can you check it was already done before you would do it?
Means: How can you safely mark Jobs as Done, preventing multipe threads doing the same thing more then once?
3. Who takes the phone if it rings?
Means: Five threads waiting for a Job - who takes it first if it comes in (Priorities?)?
4. Who reads the mails from customers and answers them (it wouldn't make sense to let it two people do at the same time).
Means: Can you share different Jobs?
But the first problem is, how to get all those people into your office.
In Terms of software the question is, how can the Data be organized that all threads can access it?
Here's first an example of two threads, both threads are able to access Data, Properties, Methods etc. from the same Object.
'---------------------------------------------------------------------------------------------------
THREAD FUNCTION Checker_01(BYVAL T01 AS DWORD) AS LONG
REGISTER R01 AS LONG
LOCAL C01 AS VLB_Interface
' Here we get Access to our Object!
POKE DWORD, VARPTR(C01),T01
C01.AddRef
FOR R01=2 TO 1000 STEP 2
' Use your own Debug-Print here
#DEBUG PRINT "Doing Job: "+STR$(R01)
' You can access all Methods and Properties of the Object in this thread now, via
' C01.Dosomething()
SLEEP 10
NEXT R01
FUNCTION
END FUNCTION
'---------------------------------------------------------------------------------------------------
FUNCTION PBMAIN () AS LONG
LOCAL S01 AS STRING
REGISTER R01 AS LONG
LOCAL Q01 AS QUAD
LOCAL C01 AS VLB_Interface
C01=CLASS "VLB"
THREAD CREATE Checker_01(OBJPTR(C01)) TO R01
TIX Q01
FOR R01=1 TO 1000 STEP 2
'#DEBUG PRINT "Doing Job: "+STR$(R01)
' Use your own Debug-Print here
X_AU "Doing Job: "+STR$(R01)
SLEEP 10
NEXT R01
TIX END Q01
#DEBUG PRINT "Used time: "+STR$(Q01/1e6)
THREAD CLOSE R01 TO R01
? "!!"
EXIT FUNCTION
Now I'll add up some more Infos to thread synchronization.
This will help you to overcome most of the obstacles to writing multithreaded Apps (working on large amounts of data)!
How do we prevent two thread from working at the same time at the same data?
In your CLASS Definition, you place this one:
INSTANCE CS AS CRITICAL_SECTION
And now you can really benefit from the
CLASS METHOD CREATE()
InitializeCriticalSection(CS)
END METHOD
and the
CLASS METHOD DESTROY()
DeleteCriticalSection(CS)
END METHOD
because this will simplify the Initialization of a needed "Critical Section".
Take therefore a look on these API's, these will define Parts of your code as parts that can be entered by one thread at a time only.
Just like a Telephone, which can only be picked by one person at a time!
EnterCriticalSection(CS) ' Wait until CS is free
LeaveCriticalSection(CS) ' Leave CS
TryEnterCriticalSection(CS) ' Return zero if its not free or nonzero if its succesfully entered
And also:
WaitForMultipleObjects nThread, BYVAL VARPTR (hThread(0)), %TRUE, %INFINITE
More tips and example code from you all is welcome here.
If this discussion finds some friends, i will even think of making a "Multithreading Board".
Let's see if you also like this theme, like me.
Oh ... here is an interesting Link from Jose:
COM Threading Models (http://msdn.microsoft.com/en-us/library/ms809971.aspx)
@Dr. Jose, will I get a "Dr." if i have understand all of this? :-)