Multithreading in C/C++
Writing a multithreaded application has become mandatory nowadays. Even though it has its own caveats it is still a firm programming aspect for lots of real time applications, including kernels. Here we will see some the most widely used techniques for writing threaded applications.
I am going to discuss threading in GNU/Linux environment, your mileage may vary in other platforms, but definitely it is not for windows. I use only pthread or POSIX thread library.
Threading is available in many languages including Perl(interpreter threads), python(?, optional thread module), ruby(green threads) and obviously C(well, kind of in C++, since you still use C functions to write C++ threaded application, with using C++ wrapper classes).Before we dig in, lets us see how and why. In any OS, A process is piece code loaded into memory with assigned address space and could be a single threaded or a multithreaded application. If it is a single threaded application we do not really need to worry much about object synchronization and protecting what might be affected if it is touched by more then two processes, but in case of multithreaded application we need to, take for example files or sound card(s), you just need control over who writes to it, reading obviously is fine if no one is modifying the content of the file/device. In threaded environment you just have to be bit more over protective, since it is very easy to step on other threads data, without the knowledge of the currently owning thread.
How you start:
Let us start first of all in C then we will discuss and see examples from other languages.
It will be easier if we take a real world example to write our threaded application. So to be more meaningful, let us take a house with all member of the house wants to do their own job but the need to use the bathroom. Let us say there is only one bathroom(and obviously people need their privacy, well intimate family members may want to take the bath together and lets exclude them). If person 1 wants to use the bathroom if there is no one else is around he could happily go and use it as he wish, but well obviously locked inside1. you never know who will show up, open the bathroom and you don't want them to be surprised. Anyway now person 2 comes to the bathroom door, finds out door is locked. He has two choices, either wait for person 1 to finish using the bathroom or go back and do what he was doing and check back later. This could be problem and may even delay his access to the bath room if some one else grabs the key once person 1 finished using the bathroom. Everything is depending upon personal choices. So the choices are either you take bath or never take bath or take bath when you get a chance. Every aspect we discussed in this paragraph is all doable in threading. That is the beauty of it.
To be fair and easy to start.
#include <pthread.h>
#include <stdio.h>
void* useBathRoom(void* data)
{
char* key = (char*)data; /* assuming it is C String, that is terminated with '\0'(ASCII 0) */
prinf("Using bathroom: %s", key);
sleep(2); /*Do somthing useful, take bath or do whatever*/
prinf("Used bathroom: %s", key);
return NULL; /* I am done, may be I will be back at different time. */
}
int main(void)
{
pthread_t threadid; /* I want to track who uses the bathroom */
/* Pass this key around, This variable is to safe to share as long is it is used as read only
* otherwise your in deep trouble, why,
* it is a pointer to static allocation, you don't want to corrupt
* your local store(what is this).
*/
char* pStr = "Key";
pthread_create(&threadid, NULL, useBathRoom, (void *)pStr);
/* Let me do some of my own work,
* or keep staring at the bath room door until person 1 comes out,
* he he joking */
printf("I am going to do something productive here\n"); /* Am I? */
/* Don't lock the house and run away, Wait for the person to get out of the bath room */
pthread_join(threadid, NULL);
pthread_exit(0);
/* return 0; Never use return, EVER, Why? */
}
So far what we have seen a basic threaded program. This is not sufficient being a full featured threaded program. So what is next?. We will try to create few more threads, all of them trying to access the bathroom or how they use it and get in the queue. How do track who is using the bathroom and how to prioritize?, That will extend to another topic QoS, We will limit ourselves to just one threading.
Anyway, now we know how to write a threaded program. We will step into synchronizing. How to synchronize and what does that mean? Let us discuss about all the aspect of threading here. In the above example code we only created one thread and a one main process thread initialized by the system at the start of the program. The main program itself is a thread but which we may not realize. Any way, let us say the main thread and the single child want to use the bathroom, they can, but with the rule only one at a time. the thread who, wants to use the bath room should grab the key and lock the bath room after entering. and finish doing what it does, then unlock, get out and handle the key to where it was picked up from. So how is this done. Let us see below.
Will continue tomorrow...



UNITED STATES,