1 Star 0 Fork 0

reference/DesignPatternExample

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
文件
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
singleton.c 1.11 KB
Copy Edit Raw Blame History
gigi authored 2016-04-23 23:08 +08:00 . update
#include <sys/time.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "base.h"
#include "singleton.h"
static Singleton* Singleton_construct(void* addr);
static pthread_once_t once_control = PTHREAD_ONCE_INIT;
static Singleton* singleton = NULL;
Singleton* Singleton_construct(void* addr)
{
if (addr == NULL) {
return NULL;
}
struct timeval tv;
int errCode = gettimeofday(&tv, NULL);
if (errCode) {
fprintf(stderr, "gettimeofday error. [err=%s]",
strerror(errCode));
abort();
}
Singleton* singleton = addr;
singleton->currentTime = tv.tv_sec * 1000000 + tv.tv_usec;
return singleton;
}
Singleton* getSingleton()
{
void init() {
singleton = new(Singleton);
if (singleton == NULL) {
fprintf(stderr, "Fail to new singleton.\n");
abort();
}
}
int errCode = pthread_once(&once_control, init);
if (errCode) {
fprintf(stderr, "pthread_once error. [err=%s]",
strerror(errCode));
abort();
}
return singleton;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/reference/DesignPatternExample.git
git@gitee.com:reference/DesignPatternExample.git
reference
DesignPatternExample
DesignPatternExample
master

Search