C++ 学习日记

工作暂无。趁这个时间好好再扎实下C++基础。也把问题记录下。以备后用

linux 时间处理。打印当前时间

time_t now;
struct tm* now_time;
time(&now);
now_time=localtime(&now);
printf("Local   time   is   %s\n",asctime(now_time));
printf("现在是%d年%d月%d日%d时%d分%d秒\n ",now_time->tm_year+1900,now_time->tm_mon+1,now_time->tm_mday,
now_time->tm_hour,now_time->tm_min,now_time->tm_sec);

=======================================const=========

const用法小结:
const最常用的就是定义常量,除此之外,它还可以修饰函数的参数、返回值和函数的定义体。
1. const修饰函数的参数
如果参数作输出用,不论它是什么数据类型,也不论它采用“指针传递”还是“引用传递”,都不能加const 修饰,否则该参数将失去输出功能。
const 只能修饰输入参数:
如果输入参数采用“指针传递”,那么加const 修饰可以防止意外地改动该指针,起到保护作用。
将“const &”修饰输入参数的用法总结如下:
(1)对于非内部数据类型的输入参数,应该将“值传递”的方式改为“const 引用传递”,目的是提高效率。例如将void Func(A a) 改为void Func(const A &a)。
(2)对于内部数据类型的输入参数,不要将“值传递”的方式改为“const 引用传递”。否则既达不到提高效率的目的,又降低了函数的可理解性。例如void Func(int x) 不应该改为void Func(const int &x)。
2. const 修饰函数的返回值
如果给以“指针传递”方式的函数返回值加const 修饰,那么函数返回值(即指针)的内容不能被修改,该返回值只能被赋给加const 修饰的同类型指针。例如函数
const char * GetString(void);
如下语句将出现编译错误:
char *str = GetString();
正确的用法是
const char *str = GetString();

如果返回值不是内部数据类型,将函数A GetA(void) 改写为const A & GetA(void)的确能提高效率。但此时千万千万要小心,一定要搞清楚函数究竟是想返回一个对象的“拷贝”还是仅返回“别名”就可以了,否则程序会出错。
函数返回值采用“引用传递”的场合并不多,这种方式一般只出现在类的赋值函数中,目的是为了实现链式表达。
例如:

class A
{
A & operate = (const A &other); // 赋值函数
};
A a, b, c; // a, b, c 为A 的对象
a = b = c; // 正常的链式赋值
(a = b) = c; // 不正常的链式赋值,但合法
如果将赋值函数的返回值加const 修饰,那么该返回值的内容不允许被改动。上例中,语句 a = b = c 仍然正确,但是语句 (a = b) = c 则是非法的。
3. const修饰成员函数
关于Const函数的几点规则:
a. const对象只能访问const成员函数,而非const对象可以访问任意的成员函数,包括const成员函数.
b. const对象的成员是不可修改的,然而const对象通过指针维护的对象却是可以修改的.
c. const成员函数不可以修改对象的数据,不管对象是否具有const性质.它在编译时,以是否修改成员数据为依据,进行检查.
e. 然而加上mutable修饰符的数据成员,对于任何情况下通过任何手段都可修改,自然此时的const成员函数是可以修改它的

小结:

1)const成员函数可以访问非const对象的非const数据成员、const数据成员,也可以访问const对象内的所有数据成员;

2)非const成员函数可以访问非const对象的非const数据成员、const数据成员,但不可以访问const对象的任意数据成员;

3)作为一种良好的编程风格,在声明一个成员函数时,若该成员函数并不对数据成员进行修改操作,应心可能将该成员函数声明为const 成员函数。


===============================

#ifndef _OPMSG_H_
#define _OPMSG_H_
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <queue>
#include <vector>
#include <iostream>
using namespace std;
#define THREADNUM 15
class OPMSG
{
public :

~OPMSG();
static OPMSG* Instance();
bool setfilename(char*);
static void* work(void*);

void catchmsg(void *);
void throwmsg();
void over();

private:
OPMSG();
static OPMSG* m_instance;
queue<char*> m_buf;
FILE *fp;
pthread_t pid[THREADNUM];
pthread_mutex_t m_mutex;
char msgbuf[100];
char pid_str[30];

};

#endif

===

#include "opmsg.h"
OPMSG* OPMSG::m_instance=NULL;
OPMSG* OPMSG::Instance()
{
if(m_instance== NULL)
    {
        m_instance=new OPMSG();
    }
return m_instance;
}


OPMSG::OPMSG()
{
cout<<"gouzao"<<endl;
fp=NULL;
//m_mutex=PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_init(&m_mutex,NULL);
}
OPMSG::~OPMSG()
{
if(fp!=NULL)fclose(fp);
pthread_mutex_destroy(&m_mutex);
delete m_instance;
m_instance=NULL;
}
bool OPMSG::setfilename(char* filename)
{
fp=fopen(filename,"wb+");
if(fp==NULL)
{printf("file build error\n");return false;}
return true;
}
void *OPMSG:: work(void* arg)
{
OPMSG* mop=(OPMSG*)arg;
time_t now;
struct tm* now_time;
//while(1)
//{
time(&now);
now_time=localtime(&now);
//char msgbuf[100];
//char pid_str[30];
memset(mop->msgbuf,'\0',sizeof(mop->msgbuf));
memset(mop->pid_str,'\0',sizeof(mop->pid_str));
pthread_mutex_lock(&(mop->m_mutex));
sprintf(mop->pid_str,"0x%X\n",pthread_self());
sprintf(mop->msgbuf,"%d.%d.%d %d:%d:%d the pid id is",now_time->tm_year+1900,now_time->tm_mon+1,now_time->tm_mday,now_time->tm_hour,now_time->tm_min,now_time->tm_sec);
strcat(mop->msgbuf,mop->pid_str);
pthread_mutex_unlock(&(mop->m_mutex));
pthread_mutex_lock(&(mop->m_mutex));
fwrite(mop->msgbuf,sizeof(mop->msgbuf),1,mop->fp);
mop->m_buf.push(mop->msgbuf);
pthread_mutex_unlock(&(mop->m_mutex));

// }
}
void OPMSG::catchmsg(void* argc)
{
for(int i=0;i<THREADNUM;i++)
{
pthread_create(&pid[i],NULL,work,argc);
}
}
void OPMSG::throwmsg()
{

char *it=m_buf.front();
while(!m_buf.empty())
{
it=m_buf.front();
cout<<it<<endl;
m_buf.pop();
}

}
void OPMSG::over()
{
for(int i=0;i<THREADNUM;i++)
{pthread_join(pid[i],NULL);}
}
int main()
{

OPMSG* mymsg=OPMSG::Instance();
mymsg->setfilename("msg.log");
mymsg->catchmsg(mymsg);
mymsg->over();

mymsg->throwmsg();

return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值