趋势科技面试题目
2013-01-16 11:18
134 查看
12年6月份的时候参加了南京趋势科技的面试。
先更新一道题目,
1 用模板写一个线程安全的单例模式。
这个题目我写的不好,今天在陈硕大婶的git上找到了一个例子,贴上来。
// excerpts from http://code.google.com/p/muduo/
//
// Use of this source code is governed by a BSD-style license
// that can be found in the License file.
//
// Author: Shuo Chen (giantchen at gmail dot com)
#ifndef MUDUO_BASE_SINGLETON_H
#define MUDUO_BASE_SINGLETON_H
#include <boost/noncopyable.hpp>
#include <pthread.h>
#include <stdlib.h> // atexit
namespace muduo
{
template<typename T>
class Singleton : boost::noncopyable
{
public:
static T& instance()
{
pthread_once(&ponce_, &Singleton::init);
return *value_;
}
private:
Singleton();
~Singleton();
static void init()
{
value_ = new T();
::atexit(destroy);
}
static void destroy()
{
delete value_;
}
private:
static pthread_once_t ponce_;
static T* value_;
};
template<typename T>
pthread_once_t Singleton<T>::ponce_ = PTHREAD_ONCE_INIT;
template<typename T>
T* Singleton<T>::value_ = NULL;
}
#endif
先更新一道题目,
1 用模板写一个线程安全的单例模式。
这个题目我写的不好,今天在陈硕大婶的git上找到了一个例子,贴上来。
// excerpts from http://code.google.com/p/muduo/
//
// Use of this source code is governed by a BSD-style license
// that can be found in the License file.
//
// Author: Shuo Chen (giantchen at gmail dot com)
#ifndef MUDUO_BASE_SINGLETON_H
#define MUDUO_BASE_SINGLETON_H
#include <boost/noncopyable.hpp>
#include <pthread.h>
#include <stdlib.h> // atexit
namespace muduo
{
template<typename T>
class Singleton : boost::noncopyable
{
public:
static T& instance()
{
pthread_once(&ponce_, &Singleton::init);
return *value_;
}
private:
Singleton();
~Singleton();
static void init()
{
value_ = new T();
::atexit(destroy);
}
static void destroy()
{
delete value_;
}
private:
static pthread_once_t ponce_;
static T* value_;
};
template<typename T>
pthread_once_t Singleton<T>::ponce_ = PTHREAD_ONCE_INIT;
template<typename T>
T* Singleton<T>::value_ = NULL;
}
#endif
相关文章推荐
- 2016年趋势科技夏令营面试题目
- 面试题目
- 最近做的一些比较基础的笔试面试题目
- iOS(Objective-C语法)面试、笔试题目
- 数据仓库与BI面试常见题目
- 常见的abap面试题目,请大家对照学习
- c/c++学习_面试题目
- linux面试题目—2
- // 程序员面试宝典 面试题目2 217 将字符串转化为整型 不能使用atoi函数。接口仿照atoi ,
- 20个最佳的Java集合框架面试题目
- 面试题目——《CC150》排序与查找
- php面试需要掌握的基础题目锦集
- 各大计算机公司 笔试及面试 题目 - 腾讯 (HTTP 协议)
- 面试题目
- 面试题目
- 面试题目整理 线程安全的单例模式
- android面试题目大全<第三部分>,java高级综合部分
- 面试之C++题目汇总7
- 2011面试题目之猴子偷桃
- 面试题目20130722 瞬联软件