You can not a .net implementation of Semphore as a substitute for the posix one. Using pinvoke, and dllimport you can call shmget() to create posix semaphore.
Semaphore or SemmaphoreSlim not working in Linux with pinvoke
Hi,
I am trying to create a .NET standard wrapper which calls the originally implemented API methods from C++ library. So, in my C++ library there is a method which takes one of the parameter is Unix type Semaphore which is sem_t * and I need to call that API method from .NET standard wrapper code which I am using initially "Semaphore" which is giving Exception like System.InvalidOperationException: Type 'System.Threading.Semaphore has a ComVisible(false) parent 'System.threading.WaitHandle' in its hierarchy, therefore Query Interface calls for I Dispatch or class interfaces are disallowed."
I found some where in documentation that "Semaphore" uses COM Win32 object, so I used passed "SemmaphoreSlim" as an argument to API method instead of "Semaphore", this time it worked fine in Windows, but giving an exception in Linux "System.Runtime.InteropServices.MarshalDirectiveException: Cannot marshal 'parameter #2': Invalid managed/unmanaged type combination (Interfaces must be paired with Interface)."
So is there any way to pass Semaphore using pinvoke by marshaling it?
please let me know.
Environment is .NET Standard 2.0 and .NET core 3.1
C++ method
void myAsyccSemaphore(sem_t * event, int eventType)