Page 1 of 1
How to saftely call a thread with several parameters
Posted: Tue Feb 09, 2010 9:53 am
by Joakim Christiansen
EDIT: Fred improved it here:
http://www.purebasic.fr/english/viewtop ... 37#p315518
Using this method you call the thread like a normal procedure and does not have to worry about a thing.
Code: Select all
EnableExplicit
Structure someProcedure_parameters
helloWorld$
number.l
isRead.l
EndStructure
Procedure _someProcedure(*parameters.someProcedure_parameters) ;the thread
Protected helloWorld$, number
helloWorld$ = *parameters\helloWorld$
number = *parameters\number
*parameters\isRead = #True ;tell it that it now can delete the memory area we read from
Delay(1000) ;wait a second (just too prove that it works)
Debug "But we did get the parameters:"
Debug helloWorld$
Debug number
EndProcedure
Procedure someProcedure(helloWorld$, number) ;the thread caller (so you can use it as a normal procedure almost)
Protected parameters.someProcedure_parameters ;create the structure containing the parameters
parameters\helloWorld$ = helloWorld$
parameters\number = number
parameters\isRead = #False
CreateThread(@_someProcedure(),@parameters) ;send the thread a pointer to our structure
Repeat
Until parameters\isRead ;do not quit (delete the structure) before it has read it
EndProcedure
someProcedure("Hello World!",123456)
Debug "Thread caller has now ended."
Delay(2000)
Re: How to saftely call a thread with several parameters
Posted: Wed Feb 10, 2010 9:52 pm
by idle
Good tip
should be something in the help docs about passing multiple parameters into a thread.
Re: How to saftely call a thread with several parameters
Posted: Wed Feb 10, 2010 10:57 pm
by Fred
Better use a dynamic memory block, so you don't need a CPU hog loop:
Code: Select all
EnableExplicit
Structure someProcedure_parameters
helloWorld$
number.l
EndStructure
Procedure _someProcedure(*parameters.someProcedure_parameters) ;the thread
Protected helloWorld$, number
helloWorld$ = *parameters\helloWorld$
number = *parameters\number
ClearStructure(*parameters, someProcedure_parameters)
FreeMemory(*parameters)
Delay(1000) ;wait a second (just too prove that it works)
Debug "But we did get the parameters:"
Debug helloWorld$
Debug number
EndProcedure
Procedure someProcedure(helloWorld$, number) ;the thread caller (so you can use it as a normal procedure almost)
Protected *parameters.someProcedure_parameters ;create the structure containing the parameters
*parameters = AllocateMemory(SizeOf(someProcedure_parameters))
*parameters\helloWorld$ = helloWorld$
*parameters\number = number
CreateThread(@_someProcedure(), *parameters) ;send the thread a pointer to our structure
EndProcedure
someProcedure("Hello World!",123456)
Debug "Thread caller has now ended."
Delay(2000)
Re: How to saftely call a thread with several parameters
Posted: Thu Feb 11, 2010 1:52 am
by byo
Wow, this is a really useful snippet!
Maybe Andre could add it to the CodeArchiv?
Everyday reading this forum I see my mind expanding...

Re: How to saftely call a thread with several parameters
Posted: Thu Feb 11, 2010 3:54 am
by SFSxOI
WoW! I wish i'df had this several months ago for a project, would have solved a buncha problems. Thank You

Re: How to saftely call a thread with several parameters
Posted: Thu Feb 11, 2010 9:43 am
by Joakim Christiansen
Thanks Fred! I didn't think of that!
And... ClearStructure() is not documented in the help file Fred
EDIT: Well, actually it is, but hitting F1 on it doesn't work.
should be something in the help docs about passing multiple parameters into a thread
Agreed.
Re: How to saftely call a thread with several parameters
Posted: Fri Feb 12, 2010 6:52 am
by idle
Yes it really should be documented as it's not really that obvious took me a few months to realize it could be done.
Re: How to saftely call a thread with several parameters
Posted: Fri Feb 12, 2010 9:58 am
by Fred
Added to doc.
Re: How to saftely call a thread with several parameters
Posted: Fri Feb 12, 2010 3:12 pm
by SFSxOI
Not intending to get off topic, but its sort of on topic in a way. The help says this:
"ClearStructure free the memory of a structured memory area. This is useful when the structure contains strings which have been allocated internally by PureBasic. 'Structure' is the name of the structure which should be used to perform the clearing. There is no internal check to ensures than the structure match the memory area. This function is for advanced users only and should be used with care."
This part in particular : " This function is for advanced users only and should be used with care."
What does that mean? Is there an example of when it would not be applicapable to use?
Re: How to saftely call a thread with several parameters
Posted: Fri Feb 12, 2010 3:36 pm
by Joakim Christiansen
SFSxOI wrote:This part in particular : " This function is for advanced users only and should be used with care."
What does that mean? Is there an example of when it would not be applicapable to use?
There is no internal check to ensures that the structure match the memory area.
Code: Select all
ClearStructure(*Address,Structure)
Since the memory at the address pointed to will be cleared it can easily cause a problem.
The structure parameter let it know how much memory to clear, so if you give it the wrong information there it could overwrite memory it shouldn't. And therefore it should only be used by users who knows what they are doing I guess.
Re: How to saftely call a thread with several parameters
Posted: Fri Feb 12, 2010 4:14 pm
by SFSxOI
Ahhhh...OK, i can see that happening. Thank You

Re: How to saftely call a thread with several parameters
Posted: Sun Feb 14, 2010 10:16 am
by Rescator
Maybe a CreatePBThread() or similar could be doe in the future?