Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Need Good tutorials on Allegro threads.

This thread is locked; no one can reply to it. rss feed Print
Need Good tutorials on Allegro threads.
Doctor Cop
Member #16,833
April 2018
avatar

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:
Is there a way to pass more than one structure in thread function?

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Doctor Cop
Member #16,833
April 2018
avatar

I have used pthreads before but I'm having difficulty in following examples at GitHub repository.

A comparison of both will be good.
I was thinking of making a small pong game as my krampus hack disaster compensation.
I am making a basic pong game for now but I will improve it with time and add elements to it gradually.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Doctor Cop
Member #16,833
April 2018
avatar

Then how will I catch input and still move ball at the same time?

al_wait_for_event() and al_rest() will cause problem.
When I'm waiting for event I can't update my game loop.
What should I do then?

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

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;
}

princeofspace
Member #11,874
April 2010
avatar

Just for the sake of completeness:

#SelectExpand
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
avatar

princeofspace
Member #11,874
April 2010
avatar

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
avatar

Thanks.

It worked.
Now I'm going to add controls and later will try to implement multiplayer system.

Edit:
The multiplayer system was easily implemented but how to implement com part.
How the game AI should work in pong?

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Go to: