GLBasic forum

Codesnippets => Inline / 3rd party => Topic started by: Kitty Hello on 2009-Oct-13

Title: Multithreading with GLBasic [official]
Post by: Kitty Hello on 2009-Oct-13
Hi,

This is a library that provides multithreading and thread synchronization functions for GLBasic (premium).

I can't find if I ever posted this before, but here is the place it belongs to.

keywords:
thread, mutex, condition, ThThreadCreate




[attachment deleted by admin]
Title: Re: Multithreading with GLBasic [official]
Post by: MikeHart on 2009-Oct-13
Does it work on the iphone?
Title: Re: Multithreading with GLBasic [official]
Post by: Schranz0r on 2009-Oct-13
Mutex are multiplatform
Title: Re: Multithreading with GLBasic [official]
Post by: Hemlos on 2009-Oct-13
Awesome!
Title: Re: Multithreading with GLBasic [official]
Post by: metzzo on 2009-Oct-13
Thank you gernot :)
Title: Re: Multithreading with GLBasic [official]
Post by: bigsofty on 2009-Oct-13
Thank you Gernot!  :good:

In the way of advice, is there anything In the RTL I would be better to avoid running in a separate thread?
Title: Re: Multithreading with GLBasic [official]
Post by: Kitty Hello on 2009-Oct-14
I have not watched for thread safety, so I suggest using calculations and graphics exclusively in threads.
Title: Re: Multithreading with GLBasic [official]
Post by: Moru on 2010-Feb-28
This thread library was very easy to get started in, thanks! Had my program running within 5 minutes :-)
Just a few questions:

1. Is it possible to stop a thread from outside the thread?

2. When ending a thread, do I just need to let the subroutine run to the end or do I need to do something special?

3. I'm assuming the ThMutexLock() will halt execution until the mutex is free?
Title: Re: Multithreading with GLBasic [official]
Post by: Kitty Hello on 2010-Mar-01
1. You can do that with a condition. One thread informs the other about a quit flag.
2. yes. Just return in the sub and you're done.
3. right. The mutex lock will pause until you can access the mutex. That way you can lock shared memory.
Title: Re: Multithreading with GLBasic [official]
Post by: S. P. Gardebiter on 2010-Apr-19
Hello, I've got a question:
So multithreading is an official part of GLBasic and has the same licence conditions?
Means: Everything I can do with GLBasic, I can do with this libary as well?
Title: Re: Multithreading with GLBasic [official]
Post by: Kitty Hello on 2010-Apr-20
Yes
Title: Re: Multithreading with GLBasic [official]
Post by: Kuron on 2010-Apr-20
I didn't realize this library existed.  I have been using my own threading routines  :D
Title: Re: Multithreading with GLBasic [official]
Post by: bigsofty on 2012-Sep-21
Quick question: What is the name of the C lib that this uses within the in-line code? I was wanting to try and gather some docs on the C side for a better understanding of the of in-lined sections if possible.
Title: Re: Multithreading with GLBasic [official]
Post by: MrTAToad on 2012-Sep-22
I think it is this : http://pubs.opengroup.org/onlinepubs/7908799/xsh/pthread_create.html
Title: Re: Multithreading with GLBasic [official]
Post by: bigsofty on 2012-Sep-23
Could be!  :good:

Um after a quick websearch, it looks like this(or not?).  :S

http://hawksoft.com/hawkthreads/
Title: Multithreading with GLBasic [official]
Post by: Kitty Hello on 2012-Sep-23
Yes, its an altered version of hawkthreads.
Title: Re: Multithreading with GLBasic [official]
Post by: bigsofty on 2012-Sep-23
Many thanks, now I have the docs I can read into it a little more(no guarantee It will sink in though!  :D).
Title: Re: Multithreading with GLBasic [official]
Post by: jestermon on 2012-Sep-24
Just when I got halfway with "INLINE"ing a "c" threading solution, I find this post. . . (Should I cry or laugh?)
A small example of this thread in use - in Basic - could streamline things all around.. Would this be possible? (An example)
Title: Re: Multithreading with GLBasic [official]
Post by: bigsofty on 2012-Sep-24
There is a very small example already included with the lib I think.
Title: Re: Multithreading with GLBasic [official]
Post by: jestermon on 2012-Sep-25
Ah yes, thanks.
Title: Re: Multithreading with GLBasic [official]
Post by: Gary on 2015-Sep-02
Been using this lib for a while though and always had a windows alert and an UNHANDLED EXCEPTION alert in the debug window when exiting a program but as the code was never actually being quit I never bothered looking for it until today and it turns out my thread is causing the issue

