How to create a thread inside a class function? - c++

I am very new to C++.
I have a class, and I want to create a thread inside a class's function. And that thread(function) will call and access the class function and variable as well.
At the beginning I tried to use Pthread, but only work outside a class, if I want to access the class function/variable I got an out of scope error.
I take a look at Boost/thread but it is not desirable because of I don't want to add any other library to my files(for other reason).
I did some research and cannot find any useful answers.
Please give some examples to guide me. Thank you so much!
Attempt using pthread(but I dont know how to deal with the situation I stated 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;
}

You can pass a static member function to a pthread, and an instance of an object as its argument. The idiom goes something like this:
class Parallel
{
private:
pthread_t thread;
static void * staticEntryPoint(void * c);
void entryPoint();
public:
void start();
};
void Parallel::start()
{
pthread_create(&thread, NULL, Parallel::staticEntryPoint, this);
}
void * Parallel::staticEntryPoint(void * c)
{
((Parallel *) c)->entryPoint();
return NULL;
}
void Parallel::entryPoint()
{
// thread body
}
This is a pthread example. You can probably adapt it to use a std::thread without much difficulty.

#include <thread>
#include <string>
#include <iostream>
class Class
{
public:
Class(const std::string& s) : m_data(s) { }
~Class() { m_thread.join(); }
void runThread() { m_thread = std::thread(&Class::print, this); }
private:
std::string m_data;
std::thread m_thread;
void print() const { std::cout << m_data << '\n'; }
};
int main()
{
Class c("Hello, world!");
c.runThread();
}

Related

