Top two interview questions of async programming

One of the most important things about building an application on top of the game engine is performance. We have to carefully...

Top two interview questions of async programming

VRcollab https://vrcollab.com is a BIM software company. We build our system fully based on C# and Unity. One of the most important things about building an application on top of the game engine is performance. We have to carefully design each function and limit the time of the excitations so that it won’t block the rendering thread.

Here are the top two interview questions of async programming from our Tech interview. Actually, VRcollab uses this system as the background engine, and with that, we are able to load a 2GB BIM model within a few seconds. If you are interested in solving these questions, please send your answer to yiwei@vrcollab.com.

Background

A task-based parallel model is widely adopted by many applications. Usually, the UI updates are only allowed to be performed on the main thread, other operations will be executed as a task on the background thread. Once the task is finished, it will be poped from the task list and notify the main thread.

However, in the earlier version of Unity, it does not support the C# task system and the parallel primitives. Our questions are to implement a custom task-based parallel model.

Question 1, Implement a custom dispatcher

Use pseudo-code to implement a custom task dispatcher. Your dispatcher shall only use the low-level thread. C# build-in task, promise, and futures are not allowed here. Your dispatcher shall at least support background tasks, main thread tasks, and continuation. For example

// Q1: Implement a custom dispatcher
// Use pseudo-code to implement a custom task dispatcher. 
// Your dispatcher shall only use the low-level thread.
// C# build-in task, promise, and futures are not allowed here.
// Your dispatcher shall at least support background tasks, main thread tasks, 
// and continuation. For example

Dispatcher.StartOnBackgroundThread(() => {
    Thread.Sleep(10000); // Do some heavy calculation
    var result = "Hello World";
    return result;
}).ContinueWith((result) => {
    // Continue the task on main thread
    UI.text = result;
}, Dispatcher.RunOnMainThread).ContinueWith(() => {
    // Continue the task on background thread
    Thread.Sleep(10000); // Do some heavy calculation
}, Dispatcher.RunOnBackgroundThread);

Question 2, Implement a custom async/await styled parallel model

With the custom dispatcher, use pseudo-code to implement a custom async/await styled parallel model, i.e. without passing in the task callback function. C# build-in task, promise, and futures are not allowed here. Your parallel model shall support both background tasks and main thread tasks. For example

// Q2: Implement a custom async/await styled parallel model
// With the custom dispatcher, use pseudo-code to implement
// a custom async/await styled parallel model, i.e. without
// passing the task callback function. C# build-in task, promise,
// and futures are not allowed here. Your parallel model shall
// support both background tasks and main thread tasks. For example

ThisIsAsyncFunction() {
    RunOnBackgroundThread()      // From here, the code shall run on background thread
    Thread.Sleep(10000);         // Do some heavy calculation
    var result = "Hello World!";
    RunOnMainThread()            // From here, the code shall run on main thread
    UI.text = result;            // Continue the task on main thread
    RunOnBackgroundThread()      // From here, the code shall run on main thread
    Thread.Sleep(10000);         // Continue the task on background thread
}