简体   繁体   中英

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);
    }
}

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM