Please consider this sample C++ code:
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <thread>
using namespace std;
class TriCycle{
private:
int YearBuilt;
int Speed;
public:
int getSpeed() { return Speed; }
TriCycle(int x);
void Pedal();
};
TriCycle::TriCycle(int x){
Speed = x;
puts("Entering front yard TriCycle Race..");
}
void TriCycle::Pedal(){
puts("Pedalling..");
Speed++;
}
void sayhi(){
puts("Hi!");
}
int main(){
TriCycle Wichita(3);
TriCycle * PtrWichita = &Wichita;
printf("Current Speed %d\n", PtrWichita->getSpeed());
thread t(sayhi);
PtrWichita->Pedal();
printf("Current Speed %d\n", PtrWichita->getSpeed());
PtrWichita->Pedal(); // <-- this works
printf("Current Speed %d\n", PtrWichita->getSpeed());
//thread t3(PtrWichita->Pedal,this); // <-- fails
//t3.join(); // <- this fails
t.join();
}
I am trying to run my Pedal member function inside a thread, apparently I am not doing this right. I am confused since PtrWichita->Pedal()
seems to run OK.
Kindly show me how can I PtrWichita->Pedal()
method inside a thread? Thanks in advance!