Have LocalAlloc and LocalFree to be declared as shared methods?

M

Mobile Boy 36

Hi all,



I'm using a ping class for a couple of months. Everything worked fine. All
class methods were declared as shared (static in c#) methods.

Now I want to make the class threadsafe, so I started to remove the shared
methods and made a 'normal' class of it, which has to be instantiated. The
class is using a couple of API calls, under which LocalAlloc and LocalFree.

These 2 functions do only work if declared as shared methods. Why?



<System.Runtime.InteropServices.DllImport("coredll")> _

Private Shared Function LocalAlloc(ByVal Flags As Integer, ByVal
size As Integer) As System.IntPtr

End Function



<System.Runtime.InteropServices.DllImport("coredll")> _

Private Shared Function LocalFree(ByVal pMem As System.IntPtr) As
System.IntPtr

End Function



What do I have to change? Would my class be threadsafe if LocalAlloc and
LocalFree are declared as shared methods?

Keep in mind, my aim is a threadsafe class. I want to avoind using sync
objects...



Any help would be greatly appreciated

Best regards
 
G

Guest

Now I want to make the class threadsafe, so I started to remove the shared
methods and made a 'normal' class of it, which has to be instantiated. The
class is using a couple of API calls, under which LocalAlloc and
LocalFree.

static methods and thread safe really have nothing to do with one another.
You can be thread safe and have static methods without problem.
These 2 functions do only work if declared as shared methods. Why?

Because you're declaring a P/Invoke - that's how it must be defined.
What do I have to change? Would my class be threadsafe if LocalAlloc and
LocalFree are declared as shared methods?

Again that has nothing to do with it. It could be thread safe, or it could
just as easily not be. Depends on how your entire class is architected.
Keep in mind, my aim is a threadsafe class. I want to avoind using sync
objects...

I doubt that's possible. How do you propose to make something thread safe
without synch objects? Global flag variables (some would argue they are
sync object if used for thread safety purposes anyway)? How do you propose
to make operations on them atomic? I think you need a better understanding
of "thread safe".

-Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top