有一个加载单声道程序集的本机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);
答案 0 :(得分: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);
}