I am setting the thread up with the following command

Code: GLBasic [Select]
        // Create a Mutex
        mx_mutex=ThMutexCreate()

        // Start a new thread
        IF ThThreadCreate("Interrupt")=FALSE
                PRINT "Can't create thread", 0,0
                SHOWSCREEN
                MOUSEWAIT
                END
        ENDIF

But how should I be handling it on app close? I thought that by doing
Code: GLBasic [Select]
ThMutexDelete(mx_mutex)
before the end command would do the trick but I still get the exception.

I am 100% certain its the thread that is causing the alert as when I dont initialise it then the code exits correctly.

Any help would be appreciated

EDIT: Found the issue, my code used the thread as a pseudo interrupt and the subroutine actually never quit, it did the code in a while 1 loop. Once I forced a graceful exit from the SUB before I deleted the Mutex it removed the error on shutdown. So the lesson is, make sure the thread is not running when you delete and exit :)
Title: Re: Multithreading with GLBasic [official]
Post by: bigsofty on 2015-Sep-03
Interesting, I never actually used this lib regretfully as I was never 100% confident that some little non-thread safe peice of external code would rear its ugly head.

It sounds like you are not(exiting to one side) have has any such problems Gary?
Title: Re: Multithreading with GLBasic [official]
Post by: Gary on 2015-Sep-03
No problems what so ever Bigsofty. It sat in the background running set code on a 20 50 100 250 and 500 ms interval with no issues. Admittedly it was not a massive amount of code but it still worked
Title: Re: Multithreading with GLBasic [official]
Post by: bigsofty on 2015-Sep-03
That's nice to know!  :good:

What are you using your interrupt for BTW?
Title: Re: Multithreading with GLBasic [official]
Post by: Gary on 2015-Sep-03
I was writing a video fruit machine and wanted it to be as close to the operating system I used for physical reel based machines where I basically "fire and forget" things like telling a lamp to flash or start the reels spinning or checking if a button was pressed. So within my graphic draw routines I have a flag for each graphic, lit and unlit, and I attach a status flag to each image saying which to draw. The interrupt can toggle these flags at a very specific interval to create flashing images. Also the reels have a flag to say stopped, spinning, stop at a position, bounce back and nudge. Again all this movement is controlled by the interrupt and not the main draw routine. Possibly I over engineered the solution but it works for me
Title: Re: Multithreading with GLBasic [official]
Post by: bigsofty on 2015-Sep-03
Interesting way of doing it. Well you've perked my interest in GLB threads again, I'll have a think on how I can use them in my game!  ;)
Title: Re: Multithreading with GLBasic [official]
Post by: kanonet on 2015-Sep-04
Actually I think that is the wrong way of approaching this, bigsofty. If you have a problem in your game that you can not solve any other way, than it may be worth to try it with multithreading. But you should not just include it, just because you want to. Threads add huge extra complexity, some overhead and many sources of possible bugs. In many cases threads dont get you anything, so you should avoid them if possible and only use them when really necessary.
BTW many GLBasic commands are not thread  save, so be careful what you pack inside a thread. That 'official' in the title is very misleading IMHO, since GLBasic was not designed with threads in mind.
Title: Re: Multithreading with GLBasic [official]
Post by: bigsofty on 2015-Sep-04
Thanks Kanonet, I've used multithreading in other languages in the past which were thread safe and I do understand the risks with non-threads age code. I will probably avoid the GLB runtime lib altogether and even then stick to inline C. The single core restriction is a real pain TBH, even getting some of the CPU power back would be a real boost. But yes, I'm very restricted to mainly calculations and even then the 2nd thread has to be almost independent of the 1st with careful mutex handling. AND even then it will be labeled as experimental for a long time.

Thank you for you word of caution though.  ;)