multithreading

 #include <iostream>

#include <thread> 

#include<unistd.h>

using namespace std;

void threadFunc()

{

cout << "Welcome to Multithreading" << endl;


}

int main()

{

//pass a function to thread

while(1)

{

thread funcTest1(threadFunc);

funcTest1.join();

sleep(3);

}

}


//---------------------------------------------------------program1-----


#include <iostream>

#include <thread>

#include<unistd.h>

class DisplayThread

{

public:

    void operator()()     

    {

        for(int i = 0; i < 2; i++)

            std::cout<<"Display Thread Executing"<<std::endl;

            sleep(4);

    }

};

 

int main()  

{

    std::thread threadObj( (DisplayThread()) );

     sleep(5);

    //  for(int i = 0; i < 3; i++)

      //  std::cout<<"Display From Main Thread "<<std::endl;

      //  sleep(5);

  //  std::cout<<"Waiting For Thread to complete"<<std::endl;

    threadObj.join();

     sleep(5);

    std::cout<<"Exiting from Main Thread"<<std::endl;

    return 0;

}


//-------------------------------------------------------


https://thispointer.com/c-11-multithreading-part-1-three-different-ways-to-create-threads/


//-------------------------------------------------------


#include <iostream>

#include <thread>

int main()  

{

    int x = 9;

    std::thread threadObj([]{

            for(int i = 0; i < 3; i++)

                std::cout<<"Display ---------Thread Executing"<<std::endl;

            });

            

    for(int i = 0; i < 1; i++)

        std::cout<<"Display From Main Thread"<<std::endl;

        

    threadObj.join();

    std::cout<<"Exiting from Main Thread"<<std::endl;

    return 0;

}


//------------------------------------------



#include <iostream>

#include <thread>

#include<vector>

#include <algorithm>

class WorkerThread

{

public:

    void operator()()     

    {

        std::cout<<"Worker Thread "<<std::this_thread::get_id()<<" is Executing"<<std::endl;

    }

};

int main()  

{

    std::vector<std::thread> threadList;

    for(int i = 0; i < 10; i++)

    {

        threadList.push_back( std::thread( WorkerThread() ) );    //vector

    }

    // Now wait for all the worker thread to finish i.e.

    // Call join() function on each of the std::thread object

    std::cout<<"wait for all the worker thread to finish"<<std::endl;

    std::for_each(threadList.begin(),threadList.end(), std::mem_fn(&std::thread::join));  //join all thread

    std::cout<<"Exiting from Main Thread"<<std::endl;

    return 0;

}

//----------------------------------------------------------------------------------



#include<iostream>

#include<thread>

#include<mutex>

class Application

{

std::mutex m_mutex;

bool m_bDataLoaded;

public:

Application()

{

m_bDataLoaded = false;

}

void loadData()

{

// Make This Thread sleep for 1 Second

std::this_thread::sleep_for(std::chrono::milliseconds(1000));

std::cout<<"Loading Data from XML"<<std::endl;

// Lock The Data structure

std::lock_guard<std::mutex> guard(m_mutex);

// Set the flag to true, means data is loaded

m_bDataLoaded = true;

}

void mainTask()

{

std::cout<<"Do Some Handshaking"<<std::endl;

// Acquire the Lock

m_mutex.lock();

// Check if flag is set to true or not

while(m_bDataLoaded != true)

{

// Release the lock

m_mutex.unlock();

//sleep for 100 milli seconds

std::this_thread::sleep_for(std::chrono::milliseconds(100));

// Acquire the lock

m_mutex.lock();

}

// Release the lock

m_mutex.unlock();

//Doc processing on loaded Data

std::cout<<"Do Processing On loaded Data"<<std::endl;

}

};

int main()

{

Application app;

std::thread thread_1(&Application::mainTask, &app);

std::thread thread_2(&Application::loadData, &app);

thread_2.join();

thread_1.join();

return 0;

}


//=======================================

Comments

Popular posts from this blog

Difference between Structure and Array in C

Mutitreading Notes