进阶之路

首页 新随笔 管理

1. 单例模式 (多线程下)

a. 饿汉式

 1 template<class T>
 2     class Singleton {
 3         public:
 4             static T* get_instance() {
 5                 if( _instance == NULL ) {
 6                     pthread_mutex_lock(&_mutex);
 7                     if( _instance == NULL ) {
 8                         _instance = new T;
 9                     }
10                     pthread_mutex_unlock(&_mutex);
11                 }
12                 return _instance;
13             }
14 
15         protected:
16             Singleton(){}
17             virtual ~Singleton() {
18                 if(_instance) {
19                     T* t = _instance;
20                     _instance = NULL;
21                     delete t;
22                 }
23             }
24 
25         private:
26             static T* _instance;
27             static pthread_mutex_t  _mutex;
28     };
29 
30 template<class T> T* Singleton<T>::_instance = NULL;
31 template<class T> pthread_mutex_t Singleton<T>::_mutex = PTHREAD_MUTEX_INITIALIZER;
View Code

b. 懒汉式

class Singleton {
    public:
        static const Singleton* get_instance() {
            return _instance;
        }
        void release() {
            if(_instance) {
                delete _instance;
                _instance = NULL;   
            }
        }
    private:
        Singleton(){}
        ~Singleton(){}

        static const Singleton* _instance;        
};
const Singleton* Singleton::_instance = new Singleton();

 

 

 

1. 字符串分割

vector<string> split(const string& str, string cutStr) {
	vector<string> res;
	size_t start = 0;
	size_t stop;
	while ((stop = str.find(cutStr, start)) != string::npos) {
		if (stop > start) {
			res.push_back(str.substr(start, stop-start));	
		}else {
			res.push_back("");
		}
		start = stop + cutStr.length();
	}
	if(start < str.length()) {
		res.push_back(str.substr(start));
	}else {
		res.push_back("");
	}
	return res;
}

 

一. 进程

C编译器(gcc)——>连接编辑器— (设置起始地址:启动例程)—>可执行程序文件—(启动例程)—>内核(命令行参数、环境变量值)

            ——>main()—(大多数)—>exit()——>_exit()【or _Exit()】

 1. exit

#include <stdlib.h>          
void exit(int status);
void _Exit(int status);

#include <unistd.h>
void _exit(int status);

 

 2. atexit  :  登记终止处理程序

#include <stdlib.h>
int atexit(void (*func)(void));
//return: success, 0; error, other.

 

3. 存储器分配

#include <stdlib.h>
void *malloc(size_t size);
void *calloc(size_t nobj, size_t size);
void *realloc(void *ptr, size_t newsize);

//return: success, 非空;error, NULL.

void free(void *ptr);

 

 4. 环境变量

 

#include <stdlib.h>
char *getenv(const char *name);

//return: 与 name 关联的指针,未找到则 NULL.

int putenv(char *str);
int setenv(const char *name, const char *value, int rewrite);
int unsetenv(const char *name);

//return: success, 0; error, 非0. 

 

5. setjmp & longjmp

#include <setjmp.h>
int setjmp(jmp_buf env);

//return: 直接返回,0; 从 longjmp 调用返回则 非0.

void longjmp(jmp_buf env, int val);

 

6. getrlimit & setrlimit

#include <sys/resource.h>
int getrlimit(int resource, struct rlimit *rlptr);
int setrlimit(int resource, const struct rlimit *rlptr);

//return: success, 0; error, 非0.

 

 

 

二.线程

#include <pthread.h>

int pthread_equal (pthread_t tid1, pthread_t tid2); //return: S, 0; E, 非0.

pthread_t pthread_self (void); // return ID.

int pthread_create (pthread_t *restrict tidp, const pthread_attr_t *restrict attr, 
                         void *(*start_rtn)(void), void *restrict arg);
//return: S, 0; E, 错误编号.

void pthread_exit(void *rval_ptr);

int pthread_join(pthread_t thread, void **rval_ptr); //return: S, 0; E, 错误编号.

int pthread_cancel(pthread_t tid);  //return: S, 0; E, 错误编号.

void pthread_cleanup_push(void (*rtn)(void *), void *arg);
void pthread_cleanup_pop(int execute);

int pthread_detach (pthread_t tid);  //return: S, 0; E, 错误编号.

int pthread_mutex_init (pthread_mutex_t *restrict mutex, 
                                  const pthread_mutexattr_t *restrict attr);
int pthread_mutex_destroy (pthread_mutex_t *mutex);
//return: S, 0; E, 错误编号.

int pthread_mutex_lock (pthread_mutex_t *mutex);
int pthread_mutex_trylock (pthread_mutex_t *mutex);
int pthread_mutex_unlock (pthread_mutex_t *mutex);
//return: S, 0; E, 错误编号.

int pthread_rwlock_init (pthread_rwlock_t *restrict rwlock, 
                                  const pthread_rwlockattr_t *restrict attr);
int pthread_rwlock_destroy (pthread_rwlock_t *mutex);
//return: S, 0; E, 错误编号.

int pthread_rwlock_rdlock (pthread_rwlock_t *rwlock);
int pthread_rwlock_wrlock (pthread_rwlock_t *rwlock);
int pthread_rwlock_unlock (pthread_rwlock_t *rwlock);
//return: S, 0; E, 错误编号.

int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
//return: S, 0; E, 错误编号.

int pthread_cond_init (pthread_cond_t *restrict cond, 
                                  const pthread_condattr_t *restrict attr);
int pthread_cond_destroy (pthread_cond_t *cond);
//return: S, 0; E, 错误编号.

int pthread_cond_wait (pthread_cond_t *restrict cond,
                                pthread_mutex_t *restrict mutex);
int pthread_cond_timedwait (pthread_cond_t *restrict cond,
                                pthread_mutex_t *restrict mutex,
                                const struct timespec *restrict timeout);
//return: S, 0; E, 错误编号.

int pthread_cond_signal (pthread_cond_t *cond);
int pthread_cond_broadcast (pthread_cond_t *cond);
//return: S, 0; E, 错误编号.

 

posted on 2016-04-21 14:25  进阶之路  阅读(165)  评论(0编辑  收藏  举报