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?
What are you planning to use threads for? Or are you just trying to learn?
struct Composite { MyStuff* a; OtherStuff* b; };
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.
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
https://github.com/liballeg/allegro5/blob/master/examples/ex_threads2.c
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?
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; }
Just for the sake of completeness:
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
[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!
Don't join the thread until you're done.
Also, shared data requires the use of synchronization or Mew Texas. (mutexes).
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.
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?
Think. What does a player do? That's at least one option for the AI to choose.