Page 1 of 1
Some Thread Questions
Posted: Fri Apr 04, 2008 10:32 pm
by Straker
I have a few questions related to Threads and either can't find what I am looking for in the forums or am getting differing information from the various posts. I am seeking clarification on the following:
1) Network Functions
Is it safe and/or possible to use Network functions within threads ? Specifically SendNetworkData() and ReceiveNetworkData() ?
2) Using Debug in Threads
Is is possible to use "Debug" within threaded procedures, because most of the thread examples seem to use "PrintN" to output data instead of Debug ? Is using Debug akin to "writing to strings" (which seems to be a thread no-no) ?
3) Calling other Functions from a Threaded Function
Consider the following code:
Code: Select all
Procedure.l OutsideProc()
...
EndProcedure
Procedure ThreadedProc(MyParm.l)
...
OutsideProc()
...
EndProcedure
firstThread.l = CreateThread(@ThreadedProc(),123)
secondThread.l = CreateThread(@ThreadedProc(),222)
In the code above, is the procedure named OutsideProc() included in the individual threads and thus safe from conflicts, since it is called from ThreadedProc() ? Or will a potential conflict arise because OutsideProc() is not explicitly called by CreateThread()?
Thanks in advance for any help.
Posted: Fri Apr 04, 2008 10:35 pm
by srod
I only have an answer for question 3, which is that there is no problem with threads calling other functions within your program. The only thing to look out for on that score are shared variables - e.g. each thread will see the same values for any static variables etc. and could cause problems if each thread attempted to alter the value etc. Local variables are fine however because each thread has it's own stack.
Posted: Fri Apr 04, 2008 10:49 pm
by Trond
Debug output and network functions should be OK as long as you check threadsafe, I think.
Posted: Sat Apr 05, 2008 2:30 am
by freak
1) Most of the Network lib is threadsafe even without turning that switch on.
Specifically it is only the creating/closing of network servers and the network
server event handling that should be protected against multiple thread access.
Of course you shouldn't receive/send data for a single client from two threads at
once, but that sounds logical.
2) You should turn on the threadsafe switch if you use the debugger.
It should actually not crash to use the debugger in threads without that switch,
but then you cannot trust any of its data (line numbers etc) as the threads
will mess up that data.
"Debug" statements should come through correctly though as they are sent through a protected pipe,
but don't use any string functions to construct the content of the debug statement...
3) Each thread has its own stack, so all local variables/procedure parameters will
be different for each thread. Only static, shared and global will be shared
between all thread. (and also between recursive calls in the same thread of course)
Posted: Sat Apr 05, 2008 11:02 am
by DoubleDutch
but don't use any string functions to construct the content of the debug statement...
Didn't know this. Thanks for the info.
Posted: Sat Apr 05, 2008 3:51 pm
by Pantcho!!
before PB4.xx versions i had many many frustrations with threads.
But in PB4.xx the thread safe switch was like a magic for me.
I created a program that manages threads like a slot machine and each slot will be free when the threaded procedure that launch is finished and sometimes it can get stack since its a thread that connects a server gets a page , parse it (meaning it calls an outside procedure like you asked) and then close the connection and free the slot for the Main program loop thread manager to see it is free for the next one.
I've been using it with also the debugger and it worked perfectly with 40 running threads where each does what i explained.
So i think you can try this you might be surprised, i still am.
Posted: Sat Apr 05, 2008 6:30 pm
by Straker
Thanks for all the feedback. It appears to work great. I am not creating a server so thats probably a good thing. I am only using the following network functions with no problems:
Code: Select all
OpenNetworkConnection()
SendNetworkData()
ReceiveNetworkData()
CloseNetworkConnection()
But, I am also using Str() in contructring my debug statement and its not causing any problems, like this:
Code: Select all
Debug "result: " + Str(myResult.l)
It does not seem to be causing any problems, but should I change it anyway?
Posted: Sat Apr 05, 2008 7:22 pm
by freak
If you use the threadsafe switch it is of course no problem.
Without the switch i would change it. It will probably a rare thing, but it may
mess up any string operation that happens at the same time in the main thread.
Posted: Sat Apr 05, 2008 7:49 pm
by Straker
Thanks freak. The threadsafe switch was not checked, but still had no problem. I will check the threadsafe switch.
Thanks again.