I haven’t used löve’s implementation of threads, but I have experience with C threading, so I’d be able to give a basic idea of what this is all about.
First, I’d suggest to be cautious
about threading, depending on what you are doing, their implementation and debugging could be somewhat difficult. If you can achieve the same results with another approach such as co-routines, then, I’d personally encourage you to explore that option instead.
For a little background, threads are essentially pieces of code that can be run simultaneously
inside a single process (i.e. application). Now, we can argue the true meaning of concurrency here, depending on the kind of CPU architecture we’re talking about (mono or multi processor) but that’s another topic.
Threads offer the ability to run various tasks
at the same time
, giving you the capacity to multi-task
certain aspects of your application. One common example of threads in action is the “loading bar” application.
Suppose your application has an initial “loading” stage where all your resources are actually being loaded into memory. If your program uses just one thread for this task, the user will stare at an unresponsive and ugly “frozen” application screen while all resources are being loaded.
That is because the application is actually doing just one thing at a time, it can’t update the graphical interface or response to user input because it is busy loading resources. But now... imagine if we could somehow split the program into two tasks and run them “simultaneously”?
We could load resources with one task (thread) and show a nice, dynamic “progress bar” for the user’s visual enjoyment with another task. Nice eh? And that’s just one example of threading!
Now, the ugly part. Threads are generally generated in one, shared, environment (i.e. one single program). That means that, if we’re not cautious enough, threads could access global data and change its value (nasty!) at the same time (known as race condition).
Of course, there’s a solution to these problems. Blocking mechanisms
should ensure that certain resources are only accessible to just one thread at a time. Depending on the implementation, thread managing could also be quite laborious, compromising the complexity of your application.
So, basically, you must be aware of the benefits and disadvantages of threads. Of course, it all depends on your application and what you are trying to achieve, there are some functionalities that can be accomplished through threads only.
That’s the general idea behind it, hope it helps!