由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 谁给改一个线程安全的smarter pointer类
相关主题
C++ Singleton的实现Palantir 2nd coding interview [pass, set for on-site]
来问一个关于smart pointer的弱问题谁给讲讲test-and-set怎么实现mutex?
CareerCup 13.9的solution有memory leaknvidia面筋
问一个smart pointer的问题面试题
问个关于c++ auto_pter的问题pthread 编程还是要看看阿
攒人品。面试经历(2)贡献一道湾区小公司的面试题 Medallia
问道多线程的简单题目read-write locker的实现
multi-threading guru们请教一下那道H2O的题
相关话题的讨论汇总
话题: pref话题: pval话题: mutex话题: const
进入JobHunting版参与讨论
1 (共1页)
w****x
发帖数: 2483
1
template
class SmartPointer
{
public:
SmartPointer(const SmartPointer& p) { addRef(p); }
SmartPointer(T* p) { assign(p); }
~SmartPointer() { decRef(); }
public:
SmartPointer& operator = (const SmartPointer& p)
{
if (this != &p)
{
decRef();
addRef(p);
}
return *this;
}
T* operator ->() const { return m_pVal; }
T& operator *() const { return *m_pVal; }
private:
void decRef()
{
(*m_pRef)--;
if (0 == *m_pRef)
delete m_pVal;
}
void addRef(const SmartPointer& ptr)
{
m_pRef = ptr.m_pRef;
m_pVal = ptr.m_pVal;
(*m_pRef)++; // not *m_pRef++ !!
}
void assign(T* p)
{
m_pVal = p;
m_pRef = new int(1);
}
private:
T* m_pVal;
int* m_pRef;
};
不会做啊,好象很难的样子。特别是如果析构和operator =同时在不同的线程里调用
g*********e
发帖数: 14401
2
OP你是女的吗?
w****x
发帖数: 2483
3

很重要吗?

【在 g*********e 的大作中提到】
: OP你是女的吗?
i******e
发帖数: 273
4
我认为可以用mutex或semaphore把reference counter保护起来。
private:
T* m_pVal;
int* m_pRef;
static pthread_mutext_t mutex = PTHREAD_MUTEX_INITIALIZER;
每次试图访问m_pRef时先要acquire mutex, 访问结束release.
void addRef(const SmartPointer& ptr)
{
pthread_mutex_lock(&mutex);
m_pRef = ptr.m_pRef;
m_pVal = ptr.m_pVal;
(*m_pRef)++; // not *m_pRef++ !!
pthread_mutex_unlock(&mutex);
}
mutex是互斥锁,如果你想允许多线程同时读,还可以用readwrite_lock.
w****x
发帖数: 2483
5

mutex是哪来的?? 什么时候销毁??
如果mutex是多个smart pointer共享的, 那么一个在调用operator =的时候中断, 执行
另一个线程的析构函数,如果这时counter是0, 销毁指针也同时销毁mutex, 那么继续执
行operator =的时候mutex就会是无效的.

【在 i******e 的大作中提到】
: 我认为可以用mutex或semaphore把reference counter保护起来。
: private:
: T* m_pVal;
: int* m_pRef;
: static pthread_mutext_t mutex = PTHREAD_MUTEX_INITIALIZER;
: 每次试图访问m_pRef时先要acquire mutex, 访问结束release.
: void addRef(const SmartPointer& ptr)
: {
: pthread_mutex_lock(&mutex);
: m_pRef = ptr.m_pRef;

i******e
发帖数: 273
6
这里的mutex是static类型的,你不用在dtor中销毁,当整个程序结束操作系统会clean
up static memory的。
static 类成员是由SmartPointer类对象所有实例所共享的,它只有一个copy, 所以不
论哪个实例的assignment operator 或 copy ctor 被调用, 只能等mutex。它的作用
就是保护reference counter在多线程异步访问时避免出现data inconsistency。
w****x
发帖数: 2483
7

clean
所有的smart pointer公用一个static的mutex会不会效率太低了? 不过好像也找不到更
好的解决办法了...

【在 i******e 的大作中提到】
: 这里的mutex是static类型的,你不用在dtor中销毁,当整个程序结束操作系统会clean
: up static memory的。
: static 类成员是由SmartPointer类对象所有实例所共享的,它只有一个copy, 所以不
: 论哪个实例的assignment operator 或 copy ctor 被调用, 只能等mutex。它的作用
: 就是保护reference counter在多线程异步访问时避免出现data inconsistency。

a*****n
发帖数: 158
8
显然用STATIC 变量是错误的,,大致说下啊,
在这里,,一个class的实例里面有2个PRIVATE的变量,,那么THREAD-SAFE的功能就是
保护这2个变量。。。在构造函数里面直接初始化MUTEX,,然后在惜购里面删除MUTEX,每
次访问这2个私有变量的时候,,要用MUTEX来保护,,防止不同线程同时访问/改变这2
个私有变量。。。
用STATIC的MUTEX不是不可以,,但是效率低,,意味着一个SMART POINTER CLASS的不
同实例也不能同时访问/修改。。。
w****x
发帖数: 2483
9

这2
这个问题怎么解决:
如果mutex是多个smart pointer共享的, 那么一个在调用operator =的时候中断, 执行
另一个线程的析构函数,如果这时counter是0, 销毁指针也同时销毁mutex, 那么继续执
行operator =的时候mutex就会是无效的.

【在 a*****n 的大作中提到】
: 显然用STATIC 变量是错误的,,大致说下啊,
: 在这里,,一个class的实例里面有2个PRIVATE的变量,,那么THREAD-SAFE的功能就是
: 保护这2个变量。。。在构造函数里面直接初始化MUTEX,,然后在惜购里面删除MUTEX,每
: 次访问这2个私有变量的时候,,要用MUTEX来保护,,防止不同线程同时访问/改变这2
: 个私有变量。。。
: 用STATIC的MUTEX不是不可以,,但是效率低,,意味着一个SMART POINTER CLASS的不
: 同实例也不能同时访问/修改。。。

n******t
发帖数: 4406
10
什么公司要你做这种东西?纯粹是二杆子公司啊。。

【在 w****x 的大作中提到】
:
: 这2
: 这个问题怎么解决:
: 如果mutex是多个smart pointer共享的, 那么一个在调用operator =的时候中断, 执行
: 另一个线程的析构函数,如果这时counter是0, 销毁指针也同时销毁mutex, 那么继续执
: 行operator =的时候mutex就会是无效的.

1 (共1页)
进入JobHunting版参与讨论
相关主题
请教一下那道H2O的题问个关于c++ auto_pter的问题
C++ 实现读写锁的问题 (vmware电面考过)攒人品。面试经历(2)
请教大牛用mutex lock实现reader writer lock问道多线程的简单题目
碰到面试官水平太差看不懂答案怎么办?multi-threading guru们
C++ Singleton的实现Palantir 2nd coding interview [pass, set for on-site]
来问一个关于smart pointer的弱问题谁给讲讲test-and-set怎么实现mutex?
CareerCup 13.9的solution有memory leaknvidia面筋
问一个smart pointer的问题面试题
相关话题的讨论汇总
话题: pref话题: pval话题: mutex话题: const