EDIT: I should make it clear. platform is OSX UnityEditor.
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 (means stop 1st time run and restart).
native plugin (built as die.bundle)
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
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*);*
pthread_join(threads*, 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 (I estimate mono runtime auto attached thread state when unmanaged thread entering it, as MS dotnet runtime does). 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,