Scope of Prototypes

Just starting out? Need help? Post your questions and find answers here.
wayne-c
Enthusiast
Enthusiast
Posts: 335
Joined: Tue Jun 08, 2004 10:29 am
Location: Zurich, Switzerland

Scope of Prototypes

Post by wayne-c »

Code: Select all

Prototype ProtoCurlEasyPerform(handle.i)
If OpenLibrary(0, "libcurl-x64.dll")
	CurlEasyPerform.ProtoCurlEasyPerform = GetFunction(0, "curl_easy_perform")
EndIf

What is the scope of the procedure CurlEasyPerform()?
- is it defined globally?
- is it safe to use the procedure in threads?
Last edited by wayne-c on Wed Jul 28, 2021 10:53 am, edited 2 times in total.
As you walk on by, Will you call my name? Or will you walk away?
User avatar
Demivec
Addict
Addict
Posts: 4085
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Scope of Prototypes

Post by Demivec »

wayne-c wrote: Wed Jul 28, 2021 9:58 am

Code: Select all

Prototype ProtoCurlEasyPerform(handle.i)
If OpenLibrary(0, "libcurl-x64.dll")
	CurlEasyPerform.ProtoCurlEasyPerform = GetFunction(0, "curl_easy_perform")
EndIf
Q: What is the scope of the procedure CurlEasyPerform()?
A: CurlEasyPerform() is not a procedure it is a prototyped
variable that has been assigned the address of a function in a dll.

Q: Is it defined globally?
A: No. The scope of 'Global' was not used in its definition. Because no explicit definition including scope was used (i.e. Global, Define, Threaded) the default scope is the same as if 'Define' was used. This means it is local to the scope in which it appears, which in this case is the main scope. Other possible local scopes include that of a procedure or a module.

Q: Is it safe to use the procedure in threads?
A: it a prototyped variable with only scope local to the main block. It is not a 'procedure' though the procedure whose address it holds can be called in a similar manner as calling other procedures. If it was a procedure its scope would be limited only by whether it was defined in a module, a module's declaration, or in the main scope outside of a module. As a prototyped variable it has additional scopes possible. . To be used in a thread It would first have to be either declared as Global in the main scope or as 'Shared' in the thread's procedure and second it should be treated as read only and not have its value changed after its initial declaration in the main scope.
wayne-c
Enthusiast
Enthusiast
Posts: 335
Joined: Tue Jun 08, 2004 10:29 am
Location: Zurich, Switzerland

Re: Scope of Prototypes

Post by wayne-c »

@Demivec Thank you

That means if I globally declare e.g. CurlEasyPerform.ProtoCurlEasyPerform then I could safely use this variable(=function) also in threads (as the variable(=function) is never changed)? Just to ensure I get you right.
As you walk on by, Will you call my name? Or will you walk away?
User avatar
Demivec
Addict
Addict
Posts: 4085
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Scope of Prototypes

Post by Demivec »

wayne-c wrote: Wed Jul 28, 2021 10:55 am @Demivec Thank you

That means if I globally declare e.g. CurlEasyPerform.ProtoCurlEasyPerform then I could safely use this variable(=function) also in threads (as the variable(=function) is never changed)? Just to ensure I get you right.
Yes.
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Scope of Prototypes

Post by infratec »

then I could safely use this variable(=function) also in threads
In this case yes.
But ...

it depends if the dll or library with the target function is threadsafe.

libcurl is 'threadsafe' so you can use it in threads.
Post Reply