Page 1 of 1
Posted: Fri Aug 02, 2002 7:07 pm
by BackupUser
Restored from previous forum. Originally posted by ricardo.
Maybe my question are too basic, but i want to know if there is a good solution.
How do i creat a Thread for a Procedure that has some return?:
Result.s = ParseString()
Using some Shared var to stores the result?
Maybe there is some better way to do it and my brain dont get it.
I have tested one solution that behaves very strange but i dont understand it well:
Procedure.s Probando()
ProcedureReturn "bien"
EndProcedure
result$ = "-"
ThreadID = CreateThread(@Probando(), result$)
MessageRequester("",result$,0)
This example does the job, no matter that the parameter on CreateThread function is suppoused to be used as sending parameters and NOT to obtain the results (thats what i understand on the help file).
Ok, but if i make any small change to the example, it dosent works anymore:
Procedure.s Probando1()
ProcedureReturn "bien"
EndProcedure
result$ = "-"
ThreadID = CreateThread(@Probando1(), result$)
MessageRequester("",result$,0)
In fact, sometimes even the first way dosent work at all, but i swear that sometimes it do works. If i make small changes and compile it sometimes it works... strange.
Thanks in advance.
Posted: Fri Aug 02, 2002 7:33 pm
by BackupUser
Restored from previous forum. Originally posted by freak.
I don't quite understand, what this should be good for.
Usually, a new Thread is created, to do two things at the same time, but if
the main prog waits for a result from the thread, why don't you
just call it as a procedure then?
IF you want to communicate between threads, use Global.
I think there's no other solution, becourse that wouldn't make any sense (at least to me, it wouldn't make any sense, maybe it does to you?)
Timo
--
If it stinks, it's chemistry. If it moves, it's biology.
If it does not work, It's computer science.
Posted: Fri Aug 02, 2002 7:43 pm
by BackupUser
Restored from previous forum. Originally posted by Pupil.
If you read the manual you'll see that it says that the threaded procedure MUST have this format:
Procedure MyProcedure(Value)
...
EndProcedure
Why your procedure sometimes seamingly returns a value i'm not quite sure of, it has perhaps to do with how arguments are passed to a procedure using the stack, anyway Fred and Danilo would know that stuff for sure i think..
The best way to pass a value from a thread to the rest of the program is through the use of a global variable, i've done that in some of my apps and it works pretty good..
Posted: Fri Aug 02, 2002 7:51 pm
by BackupUser
Restored from previous forum. Originally posted by ricardo.
I don't quite understand, what this should be good for.
Usually, a new Thread is created, to do two things at the same time, but if
the main prog waits for a result from the thread, why don't you
just call it as a procedure then?.
Because i want my app to proccess multiple jobs at the same time, every one indenpendient from others.
Posted: Fri Aug 02, 2002 8:12 pm
by BackupUser
Restored from previous forum. Originally posted by ricardo.
I don't quite understand, what this should be good for.
Usually, a new Thread is created, to do two things at the same time, but if
the main prog waits for a result from the thread, why don't you
just call it as a procedure then?.
Thats exactly the point that im looking for... how to run a thread and get a return without waiting for the result.
I think that having the result on a structure
Structure
result.s
Id.l
EndStructure
Then i could check often during my main app when a Thread (with is own ID for every process) gives me back some result.
Im not sure if this is the best way, thats why im asking for help.
The idea is this one:
My server (the app thats will be able to do all this stuff) are monitoring permanently and everytime its receives some petition it search on a database and give the result to the client who makes the petition.
But i need to make it work in a way that can run every petition as a threat to let my app manage multiple petitions at the same time for several clients.
Hope that my brief descriptions explains my point.
Thanks.
Posted: Fri Aug 02, 2002 10:41 pm
by BackupUser
Restored from previous forum. Originally posted by freak.
OK, I got that now.
First, what the Manual talks about is passing a Value to the Thread,
not getting it from the Thread.
A Thread returning a Value doesn't make sense, becourse you never know, when it ends. That's the reason this is not built in, becourse where should you return the values to, if you don't know, when this will happen? Got my point?
You'll have to do the checks at your own. The best way is using Globals
Define your Structure and than just use 'Global Result.MyStruct' or something alike at start, so every Thread (and the main Thread) can access it, and
comunicate with each other.
I really don't know, why it worked for you some times, but i is not supposed to
do so.
That's it, hope it helped...
Timo
--
If it stinks, it's chemistry. If it moves, it's biology.
If it does not work, It's computer science.
Posted: Fri Aug 02, 2002 10:45 pm
by BackupUser
Restored from previous forum. Originally posted by freak.
One more thing...
> Im not sure if this is the best way, thats why im asking for help.
Using Globals is not a workaround, or 'way' of doing it, it's the usual way
to do it. Every multi-Thread-app does it that way.
Just to get that clear...
Timo
Posted: Sat Aug 03, 2002 12:07 am
by BackupUser
Restored from previous forum. Originally posted by ricardo.
Using Globals is not a workaround, or 'way' of doing it, it's the usual way
to do it. Every multi-Thread-app does it that way.
Just to get that clear...
Ok, Thats what i want to know. Because i could imagine the Structure solution (i was missing the Global Mystruct.Structure) but i wasnt sure that it was the best way to do it.
And its strange that the trick works sometimes, i do know that the value pass parameters and is not suppoused to receive it... but i does ha ha ha ha
Of course im not planning to do it that way.
Thanks a lot for your answer !!
Posted: Mon Aug 05, 2002 4:17 pm
by BackupUser
Restored from previous forum. Originally posted by fweil.
Hello,
Just from my experience ... I have some multi-threading apps using global variables.
The trick is probably that if you start multiple instances from a threaded procedure you will need to have tables to store your variables with an indexed value corresponding to the thread ID (eg ThreadID = CreateThread ... and have a table containing all threads IDs).
I have got fine results so and it applies wether you use structures or not depending on your needs.
Just be careful with the amount of memory you are using when running large numbers of threads, it consumes some memory for each.
I have one example of Web downloading that can download lists of pages with such process. I was impressed at beginning of writing this about the performances it is possible to get. But it is rather tricky to tune.
BTW I cannot imagine a way to use threads without having somewhere a part of the application (internal or external) waiting for a result. And even if you have a possibility of returning a single value from the thread procedure it will probably change from each instance of multiple threads. So that the global data sharing might be the only way to code except if you have a logging system to an file or database for results or some expected values.
Do you agree that it explains how to understand it ?
Rgrds
Francois Weil
14, rue Douer
F64100 Bayonne