Roland Sams
Roland Sams

Reputation: 173

Creating a thread in another class

I am trying to create a thread in the main function for the function named thefunction() in the ThreadMe class. The tricky part is that I need to start a thread in another class TYIA -Roland

#include <iostream>
#include <process.h>
#include <windows.h>

int main()  {

    char cincatcher[24];

        std::cout << "I want to run a thread using a function on another class\n";

//      Here is a good place to start the thread

        while( true )   {


        std::cin >> cincatcher
    }
}


class ThreadMe  {

    void thefunction();
};

void ThreadMe::thefunction()    {

    while( true )   {

        std::cout << "working!\n"
        Sleep(800);
    }
}

Upvotes: 0

Views: 83

Answers (1)

TieDad
TieDad

Reputation: 9879

You cannot start thread directly with a class method. You must wrap the class method into a normal function, then start thread with the function. Like the following:

void threadBody(void *p) {
     ThreadME tm;
     tm.thefunction();
}

Upvotes: 1

Related Questions