thread safe or not???# Programming - 葵花宝典
p*u
1 楼
Please take a look at following Pseudocode. I don't think
it's thread-safe. any ideas how to make it thread-safe?
class A
{
public:
A() : ptr(NULL) { };
void startup(B b, int i)
{
static C c(i);
ptr = &c;
b.dostuff(ptr);
}
private:
const C * ptr;
};
class B
{
public:
B() { };
/*
* value that c points to is NOT copied,
* so it needs to stay valid during
* muliple invocations of dostuff
it's thread-safe. any ideas how to make it thread-safe?
class A
{
public:
A() : ptr(NULL) { };
void startup(B b, int i)
{
static C c(i);
ptr = &c;
b.dostuff(ptr);
}
private:
const C * ptr;
};
class B
{
public:
B() { };
/*
* value that c points to is NOT copied,
* so it needs to stay valid during
* muliple invocations of dostuff