I am very new to C ++.
I have a class and I want to create a stream inside a class function. And this thread (function) will call and get access to the class function and variable. At first I tried to use Pthread, but worked only outside the class, if I want to access the function / variable of the class, I got an error outside the scope. I am looking at Boost / thread, but this is undesirable because I do not want to add any other library to my files (for another reason).
I did some research and did not find any useful answers. Please give some examples to help me. Thank you very much!
Trying to use pthread (but I donβt know how to deal with the situation I mentioned above):
#include <pthread.h> void* print(void* data) { std::cout << *((std::string*)data) << "\n"; return NULL; // We could return data here if we wanted to } int main() { std::string message = "Hello, pthreads!"; pthread_t threadHandle; pthread_create(&threadHandle, NULL, &print, &message); // Wait for the thread to finish, then exit pthread_join(threadHandle, NULL); return 0; }
source share