2

有一个加载单声道程序集的本机 c++ 应用程序 (linux)。如何在同一应用程序的托管部分和本机部分之间共享互斥锁(或他的类似物)?

像这样的东西:

原生部分:

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

单声道部分:

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

1 回答 1

0

您必须将本机互斥锁公开给托管代码,如下所示:

C代码:

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#代码:

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);
}
于 2012-11-01T09:18:05.250 回答