返回信息流在一个单例模式里用Spinlock怎么写。。。
传送门:http://sfau.lt/bN1Sk
这是一条镜像帖。来源:北邮人论坛 / cpp / #71365同步于 2013/5/30
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
[问题] 问一个Spinlock的问题
Wizmann
2013/5/30镜像同步16 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
我就是用的static + 双重检查。。。
只是不懂双重检查的Spinlock怎么写。。。
写完编译不通过啊啊啊。。。
求神牛改改我的代码。。。应该不难。。。OrzOrz。。。
谢谢啦~~
【 在 a206206 的大作中提到: 】
: 不懂。
: 可以用静态成员+criticalsection
把mutex改成spinlock怎么写。。。
关键是spinlock需要初始化(吧?)。。。
#ifndef SINGLETON
#define SINGLETON
class CSingleton
{
public:
static CSingleton* getInstance()
{
if(!uniqueInstance)
{
pthread_mutex_lock(&mutex);
if(!uniqueInstance)
{
uniqueInstance = new CSingleton();
}
pthread_mutex_unlock(&mutex);
}
return uniqueInstance;
}
void fill(int x){ val += x; }
int getVal(){ return val; }
private:
int val;
CSingleton()
{
val = 0;
}
CSingleton(const CSingleton&){}
CSingleton & operator = (const CSingleton&);
static CSingleton * uniqueInstance;
static pthread_mutex_t mutex;
};
CSingleton * CSingleton::uniqueInstance = NULL;
pthread_mutex_t CSingleton::mutex = PTHREAD_MUTEX_INITIALIZER;
#endif
#ifndef SINGLETON
#define SINGLETON
#include <linux/spinlock>
class CSingleton
{
public:
static CSingleton* getInstance()
{
if(!uniqueInstance)
{
pthread_spin_lock(lock);
if(!uniqueInstance)
{
uniqueInstance = new CSingleton();
}
pthread_spin_unlock(&lock);
}
return uniqueInstance;
}
void fill(int x){ val += x; }
int getVal(){ return val; }
private:
int val;
CSingleton()
{
val = 0;
//pthread_spin_init(&lock, 0);
}
CSingleton(const CSingleton&){}
CSingleton & operator = (const CSingleton&);
static CSingleton * uniqueInstance;
static pthread_spinlock_t lock;
};
CSingleton * CSingleton::uniqueInstance = NULL;
// 静态初始化在这里~
pthread_spinlock_t CSingleton::lock = SPIN_LOCK_UNLOCKED; //这个宏函数就是对lock成员进行初始化~如果编译出错的话参考下pthread_spinlock_t的初始化函数里的内容吧~
#endif
凭记忆手动改的~具体代码名称有出入的话智能感知下吧- -
【 在 Wizmann 的大作中提到: 】
: 把mutex改成spinlock怎么写。。。
: 关键是spinlock需要初始化(吧?)。。。
: [code=c]
: ...................
好厉害!
不过弱弱问下spinlock在第一次使用的时候貌似没有初始化吧。。。
【 在 tonyjansan 的大作中提到: 】
: [code=c]
: #ifndef SINGLETON
: #define SINGLETON
: ...................
应该吧那个注释开启~因为我不很确定那个未加锁的宏名称了~印象应该是这个~
pthread_spin_init(&lock, 0);
应该就相当于pthread_spinlock_t CSingleton::lock = SPIN_LOCK_UNLOCKED;
整体的代码还是回看之前那贴~我又改了下~
【 在 Wizmann 的大作中提到: 】
: 好厉害!
: 不过弱弱问下spinlock在第一次使用的时候貌似没有初始化吧。。。
:
: ...................
【 在 tonyjansan 的大作中提到: 】
: 应该吧那个注释开启~因为我不很确定那个未加锁的宏名称了~印象应该是这个~
: pthread_spin_init(&lock, 0);
: 应该就相当于pthread_spinlock_t CSingleton::lock = SPIN_LOCK_UNLOCKED;
: ...................
貌似不行的。。。应该不能用<linux/spinlock.h>吧。。。。
我写了一个。。。貌似运行是没问题的。。。
#ifndef SINGLETON
#define SINGLETON
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <pthread.h>
using namespace std;
#define print(x) cout<<x<<endl
#define input(x) cin>>x
class CSpinlock
{
public:
CSpinlock()
{
pthread_spin_init(&this->_lock, 0);
}
inline void lock()
{
pthread_spin_lock(&this->_lock);
}
inline void unlock()
{
pthread_spin_unlock(&this->_lock);
}
private:
CSpinlock(const CSpinlock&){}
pthread_spinlock_t _lock;
};
class CSingleton
{
public:
static CSingleton* getInstance()
{
if(!uniqueInstance)
{
spinlock.lock();
if(!uniqueInstance)
{
uniqueInstance = new CSingleton();
}
spinlock.unlock();
}
return uniqueInstance;
}
void fill(int x){ val += x; }
int getVal(){ return val; }
private:
int val;
CSingleton()
{
val = 0;
}
CSingleton(const CSingleton&){}
static CSingleton * uniqueInstance;
static CSpinlock spinlock;
};
CSingleton * CSingleton::uniqueInstance = NULL;
CSpinlock CSingleton::spinlock;
#endif
在用MinGW吗?
如果都走pthread标准的话还真不知道pthread_spinlock_init中是怎么实现的~你可以跟进去看下~估计应该也是宏函数~直接定义成static然后在ifdef内用宏函数初始化pthread_spinlock_t变量也是可以的~
【 在 Wizmann 的大作中提到: 】
:
: 貌似不行的。。。应该不能用<linux/spinlock.h>吧。。。。
: 我写了一个。。。貌似运行是没问题的。。。
: ...................
【 在 tonyjansan 的大作中提到: 】
: 在用MinGW吗?
: 如果都走pthread标准的话还真不知道pthread_spinlock_init中是怎么实现的~你可以跟进去看下~估计应该也是宏函数~直接定义成static然后在ifdef内用宏函数初始化pthread_spinlock_t变量也是可以的~
:
我用Linux,是G++。。。
貌似是直接=1来着。。。不过这么写好丑。。。还不如我再加个类呢。。。。。
怎么用宏函数,求解求解?~