What is Message Passing in C++?
The object-oriented programming (oops) concepts in C++ involves the use of objects which are real-life instances of classes. Message passing in C++ is the communication between two or more objects using a logical entity called a message.
What is a Message?
- A message is a form of request that is made to an object to perform a function specific to the object to which the request was made.
- A message is a form of communication between objects that make an object perform a task for the system.
- The message is an abstract entity and will not hold the information of the function that is going to be performed and only plays the role of invoking the function.
- A message is a part of OOPS concepts in c++.
Examples
The Object-oriented programming concept implies objects as real-life entities that are represented using code to combine data and functions performed using that data. Message passing is a part of oops concepts and is applicable in most of the real-life scenarios implemented using the oops concepts. Consider a lottery machine as an example, when you press the button to fetch your lottery, a function is generated to communicate with the machine and call the function to provide a lottery. We can implement the above real-life example using C++ as follows,
In the above example, the lottery machine is represented by the class Lottery_Machine and the lottery machine has the function to provide a lottery number. The user is represented by the User class and has a user_name and user_id and a call_lottery function which calls the function in the Lottery_Machine class. The main function has the real-life instances of the class called objects for the user and lottery machine which are used by the call_lottery function to pass the message that the get_lottery() function must be called to provide the lottery number to the user.
++
The message in the above example will be the invocation of the get_lottery() function and the message is passed by the Bob object of the User class.
The output of the above code will be,
The rand() function returns a integer value between 0 and RAND_MAX. The minimum value of RAND_MAX is 32767 and may vary during program execution.
The User(string name, int id) function in the above example is called a constructor and is a special function that must have the same name as the name of the class. The function is invoked when the User object, Bob is created in the main function. This is one of the fundamental concepts of OOPS in C++ and is used to initialize variables during the creation of the object.
How it Works?
Message passing in C++ works by the implementation of the following steps,
- Creating a class and object for the class.
- Communication between objects can be made with the help of a message.
- A message is passed to an object which will invoke the function in another object.
- Thus, communication between two objects is established using a message.
Writing Framework for Inter Thread Message Passing in C++
A thread is used to represent a part of the process. Threads are similar to objects when applying the OOPS concept of C++. A thread is an execution unit used to execute code in the system. Threads can communicate with each other by using the inter-thread Message Passing in C++. The following code illustrates the communication between two threads using messages,
In the above example, there are two functions called producer and consumer.
- The condition_variable class provides functions to put the thread into a waiting state based on a condition.
- A mutex object is used to lock the concurrent functioning of two threads and unlock them using separate functions. The unique_lock function is used to lock and unlock the mutex.
- If the current thread is locked and paused execution, then the notify_one() function unlocks that thread and resumes execution.
- The execution of the thread can be paused by using the sleep_for() function for 8 milliseconds using the crono library.
- The wait() function stops the execution of the current thread until the notify function is called or the condition given as 2nd argument is found to be true.
- The join() method is used to prevent the two threads to be running at the same time.
The message passing in the above example is done using the notify_one() and wait() functions which generate a message and invokes the respective function to put the thread in a state of paused or running. The mutex object also uses the unlock() function to perform message passing to unlock the mutex.
The output is,
Difference Between a Method Call and Message Passing
The message passing in C++ is an abstract version of the term method call. Technically, there are no differences between these two terms. The term method call is used to depict a single method that is invoked directly. The term message passing also indirectly depicts the invoking of a function and is used to illustrate the communication between two objects.
Messaging Design Patterns
The message designing patterns are used to create a middle layer that provides the abstraction while transferring the message from the sender to the receiver. The message design patterns are used to implement the OOPS concepts such as encapsulation and modular programming to message passing in C++. This makes the system easy to understand for the end user or the receiver as all the logic is hidden from the end user.
a. Strategy Pattern
The strategy pattern allows the interchangeability of the algorithm during run time. This is possible by encapsulating the family of algorithms and making them interchangeable.
Learn more
a.What is Object Oriented Programming
The methodology of using class and object to create more abstract code with features like encapsulation, inheritance, etc.. is called object-oriented programming. Learn more about object-oriented programming
b.Operator-Overloading
Object-oriented programming in C++ provides a feature to use operators such as +,- etc.. to perform functions rather than simple addition and subtraction by overloading the operators with more functions. Explore more on Operator-Overloading in C++
c.Templates
Generic programming is made possible in C++ with the help of templates. A template can allow writing functions and class members without data types. The data type of the function and members is passed as a parameter to the template of the function. Explore on Templates in C++.
Conclusion
- Message passing in C++ is the method of communication between two or more objects.
- A message is used to communicate with other objects by invoking the function of other objects.
- A real-life entity modeled using the oops concept in C++ provides an example of message passing.
- A Thread can communicate with other threads using the inter-thread message passing in C++.
- There is no technical difference between a method call and message passing.
- The design patterns are used to create an intermediatory abstraction layer for the end users.