Some Thread Questions

Just starting out? Need help? Post your questions and find answers here.
Straker
Enthusiast
Enthusiast
Posts: 701
Joined: Wed Apr 13, 2005 10:45 pm
Location: Idaho, USA

Some Thread Questions

Post 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.
Image Image
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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.
I may look like a mule, but I'm not a complete ass.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Debug output and network functions should be OK as long as you check threadsafe, I think.
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post 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)
quidquid Latine dictum sit altum videtur
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post 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.
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
Pantcho!!
Enthusiast
Enthusiast
Posts: 538
Joined: Tue Feb 24, 2004 3:43 am
Location: Israel
Contact:

Post 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.
Straker
Enthusiast
Enthusiast
Posts: 701
Joined: Wed Apr 13, 2005 10:45 pm
Location: Idaho, USA

Post 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?
Image Image
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post 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.
quidquid Latine dictum sit altum videtur
Straker
Enthusiast
Enthusiast
Posts: 701
Joined: Wed Apr 13, 2005 10:45 pm
Location: Idaho, USA

Post by Straker »

Thanks freak. The threadsafe switch was not checked, but still had no problem. I will check the threadsafe switch.

Thanks again.
Image Image
Post Reply