c++ virtual function and thread [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
here is my main.cpp code:
#include <opencv2/opencv.hpp>
using std::string;
#include "caffe_thread_learn.hpp"
class VideoCaptureTest : public InternalThread {
public:
string video;
explicit VideoCaptureTest(string v) : video(v) { StartInternalThread(); }
protected:
virtual void InternalThreadEntry();
};
void VideoCaptureTest::InternalThreadEntry(){
std::cout << "video child" << std::endl;
}
int main(){
InternalThread* vt = new VideoCaptureTest("/Users/zj-db0655/Documents/data/528100078_5768b1b1764438418.mp4");
delete vt;
return 0;
}
caffe_thread.cpp code:
#include "caffe_thread_learn.hpp"
InternalThread::~InternalThread() {
StopInternalThread();
}
bool InternalThread::is_started() const {
return thread_ && thread_->joinable();
}
bool InternalThread::must_stop() {
return thread_ && thread_->interruption_requested();
}
void InternalThread::StopInternalThread() {
if (is_started()) {
thread_->interrupt();
try {
thread_->join();
} catch (boost::thread_interrupted&) {
} catch (std::exception& e) {
std::cout << "Thread exception: " << e.what();
}
}
}
void InternalThread::StartInternalThread() {
thread_.reset(new boost::thread(&InternalThread::entry, this));
}
void InternalThread::entry() {
InternalThreadEntry();
}
caffe_thread.hpp code
#ifndef caffe_thread_learn_hpp
#define caffe_thread_learn_hpp
#include <stdio.h>
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
namespace boost { class thread; }
class InternalThread {
public:
InternalThread() : thread_() {}
virtual ~InternalThread();
/**
* Caffe's thread local state will be initialized using the current
* thread values, e.g. device id, solver index etc. The random seed
* is initialized using caffe_rng_rand.
*/
void StartInternalThread();
/** Will not return until the internal thread has exited. */
void StopInternalThread();
bool is_started() const;
protected:
/* Implement this method in your subclass
with the code you want your thread to run. */
virtual void InternalThreadEntry() { std::cout << "parent" << std::endl; }
virtual void fun() {}
/* Should be tested when running loops to exit when requested. */
bool must_stop();
private:
void entry();
boost::shared_ptr<boost::thread> thread_;
};
#endif /* caffe_thread_learn_hpp */
actually, the output is:parant
However, i think output should be:video child. Because when StartInternalThread in VideoCaptureTest is called, it will new a thread with parameter (&InternalThread::entry, this), and I think this pointer to VideoCaptureTest and will call VideoCaptureTest's InternalThreadEntry which output video child. However, it output parent.
Thanks!
This is likely a timing issue between your threads. You create a new VideoCaptureTest object then immediately delete it before the thread created in StartInternalThread has a chance to run. When the destructor is called, the object will be torn down to an InternalThread object before the output has been generated.
Either put a sleep between your new/delete in main or wait for the thread to finish before destroying it.

Running a thread

I have a program which I am attempting to run a process in a separate thread. Normally I would use Qt for this, but in this particular instance I can't (due to it going on to an embedded device). My concern is whether or not my current implementation will run the thread correctly, or if it will destroy the thread before processing. Below is my code:
int main(){
//code
Processor *x = new Processor;
//other code
x->startThread(s);
//more code which needs to be running seperately
}
Processor.h
Class Processor {
public:
Processor();
void startThread(std::string s);
void myCode(std::string s);
//more stuff
}
Processor.cpp
void Processor::startThread(std::string s){
std::thread(&Processor::startRecording, s).detach();
}
void Processor::myCode(std::string s){
//lots of code
}
Alternatively, if there is an easier way to start myCode from the main function rather than needing to have the class startThread, please let me know.
I suggest that you make the thread as a Processor attribute.
Run It Online
#include <iostream>
#include <memory>
#include <string>
#include <thread>
//Processor.h
class Processor {
private:
std::shared_ptr<std::thread> _th;
public:
Processor();
void startThread(std::string s);
void joinThread();
void myCode(std::string s);
void startRecording(std::string s);
//more stuff
};
//Processor.cpp
Processor::Processor() {
}
void Processor::startThread(std::string s) {
_th = std::make_shared<std::thread>(&Processor::startRecording, this, s); // "this" is the first argument ;)
}
void Processor::joinThread() {
_th->join();
}
void Processor::myCode(std::string s) {
//lots of code
}
void Processor::startRecording(std::string s) {
std::cout << "msg: " << s << std::endl;
}
// main.cpp
int main(){
//code
auto x = std::make_unique<Processor>();
//other code
x->startThread("hello");
//more code which needs to be running seperately
x->joinThread();
}

Call member function of an object using pthread

How can I call the thread_ready_function into a thread as commented, using pthread ? I need to call it with the class object (In the real world the function uses attributes previously set).
MWE
#include <iostream>
#include <pthread.h>
class ClassA
{
public:
void * thread_ready_function(void *arg)
{
std::cout<<"From the thread"<<std::endl;
pthread_exit((void*)NULL);
}
};
class ClassB
{
ClassA *my_A_object;
public:
void test(){
my_A_object = new ClassA();
my_A_object->thread_ready_function(NULL);
// my_A_object->thread_ready_function(NULL);
// ^
// I want to make that call into a thread.
/* Thread */
/*
pthread_t th;
void * th_rtn_val;
pthread_create(&th, NULL, my_A_object.thread_ready_function, NULL);
pthread_join(th, &th_rtn_val);
*/
}
};
int main()
{
ClassB *my_B_object = new ClassB();
my_B_object->test();
return 0;
}
if you don't want to use C++11 or stl or boost, you must use the static key word for your member function,so that the pthread can call your member function!
example code:
#include <iostream>
#include <pthread.h>
using namespace std;
class A{
public:
static void* thread(void* args);
int parella_thread(int thread_num);
};
void* A::thread(void* args)
{
cout<<"hello world"<<endl;
}
int A::parella_thread(int thread_num)
{
pthread_t* thread_ids = new pthread_t[thread_num];
for(int i=0;i<thread_num;i++)
{
pthread_create(&thread_ids[i],NULL,thread,(void*)NULL);
}
delete[] thread_ids;
}
int main(int argc,char*argv[])
{
A test;
test.parella_thread(4);
return 0;
}

Function calls with class members?

Before I present the code which is found at the bottom of this post I would like to talk about the issue and the fix's that I do not desire. Okay basically I've created a GUI from scratch sort of and one requirement I wanted for this was allow components to have their own click executions so if i click a button or tab etc.. It would call Component->Execute(); Well normally you would do something like a switch statement of ids and if that components ID equaled n number then it would perform this action. Well that seemed kinda dumb to me and I thought there has to be a better way. I eventually tried to incorporate a feature in JAVA where you would do like Component.AddActionListener(new ActionListener( public void execute(ActionEvent ae) { })); or something like that and I thought that this feature has to be possible in C++. I eventually came across storing void functions into a variable in which could be executed at any time and modified at any time. However I hadn't noticed an issue and that was this only worked with static functions. So below you'll see my problem. I've patched the problem by using a pointer to SomeClass however this would mean having an individual function call for every class type is there no way to store a function callback to a non-static class member without doing the below strategy? and instead doing a strategy like the commented out code?
//Main.cpp
#include <iostream> //system requires this.
#include "SomeClass.h"
void DoSomething1(void)
{
std::cout << "We Called Static DoSomething1\n";
}
void DoSomething2(void)
{
std::cout << "We Called Static DoSomething2\n";
}
int main()
{
void (*function_call2)(SomeClass*);
void (*function_call)() = DoSomething1; //This works No Problems!
function_call(); //Will Call the DoSomething1(void);
function_call = DoSomething2; //This works No Problems!
function_call(); //Will Call the DoSomething2(void);
SomeClass *some = new SomeClass(); //Create a SomeClass pointer;
function_call = SomeClass::DoSomething3; //Static SomeClass::DoSomething3();
function_call(); //Will Call the SomeClass::DoSomething3(void);
//function_call = some->DoSomething4; //Non-Static SomeClass::DoSomething4 gives an error.
//function_call(); //Not used because of error above.
function_call2 = SomeClass::DoSomething5; //Store the SomeClass::DoSomething(SomeClass* some);
function_call2(some); //Call out SomeClass::DoSomething5 which calls on SomeClass::DoSomething4's non static member.
system("pause");
return 0;
}
//SomeClass.hpp
#pragma once
#include <iostream>
class SomeClass
{
public:
SomeClass();
~SomeClass();
public:
static void DoSomething3(void);
void DoSomething4(void);
static void DoSomething5(SomeClass* some);
};
//SomeClass.cpp
#include "SomeClass.h"
SomeClass::SomeClass(void)
{
}
SomeClass::~SomeClass(void)
{
}
void SomeClass::DoSomething3(void)
{
std::cout << "We Called Static DoSomething3\n";
}
void SomeClass::DoSomething4(void)
{
std::cout << "We Called Non-Static DoSomething4\n";
}
void SomeClass::DoSomething5(SomeClass *some)
{
some->DoSomething4();
}
Secondary Fix for what I'll do not an exact answer I wanted but it meets my needs for now along with allowing additional features which would have become overly complicate had this not existed.
//Component.hpp
#pragma once
#include <iostream>
#include <windows.h>
#include <d3dx9.h>
#include <d3d9.h>
#include "Constants.hpp"
#include "ScreenState.hpp"
#include "ComponentType.hpp"
using namespace std;
class Component
{
static void EMPTY(void) { }
static void EMPTY(int i) { }
public:
Component(void)
{
callback = EMPTY;
callback2 = EMPTY;
callback_id = -1;
}
Component* SetFunction(void (*callback)())
{
this->callback = callback;
return this;
}
Component* SetFunction(void (*callback2)(int), int id)
{
this->callback_id = id;
this->callback2 = callback2;
return this;
}
void execute(void)
{
callback();
callback2(callback_id);
}
}
The syntax for pointers-to-member-functions is as follows:
struct Foo
{
void bar(int, int);
void zip(int, int);
};
Foo x;
void (Foo::*p)(int, int) = &Foo::bar; // pointer
(x.*p)(1, 2); // invocation
p = &Foo::zip;
(x.*p)(3, 4); // invocation
Mind the additional parentheses in the function invocation, which is needed to get the correct operator precedence. The member-dereference operator is .* (and there's also ->* from an instance pointer).

Program fails when using my thread implementation?

This is my first post on your forum, but I'm not quite sure if I'm asking the right place? - Can I post C++ questions in this section, or is this just like a general programming section?
Anyway, enough of my noobish doubts, let's get to my problem :).
In my .h file (thread.h) I have a struct (RUNNABLE) and a class (thread).
'RUNNABLE' is like the interface you implement and override, or at least you override its virtual 'run()' void. You then create a 'thread' instance and call its 'start(void* ptr)' function, to start the thread. You pass in an object instance which has RUNNABLE as the base class as the parameter for the 'start' function.
This all seems great, but my implementation crashes my program.
Here's thread.h:
#include <process.h>
struct RUNNABLE{
virtual void run() = 0;
};
class thread{
public:
void start(void *ptr){
DWORD thr_id;
HANDLE thr_handl = (HANDLE)_beginthreadex(NULL, 0, thread_proc, ptr, 0, (unsigned int*)&thr_id);
}
private:
static unsigned int __stdcall thread_proc(void *param){
((RUNNABLE*)param)->run();
ExitThread(0);
return 0;
}
};
And this is my example implementation:
class test : RUNNABLE{
virtual void run(){
while(true){
dbText(0, 0, "hej");
}
}
};
test *obj = new test();
thread th;
th.start(obj);
And the program simply just crashes when I open it.
Help is appreciated :).
Best regards,
Benjamin.
test *obj = new test();
This is a memory management problem. When does obj get deleted? It can take a while for the thread to actually start running, the object needs to stay around long enough. I'm guessing you've got some code that's not in the snippet that is deleting that object again. Too soon.
The only code that can safely and accurately delete the object is the thread itself.
I tested it and it runs just fine, probably your dbText thingy there that crashes, I replaced it with printf("hejdå").
This runs fine for me:
#include <iostream>
#include <process.h>
#include <windows.h>
struct RUNNABLE{
virtual void run() = 0;
};
class thread{
public:
void start(void *ptr){
DWORD thr_id;
HANDLE thr_handl = (HANDLE)_beginthreadex(NULL, 0, thread_proc, ptr,
0, (unsigned int*)&thr_id);
}
private:
static unsigned int __stdcall thread_proc(void *param){
((RUNNABLE*)param)->run();
std::cout << "ending thread\n";
::ExitThread(0);
return 0;
}
};
class test : RUNNABLE{
virtual void run(){
for(unsigned int u=0; u<10; ++u){
std::cout << "thread\n";
}
}
};
int main()
{
test *obj = new test();
thread th;
th.start(obj);
std::cout << "giving thread some time\n";
::Sleep(5000);
std::cout << "ending process\n";
return 0;
}
However, you should probably call _endthreadex() (instead of EndThread()) to end the thread.