|
Need Good tutorials on Allegro threads. |
Doctor Cop
Member #16,833
April 2018
|
Hi guys. I was trying to use ALLEGRO_THREAD in my C program but I just couldn't figure it out that how it actually works. Edit:
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
What are you planning to use threads for? Or are you just trying to learn? struct Composite { MyStuff* a; OtherStuff* b; };
My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Doctor Cop
Member #16,833
April 2018
|
I have used pthreads before but I'm having difficulty in following examples at GitHub repository. A comparison of both will be good.
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
Multi threaded pong? What on earth for? Threads are for parallel processing. There's nothing in pong that would benefit from using a thread. See ex_threads.c : https://github.com/liballeg/allegro5/blob/master/examples/ex_threads.c My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Doctor Cop
Member #16,833
April 2018
|
Then how will I catch input and still move ball at the same time? al_wait_for_event() and al_rest() will cause problem.
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
Use a timer to wake up the queue. See the example of a generic game loop on the wiki : https://github.com/liballeg/allegro_wiki/wiki/Allegro-Vivace%3A-Basic-game-structure Click on View Source near the top to see an example. Basic event loop is like this : Psuedocode : while (!quit) { if (redraw) {Redraw();redraw = false;} do { WaitForEvent(); if (TimerEvent) {Logic();redraw = true;} } while (HaveEvent()); } void Logic() { ball.x += vx; ball.y += vy; }
My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
princeofspace
Member #11,874
April 2010
|
Just for the sake of completeness: 1
2struct FOO {
3
4 struct A a;
5 struct B b;
6
7};
8
9
10void process_struct_foo(ALLEGRO_THREAD *t, void *arg)
11{
12
13 struct FOO *foo = (struct FOO *) arg;
14 do_stuff_with_a(&foo->a);
15 do_stuff_with_b(&foo->b);
16
17}
18
19
20void do_threading(struct FOO *foo)
21{
22
23 ALLEGRO_THREAD *t = al_create_thread(process_struct_foo, foo);
24 al_start_thread(t);
25 al_join_thread(t, NULL);
26
27}
Void casting is usually something we try to avoid but in this instance, we sort of have to. C puts the developer in the driver's seat, so you'd double-down better know what you're doing. Also, in real code, you would manually destroy the structs so as not to be leaking memory. One caveat: BE CAREFUL with multithreading pointers. If you have a pointer inside a struct, which in turn points to some data you're messing with, and two (or more) threads manipulate that data, you create a race condition.[Ominous music plays in background] Race conditions are a quick road to undefined behavior, where lie dragons and madness. You literally have NO IDEA what the hardware will do!! You certainly can multithread pointers, and lots of good code does this, but be warned: only one piece of code manipulating on one chunk of data at a time. For the love of all that you hold dear, avoid the dread race condition! |
Edgar Reynaldo
Major Reynaldo
May 2007
|
Don't join the thread until you're done. Also, shared data requires the use of synchronization or Mew Texas. (mutexes). My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
princeofspace
Member #11,874
April 2010
|
Edgar Reynaldo said: Don't join the thread until you're done. Also, shared data requires the use of synchronization or Mew Texas. (mutexes). Good catch, I forgot to write that stuff in the quick example code. In most cases, Allegro already does all the practical threading for you. If you're using the event queue system or music streams, Allegro is threading away in the background. |
Doctor Cop
Member #16,833
April 2018
|
Thanks. It worked. Edit:
|
Edgar Reynaldo
Major Reynaldo
May 2007
|
Think. What does a player do? That's at least one option for the AI to choose. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
|