Call overloaded operator from thread

Yes it is possible to call overloaded operator with thread. Here are some of the commonly used methods.

Let check with example where we shall try to compare two objects (sObj & newSuggest) of Suggest class.


#include <thread>
using namespace std;

class Suggest
    string m_value;
        cout<<"Constructor(default) :: Called"<<endl;
        m_value="Default Suggestion Given";
    Suggest(string str)
        cout<<"Constructor(string) :: Called"<<endl;
    //Overload == operator
    bool operator==(const Suggest& obj)const
        if(this ->m_value == obj.m_value)
            cout<<"SAME OBJECTS"<<endl;
            return true;
        cout<<"DIFFERENT OBJECT"<<endl;
        return false;
    void display()
        cout<<"Suggestion:: "<<m_value<<endl;
int main()

    Suggest sObj("TechSujhav");

    //Call Overloaded operator== from thread
    cout<<"\nCall Overloaded operator== from thread"<<endl;

    Suggest newSuggest;

    std::thread objThrd2 ((&Suggest::operator==),sObj,newSuggest);

    cout<<"\nCall Overloaded operator== using lambda from thread"        <<endl;

    //Call Overloaded operator== using lambda from thread
    //passing argument in Capture list

    std::thread objThrd3 ([sObj,newSuggest](){
        if(sObj == newSuggest)
               cout<<"Perform Activity-1"<<endl;
               cout<<"Perform Activity-2" <<endl;
    return 0;

Command: g++ equalToOperator.cpp -o equalToOperator -lpthread

Output: ./equalToOperator

Constructor(string) :: Called

Suggestion:: TechSujhav

Call Overloaded operator== from thread
Constructor(default) :: Called
Suggestion:: Default Suggestion Given


Call Overloaded operator== using lambda from thread
Perform Activity-2


PREV:: How to call functor from thread

Your Comments /Suggestions & Questions are always welcome. 

We would like to help you with best of our knowledge.

So feel free to put Questions

No comments:

Post a Comment