by author

Threads in C++. Watchdog app. Simple approach.

In this article I will demonstrate simple application, which will allow you understanding the concept of threading in computer science (here in C++ running in Linux). Please note, we will discuss only the basic of threading domain, however good understanding the principles of application we are going to through give you for sure great advantage to deploy following concept in your own programs.

by author
std::thread your_thread (functionToBeCalled); 
// spawn new thread that calls functionToBeCalled()
your_thread.join();
/pseudo-codeshared_memory;
mutex; //controls access to share_memory
def your_thread_1(funct_1);
//definition of thread which calls function funct_1
def your_thread_2(funct_2);
//definition of thread which calls function funct_2
//your_thread_1 and your_thread_2 runs independently and fights for access to share_memory// the share_memory is available for one that at the time.
// the control for the availability is performed by mutex
funct_1 () {
mutex.lock();
call (sheared_memory) // your_thread_2 DO NOT have an access to //memory
mutex.unlock ()
}
funct_2 () {
mutex.lock();
call (sheared_memory) // your_thread_1 DO NOT have an access to //shared_memory
mutex.unlock ()
}
g++ watchdog_medium.cpp -o watchdog -pthread
./watchdog
by author