How to saftely call a thread with several parameters

Share your advanced PureBasic knowledge/code with the community.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

How to saftely call a thread with several parameters

Post 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)
Last edited by Joakim Christiansen on Thu Feb 11, 2010 9:41 am, edited 3 times in total.
I like logic, hence I dislike humans but love computers.
User avatar
idle
Always Here
Always Here
Posts: 5094
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: How to saftely call a thread with several parameters

Post by idle »

Good tip
should be something in the help docs about passing multiple parameters into a thread.
Fred
Administrator
Administrator
Posts: 16681
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: How to saftely call a thread with several parameters

Post 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)
byo
Enthusiast
Enthusiast
Posts: 635
Joined: Mon Apr 02, 2007 1:43 am
Location: Brazil

Re: How to saftely call a thread with several parameters

Post 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... :lol:
Proud registered Purebasic user.
Because programming should be fun.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: How to saftely call a thread with several parameters

Post by SFSxOI »

WoW! I wish i'df had this several months ago for a project, would have solved a buncha problems. Thank You :)
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Re: How to saftely call a thread with several parameters

Post by Joakim Christiansen »

Thanks Fred! I didn't think of that!
And... ClearStructure() is not documented in the help file Fred :shock:
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.
I like logic, hence I dislike humans but love computers.
User avatar
idle
Always Here
Always Here
Posts: 5094
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: How to saftely call a thread with several parameters

Post 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.
Fred
Administrator
Administrator
Posts: 16681
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: How to saftely call a thread with several parameters

Post by Fred »

Added to doc.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: How to saftely call a thread with several parameters

Post 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?
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
User avatar
Joakim Christiansen
Addict
Addict
Posts: 2452
Joined: Wed Dec 22, 2004 4:12 pm
Location: Norway
Contact:

Re: How to saftely call a thread with several parameters

Post 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.
I like logic, hence I dislike humans but love computers.
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Re: How to saftely call a thread with several parameters

Post by SFSxOI »

Ahhhh...OK, i can see that happening. Thank You :)
The advantage of a 64 bit operating system over a 32 bit operating system comes down to only being twice the headache.
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Re: How to saftely call a thread with several parameters

Post by Rescator »

Maybe a CreatePBThread() or similar could be doe in the future?
Post Reply