MainThread

BlitzMax Forums/BlitzMax Programming/MainThread

Yue(Posted 2016) [#1]
According to the documentation says it returns the main thread . I thought that another pair command set that thread as the main , but not actually doing entende achievement if I have multiple threads, which is the main ?, some simple code to understand this.

I appreciate the help we can get


Derron(Posted 2016) [#2]
The main thread is your normal code

SuperStrict
Import brl.foo
Import pub.bar
'...

Graphics 800,600

repeat '<-- mainthread
  Cls
  if neededNewThread then CreateThread(foo, bar)

  DrawText("i am the main thread " + Millisecs(), 10,10)

  Flip
until KeyHit(KEY_ESCAPE) or AppTerminate()



bye
Ron


Yue(Posted 2016) [#3]
Thanks Derron, then that makes the Main Thread?
Function Main Thread: Thread ()
Description: Get Main thread
Returns: A thread Object Representing the Main application thread.


col(Posted 2016) [#4]
Hiya,

As Derron says - your main thread is your normal code.

Every piece of code needs to run in a thread including your regular code. The confusion can come because in other languages the main thread always needs you ( as a coder ) to define the main thread entry point - ie the main thread function. For eg in c/c++ the main thread function is the 'main(int argc, char *argv[])' function.

In Blitz languages that main thread function is hidden from you ( to make programming easier, it is also used to get the program name and command line parameters that Blitz languages store for you so that things like AppArgs() works properly ) and, behind the scenes, that main thread then calls your own 'regular code' as if it was a function. When your regular code exits then the program actually drops back into the main thread function ( remember it is hidden from us Blitzers :p ).

When you create a new thread you need a function that the thread will run. This is the basic mechanics of how multiple 'threads' or multiple 'functions' can be 'selected' to run at the same time. You choose a function for a thread to run - just that in Blitz languages that 'main' function definition is hidden from us.

Using MainThread() will give you a TThread Object that represents the main thread so that any threads may use it for whatever reason ( including the main thread itself ).

Threads are fun eh :^)


Yue(Posted 2016) [#5]
Thanks You Col.


Now my question is, as I delete a thread removed them alone ?, when ?, finishes its work when the application is closed ?, For example I want to implement a charging wire resource for the application , when it finishes its work , I would like it is whether awake or just dies after loading meshes, sounds, textures , etc.


col(Posted 2016) [#6]
Never try to 'kill' a thread. Always let the thread exit from the function as normal. Letting the program flow exit the function will also exit the thread too.

edit:
While the thread is still running then ThreadRunning(loadingThread) will return True, when the thread has exited the function then ThreadRunning(loadingThread) will return False. You can then know that a thread running the function 'loadingThread' is either still loading or has finished loading.

There are ways to keep a thread alive, but simple small steps are best to help understand.


Yue(Posted 2016) [#7]
Ok, Col, Thanks You.


that is what makes the DetachThread ?


Google translator tells me that removes a fruit , called mango




col(Posted 2016) [#8]
Always trust google :D

DetachThread
will stop ThreadRunning detecting if the thread is still running or not. It will allow the thread to run freely without you being able to know if it is running or not via ThreadRunning.


Yue(Posted 2016) [#9]
Thanks , really google translator is rather difficult and sometimes makes things worse , in Castilian can apply the name of the fruit to the handle of a pan that has something on the stove, if not handle , this means that I can not take the pot.

: D





I appreciate the patience and support me.

Another question I want to know , is that the threads are functions that run parallel to the main thread , but for example if I want to load resources of my game, all of these variables must be global ?, because the thread function recige a couple type object function Thread, but that can not happen in that parameter.


col(Posted 2016) [#10]
As in regular programming there are a million different ways to do the same thing. I always prefer the easiest and simplest that is robust and works accurately.

When you are using multiple threads then you soon learn that you will need some way to prevent threads accessing the same data at the same time! You mention Global variables, Yes you could do that. But you NEED to know that you can get into bad trouble if more than 1 thread is writing/reading from the same variable at the same time - and with multiple threads sometimes more than 1 thread will access the variable at the same time, sometimes not... you really cannot tell. But always write code that will assume that more than 1 thread is trying to modify a variable at the same time and you will be safe.

This is where TMutex and TSemaphores can be used as building blocks to prevent two threads from accessing various code at the same time. You should learn how and when to use these as soon as possible. Experiment is best way to learn. You can learn using 1 or 2 threads to see what they do and how they affect the code.

On the contrary...
However with some good planning you can sometimes avoid needed mutex and/or semaphore locks in the first place. Sometimes you just HAVE to use them.

Using another thread just for loading resources is a very good example where you don't need to use a TMutex for protecting variables ( but I stress... PLEASE learn how they work, when you need them, and how to use them ).

You will notice that the CreateThread takes a function and an Object in the parameters and also returns an Object too. When you return an Object from the thread function you can get that object by calling WaitThread ( it will return the Object returned by the thread function ). Even if the thread has exited a long time then you can still WaitThread for one time and get the Object returned. However! if the thread has NOT finished then WaitThread will cause your code to wait until the thread has finished, this is where you can use ThreadRunning to know if the thread has finished or not and then use WaitThread to get the Object returned from the thread.

An example. This code hasn't been tested and should be treated as 'pseudo code' and NOT a working example, although it may work :D



Yue(Posted 2016) [#11]
Thanks for your help, actually I have a lot ahead to learn, Peroy today much more than I did yesterday, and this is a constant learning.

Something that strikes me and I always wanted to emulate Blitz3D, is how it is possible to make a change screen resolution without having to remove all objects, sounds, meshes, textures and then load them again, for example in Unity I noticed that are not removed and changing screen resolution is very fast, they come into play threads in programming?

It is possible to do that in BlitzMax.


Currently esoty using Xors3D.


And I can from the program in 4 parts, creating 3D graphics, comment xGraphics3D by then the xUpdateWordl, the XrenderWorld and XFlip. What always happened in Blitz3D is that by applying for example xReleaseGraphis, everything is deleted, and I wonder if you can store the objects in another thread to make a resolution change if you need to charge such a scene.


col(Posted 2016) [#12]
BlitzMax and Max2D, you don't need to reload any graphics as Max2D handles all of that for you.

If you are using Xors3D then you will read the Xors3D documentation to know if Xors3D can handle the screen change without reloading.

It is always possible but it's 'low level' and you need to keep references of what is already loaded. BTW only Dx7,Dx8 and Dx9 have the reloading issue for fullscreen resolution change. Dx10,Dx11,Dx12,OpenGL and Vulkan don't have the issue with reloading when fullscreen resolution changes. Maybe unity is using Dx11? Can Xors3D use Dx11 and/or OpenGL?