2

There is a native c++ application (linux) that load mono assembly. How to to share mutex (or his analogue) between managed and native parts of same application?

something like this:

native part:

native_lock_mutex(&db_mutex);
// do something with db
native_unlock_mutex(&db_mutex);

mono part:

managed_lock_mutex(db_mutex);
// do something with db
managed_unlock_mutex(db_mutex);

1 Answers1

0

You must expose the native mutex to managed code, something like this:

C code:

pthread_mutex_t* managed_get_mutex ()
{
    return &db_mutex;
}

void managed_lock_mutex (pthread_mutex_t *mutex)
{
    pthread_mutex_lock (mutex):
}

void managed_unlock_mutex (pthread_mutex_t *mutex)
{
    phtread_mutex_unlock (mutex);
}

C# code:

class NativMutex {
    [DllImport ("nativeLibrary")]
    public static export IntPtr get_managed_mutex ();

    [DllImport ("nativeLibrary")]
    public static export void managed_lock_mutex (IntPtr mutex);

    [DllImport ("nativeLibrary")]
    public static export void managed_unlock_mutex (IntPtr mutex);
}
Rolf Bjarne Kvinge
  • 19,253
  • 2
  • 42
  • 86