Function can only be called from main thread Error - multithreading

When I try to call terrain.terrainData.GetHeight() within an IJobParallelForDefer gives the error
'get_terrainData can only be called from main thread'
But I can't call it outside of the parallel job so how do I call the main thread function from a parallel job?
Unity version 2021.3.6 if that helps

Related

How many threads run event loop in nodejs

I am checking nodejs internal architecture and was curious about the event loop. So I cloned the node repo and modified the libuv code a little and added printf statements to understand the loop better. Then I build the code and ran an empty js file.
I added a printf statement before uv__io_poll function call
printf("polling io with thread %d\n", tid);
where tid is the thread id.
After running the program, I got the following output
polling io with thread 2242953
polling io with thread 2242949
polling io with thread 2242949
polling io with thread 2242949
Why there are 2 different thread ids?

JVCL TJvThreadTimer.OnTimer : Is This Code Executed in The TJvThreadTimer, ie. NOT the main thread

I want to have the code executed by the OnTimer event to be executed in a separate (non-Main) background thread. this code does not access or communicate with the main thread/GUI. Simple question, I get the timer (TJvThreadTimer) is executed in it's own background thread, but:
Does the code contained in TJvThreadTimer.OnTimer event get executed in that background thread as well?
It is unclear from the limited documentation.
Thanks......
If you look at the timer's source code for yourself, you will see that the OnTimer event handler is called inside of a class method that is Synchronize()'ed by the internal background thread, which means the event handler runs in the main UI thread.

Does Thread.sleep method in kotlin blocks the thread?

Just started learning Kotlin and I am trying to make sense of the output of the code snippet below. Per my understanding, the output should be in this order -
Right After Thread.sleep call - from main thread
mainline - from main thread
launched in new thread - from pool-1-thread-1 thread
The first coroutine is created and passed to new thread for execution and suspend for 2 secs.
Second coroutine is launched in main thread. Block the main thread for 0.5 sec and print.
Main thread get unblocked, and third print statement is executed.
Lastly, the first coroutine resume execution after 2 secs delay and prints the statement.
However, the actual result printed is -
launched in new thread - from pool-1-thread-1 thread
mainline - from main thread
Right After Thread.sleep call - from main thread
Greatly appreciate if someone could help me understand the logic behind it
fun main() = runBlocking {
val es = Executors.newSingleThreadExecutor()
withContext (es.asCoroutineDispatcher()) {
delay(2000L)
println("launched in new thread - from ${Thread.currentThread().name} thread")
}
launch {
Thread.sleep(500L)
println("Right After Thread.sleep call - from ${Thread.currentThread().name} thread")
}
println("mainline - from ${Thread.currentThread().name} thread")
es.shutdown()
}
You should read the documentation
Calls the specified suspending block with a given coroutine context, suspends until it completes, and returns the result. ... withContext
Launches a new coroutine without blocking the current thread ...
By default, the coroutine is immediately scheduled for execution. Other start options can be specified via start parameter. ... launch
Default – immediately schedules the coroutine for execution according to its context. ... the coroutine code is dispatched for execution later, while the code that invoked the coroutine builder continues execution. ...CoroutineStart.DEFAULT
It means that
first withContext is executed
next a new coroutine is created and scheduled
then println("mainline - from ${Thread.currentThread().name} thread")
es.shutdown() is executed
and finally the coroutine block is executed

About asynchronous methods and threads

What actually happens behind the scenes with asynchronous functions?
Does it open a new thread and let the OS start and run it?
If so, can it cause deadlocks or other thread problems?
Here's an example of a async method:
var fs = require('fs')
var file = process.argv[2]
fs.readFile(file, function (err, contents) {
var lines = contents.toString().split('\n').length - 1
console.log(lines)
})
In fs.readFile(file,callback).This is a non-blocking call which means.
node's main thread stores the callback in event-table and
associate it with an event which will be emitted whenever file
reading process is done.
By the same time node has several internal threads(thread pool) from
which node's main thread assign file reading task to one of the
thread.
After this assignment the command is returned to main thread and
main thread continues with the other tasks and file reading process
is being done in background by other thread(not main thread).
Whenever file reading process is completed the event associated with
the callback is emitted along with the data from file and that
callback is pushed into task-queue where event loop tries to push
each task to the main thread(stack).
And when main thread(stack) becomes available and and there is no
task present before the callback's task this callback is pushed to
main thread's stack by the event-loop.
Please read event-loop for more info.
So the thread which is responsible for file reading doesnt cause Deadlock to othere threads.
It simply emit exception or success which is later handled by the callback

thread debug problem

how to test the function called by thread?
Debugger is not coming at the point where i kept break point for testing in the function called by the thread using thread start method.
Thanks in advance.

Resources