hi, I have a plugin which uses unmanaged thread internally (using c++11 std::thread)
these threads call UnityEngine.Debug.Log via [UnmanagedFunctionPointer(CallingConvention.Cdecl)] delegate to show some information on Editor console.
when it runs under stable scripting runtime (dotnet 3.5), no problem occurs. but when I use experimental runtime (dotnet 4.6), it crashes inside Domain Unloader thread on entering play mode twice.
I could narrow down the problem to below little example.
when it runs on dotnet 3.5, it always shows "called back" 5 times then shows "done" .
but for dotnet 4.6, first time it runs ok, but crash on 2nd time.
**native plugin (built as die.bundle)**
#include
#include
#include
static pthread_t *threads;
static int n_threads = 0;
void *proc(void *a) {
while (1) {
((void (*)())a)();
sleep(1);
}
return NULL;
}
extern void start_thread(int n, void (*func)()) {
threads = malloc(n * sizeof(pthread_t));
for (int i = 0; i < n; i++) {
pthread_create(threads + i, NULL, proc, func);
}
n_threads = n;
}
extern void stop_thread() {
for (int i = 0; i < n_threads; i++) {
pthread_cancel(threads[i]);
pthread_join(threads[i], NULL);
}
free(threads);
}
**MonoBehaviour uses this plugin**
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Collections;
using UnityEngine;
class Crash : MonoBehaviour
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void ThreadCB();
[DllImport ("die")]
extern static void start_thread(int n, ThreadCB cb);
[DllImport ("die")]
extern static void stop_thread();
bool stopped;
void Start()
{
start_thread(1, () => {
Debug.Log("called back");
});
stopped = false;
}
void Update() {
if (Time.time > 5.0f && !stopped) {
stop_thread();
Debug.Log("done");
stopped = true;
}
}
}
I suspect that it relate with this bug https://bugzilla.xamarin.com/show_bug.cgi?id=50537 and need cleanup mono thread state somehow. but check with mono_thread_internal_current_is_attached shows these threads does not seems to be attached MonoInternalThread. so now I completely stuck.
does anyone got same issue? if does, can solve this problem? any suggestions are welcome.
regards,
↧