Module: Operating System Basics, Setting up Development Environment
Topics: Threads and Concurrency, * from Setting up Development Environment
Threads and Concurrency
PHP has support for multithreaded execution, even though we rarely used it in wordpress ecosystem, it is still important to learn the concepts.
So when ever we hit a HTTP request it spins up a new PHP process for it, so you have to be careful with queries like db queries or wp_remote_get(). We should be caching any lengthy HTTP request using AJAX or REST APIs.
Transients allows us to do the same, it is basically a simple way to store cache data temporarily.
Transients basically accept 3 args. Listed below:
$transient(string): Transient name.
Expected to not be SQL-escaped. Must be 172 characters or fewer in length.
$value(array|object): Data to save, either a regular variable or an array/object.
The API will handle serialization of complex data for you.
$expiration(integer): The maximum of seconds to keep the data before refreshing.
Transients may expire before the
$expiration(Due to External Object Caches, or database upgrades) but will never return their value past $expiration.
To set the transient value we can use the set_transient function
set_transient( $transient, $value, $expiration );
To get the transient value we can use the get_transient function, it takes single arg to get the data. here
$transient is the unique slug you used to set the data.
get_transient( $transient );
It protects accessing of same shared resource simultaneously
look at the overview of its working from here.
//Only one thread can access at a time
using Condition Variables we can access mutual exclusion under certain conditions. here is how it works in three steps:
- Wait(mutex, condition)
When two or more threads are competing and waiting for each other to finish , but none of them finishes then it is called a deadlock situation.
In the given fig. below T1 and T2 are in a deadlock situation.
So, to solve this issue we can simply allow one (T1) before allowing other (T2).
Kernel vs User level Threads
There are basically three types of model
- One to One model: This model provide synchronization and blocking but OS may have limited threads and Issues to portability.
- Many to One model: This model is totally portable but if one user-level thread is blocked then whole process will be blocked by the OS.
- Many to Many model: This model is best because it can bound or unbound the threads but might require coordination between user and kernel level thread managers
Here is the more information related to the kernel vs user level threads
GIT VS SVN
When writing a code version control helps us a lot throughout the development. When we break something, accidentally delete the files or code. If we use some kind of version control system we can prevent all this from happening.
Although in Subversion (SVN) works on a centralized manner and Git works on a distributed manner, versioning and revision control system is almost same in both of them.
When writing a commit message. It is important that you write a Effective Git Commit message so it should be easy for everyone to understand. Always summaries the line, here is an effective guide for it.