Hello,
In java I have the following generated code:
public class B {
public void exec(){
X x = (X) Thread.currentThread();
System.out.println(x.value);
}
}
public class X extends Thread{
public int value;
public X(int x){
value = x;
}
public void run(){
B b = new B();
b.exec();
}
}
new X(4).start();
The B exec() method retrieves the field value corresponding to the current Thread (also instance of the class X).
Is there a way to simulate the same behavior in C++? Note: I wouldn't like to pass x as a parameter to the B instance because the code is generated.
class B {
public:
void exec();
};
class X {
public:
int value;
X(int x) {
value = x;
}
void run() {
B * b = new B();
b->exec();
}
};
void B::exec() {
std::cout << ??? << std::endl;
}
int main() {
X * x = new X(3);
boost::thread thr(boost::bind(&X::run, x));
thr.join();
}
I don't know how to retrieve the class instance related to the thread ( I know I don't have one ), any ideas?