This is in Unity / Mono, but any .Net ...
Imagine some code like this
using System.Threading;
using System.Runtime.InteropServices;
public class Example: MonoBehaviour {
[DllImport("__Internal")] private static extern void long_function();
[DllImport("__Internal")] private static extern void short_function();
public Thread happyThread;
public void Test() {
happyThread = new Thread(LongTest);
happyThread.Start();
}
private void LongTest() { long_function(); }
private void ShortTest() { short_function(); }
}
We run Test
and thus long_function
from the C library is launched. It is launched on another thread. ("happyThread ")
Say it takes 60 seconds to run.
At the end of that time, does
happyThread
cease to exist? Or is it still there in Example scope? Or can you retain it or something??Let's say we know
long_function
finished. I then want to callShortTest
and henceshort_function
. Can you do something like this ..
Earlier we did this
happyThread = new Thread(LongTest);
Can we now "change" the "contents" of the thread to
happyThread.newStuff( ShortTest());
happyThread.StartAgain();
or is that stupid and meaningless?
Would I simply start ShortTest again just on another new Thread?
Say the library has a simple global variable
int k
...
So TBC, the C file which is built to the static library looks like this at the top:
// file happyLibrary.c Fattie(TM) 2019
#include <stdio.h>
#include <CoreFoundation/CoreFoundation.h>
int k = 7; // (NB you almost certainly want volatile here in real apps)
void long_function
{
...
... which
long_function
sets to be 42. In fact .... willshort_function
be able to also later access that value?? Or? Only if I use the same thread or something? Again,k
is a global global.if you have to launch zillions of little tasks, if there perhaps a performance advantage in re-using happyThread, if you can reuse 'em??
(I fully appreciate that, simply, long_function would persist over there and offer callbacks to the main thread, with the short_function concept; but, uh, I'm wondering about what I ask about above. Can you sort of "re-use" a thread? Can you get "in to" it again?)