Cpp Multi-Threading : Different ways to create a thread in C++

In the previous chapter we saw an introduction to threads and how thread is usedul to increase the execution time.

In this chapter we shall see in how many ways we can create a thread.

There are 5 different ways to create a thread:

1. Function Pointers
2. Lambda Functions
3. Functors
4. Member Functions
5. Static Member functions

For all the above thread creation types, if there are multiple threads created, then we cannot say which thread got created first and which thread is going to be created at the last.

Method 1: Creating a thread using Function Pointers

#include <iostream>
#include <thread>

// for more tutorial in C++ visit www.prodevelopertutorial.com

using namespace std;

void fun(int num)
{
    while(num > 0)
    {
        cout << num<<endl;
        num--;
    }
}
int main(void)
{
	// creating a thread,
	// we are sending function pointer followed by function arguments.

    std::thread t1(fun, 5);

    // we use .join(), to join this thread to the main thread.    
    t1.join();
    return 0;
}

Method 2: Creating a thread using Lambda Functions

#include <iostream>
#include <thread>

// for more tutorial in C++ visit www.prodevelopertutorial.com

using namespace std;


int main(void)
{
    auto fun = [ ] (int num) {
                    
                    while(num > 0)
                    {
                        cout << num<<endl;
                        num--;
                    }
                };

    std::thread t1(fun, 5);
    
    t1.join();
    return 0;
}

We have created a lambda function as below:

    auto fun = [ ] (int num) {
                    
                    while(num > 0)
                    {
                        cout << num<<endl;
                        num--;
                    }
                };

Then we are calling the thread as “std::thread t1(fun, 5);”.

Method 3: Creating a thread using Functors

Functors are also called as function objects.

We can create a thread using functors as below:

#include <iostream>
#include <thread>

// for more tutorial in C++ visit www.prodevelopertutorial.com

using namespace std;

class MyClass
{
    public:
        
        void operator() (int num)
        {
            while(num > 0)
            {
                cout << num<<endl;
                num--;
            }            
        }
};

int main(void)
{

    std::thread t1(MyClass(), 5);
    
    t1.join();
    return 0;
}

Method 4: Creating a thread using Non Static Member Functions

#include <iostream>
#include <thread>

// for more tutorial in C++ visit www.prodevelopertutorial.com

using namespace std;

class MyClass
{
    public:
        
        void myFun (int num)
        {
            while(num > 0)
            {
                cout << num<<endl;
                num--;
            }            
        }
};

int main(void)
{
    MyClass obj;
    
    //syntax to create a thread using non static member function:
    // arg 1: you need to send the address of the function to be called
    // arg 2: send the address of the object to be attached
    // arg 3: send the function arguments
    // t1(&MyClass::myFun, &obj, 5);
    
    std::thread t1(&MyClass::myFun, &obj, 5);
    
    t1.join();
    return 0;
}

Method 5: Creating a thread using Static Member functions

#include <iostream>
#include <thread>

// for more tutorial in C++ visit www.prodevelopertutorial.com

using namespace std;

class MyClass
{
    public:
        
        static void myFun (int num)
        {
            while(num > 0)
            {
                cout << num<<endl;
                num--;
            }            
        }
};

int main(void)
{
    MyClass obj;
    
    //syntax to create a thread using static member function:
    // In case of static member function, you can call the function without creating an object. 
    // Hence there is no need to send the address of the object
    // arg 1: you need to send the address of the function to be called
    // arg 2: send the function arguments
    // t1(&MyClass::myFun, &obj, 5);

    std::thread t1(&MyClass::myFun, 5);
    
    t1.join();
    return 0;
}
Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *