- 论坛徽章:
- 0
|
本帖最后由 korpus 于 2014-02-13 16:23 编辑
我把代码整理出来了一份可以独立编译
比较奇怪的是如果注释掉函数 trans_SingleThread LOG_DEBUG("Thread Will Quit [%ld]", pthread_self());,释放回正常,如果没注释释放就不正常了- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <errno.h>
- #include <stdarg.h>
- #include <sys/timeb.h>
- #include <sys/stat.h>
- #include <pthread.h>
- typedef enum
- {
- LOG_LEVEL_ERROR, // only print ERROR log
- LOG_LEVEL_WARN, // print ERROR and WARN log
- LOG_LEVEL_INFO, // print ERROR, WARN and INFO log
- LOG_LEVEL_DEBUG, // print ERROR, WARN, INFO AND DEBUG log
- LOG_LEVEL_ALL // print all log, and print full debug log and other informations
- }LOG_LEVEL;
- static char g_szLogHead[200];
- static long g_iSetLogLevel = 3;
- static long g_lSetLogSize = 0;
- static int g_iLogLevel;
- static char g_szLogFile[256]="./test.log";
- pthread_mutex_t g_WriteLog = PTHREAD_MUTEX_INITIALIZER;
- static char g_szLogPath[128]=".";
- static char g_szLogName[128]="test.log";
- #define LOG_ERROR Log_SysInfo(LOG_LEVEL_ERROR,__FILE__,__LINE__),Log_Msg
- #define LOG_WARN Log_SysInfo(LOG_LEVEL_WARN,__FILE__,__LINE__),Log_Msg
- #define LOG_INFO Log_SysInfo(LOG_LEVEL_INFO,__FILE__,__LINE__),Log_Msg
- #define LOG_DEBUG Log_SysInfo(LOG_LEVEL_DEBUG,__FILE__,__LINE__),Log_Msg
- #define RT_SUCCESS 0
- #define RT_FAILURE -1
- void Log_Check();
- long timel_GetCurrentTime()
- {
- char szTime[7];
- long lCurTime;
- struct tm *ptime;
- time_t times;
- time(×);
- ptime = localtime(×);
- memset(szTime, 0, sizeof(szTime));
- sprintf(szTime, "%02d%02d%02d", ptime->tm_hour, ptime->tm_min, ptime->tm_sec);
- lCurTime = atol(szTime);
- return lCurTime;
- }
- long timel_GetCurrentDate()
- {
- char szDate[9];
- long lDate;
- struct tm *ptm;
- time_t times;
- time(×);
- ptm = localtime(×);
- memset(szDate, 0, sizeof(szDate));
- sprintf(szDate, "%d%02d%02d", ptm->tm_year+1900, ptm->tm_mon + 1, ptm->tm_mday);
- lDate = atol(szDate);
- return lDate;
- }
- void timev_GetLogTime(char *out_pLogTime)
- {
- struct timeb ptm;
- struct tm *ptmb;
- ftime(&ptm);
- ptmb = localtime(&ptm.time);
- sprintf(out_pLogTime, "%02d-%02d %02d:%02d:%02d.%03d", ptmb->tm_mon+1, ptmb->tm_mday,
- ptmb->tm_hour, ptmb->tm_min, ptmb->tm_sec, ptm.millitm);
- return ;
- }
- void Log_SysInfo(const int iLogLevel, const char *pszFileName, const int iNum)
- {
- int i = 0;
- char szLogTime[30];
- pthread_mutex_lock(&g_WriteLog);
- memset(szLogTime, 0, sizeof(szLogTime));
- memset(g_szLogHead, 0, sizeof(g_szLogHead));
- g_iLogLevel = iLogLevel;
- if(g_iSetLogLevel < g_iLogLevel)
- {
- return;
- }
- Log_Check();
- timev_GetLogTime(szLogTime);
- snprintf(g_szLogHead, sizeof(g_szLogHead), "%s %s L=%d T=%ld E=%d ", szLogTime, pszFileName,
- iNum, pthread_self(), errno);
- switch(iLogLevel)
- {
- case LOG_LEVEL_ERROR:
- sprintf(g_szLogHead + strlen(g_szLogHead), "%s", "[ERROR] ");
- break;
- case LOG_LEVEL_WARN:
- sprintf(g_szLogHead + strlen(g_szLogHead), "%s", "[WARN] ");
- break;
- case LOG_LEVEL_DEBUG:
- sprintf(g_szLogHead + strlen(g_szLogHead), "%s", "[DEBUG] ");
- break;
- case LOG_LEVEL_INFO:
- sprintf(g_szLogHead + strlen(g_szLogHead), "%s", "[INFO] ");
- break;
- case LOG_LEVEL_ALL:
- sprintf(g_szLogHead + strlen(g_szLogHead), "%s", "[ALL] ");
- break;
- default:
- break;
- }
- }
- void Log_Check()
- {
- struct stat st;
- int iRet;
- char szLogDate[9];
- memset(szLogDate, 0, sizeof(szLogDate));
- memcpy(szLogDate, g_szLogFile + strlen(g_szLogPath) + 1, 8);
- if(atol(szLogDate) == timel_GetCurrentDate())
- {
- if(g_lSetLogSize > 0)
- {
- stat(g_szLogFile, &st);
- if(st.st_size / 1024 / 1024 >= g_lSetLogSize)
- {
- snprintf(g_szLogFile, sizeof(g_szLogFile), "%s/%ld/%s.%ld%s%06d", g_szLogPath,
- timel_GetCurrentDate(), g_szLogName,
- timel_GetCurrentDate(), "_", timel_GetCurrentTime());
- }
-
- }
- }
- else
- {
- if(g_lSetLogSize > 0)
- {
- snprintf(g_szLogFile, sizeof(g_szLogFile), "%s/%ld/%s.%ld%s%06d", g_szLogPath,
- timel_GetCurrentDate(), g_szLogName,
- timel_GetCurrentDate(), "_", timel_GetCurrentTime());
- }
- else
- {
- snprintf(g_szLogFile, sizeof(g_szLogFile), "%s/%ld/%s.%ld", g_szLogPath,
- timel_GetCurrentDate(), g_szLogName,
- timel_GetCurrentDate());
- }
- }
- }
- void Log_Msg(const char *format, ...)
- {
- FILE *fp;
- va_list args;
- int olderrno;
- int i;
- if(g_iSetLogLevel < g_iLogLevel)
- {
- pthread_mutex_unlock(&g_WriteLog);
- return;
- }
- olderrno = errno;
- if(NULL != (fp = fopen(g_szLogFile, "a+")))
- {
- fprintf(fp, "%s", g_szLogHead);
- va_start(args, format);
- vfprintf(fp, format, args);
- va_end(args);
- fprintf(fp, "%s", "\n");
- fclose(fp);
- }
- else
- {
- printf("fopen error, errno=%d, file=%s\n", errno, g_szLogFile);
- perror("fopen");
- }
- errno = olderrno;
- pthread_mutex_unlock(&g_WriteLog);
- }
- void *ufs_MallocMemory(const long lSize)
- {
- void *pAddr = NULL;
- if(lSize <= 0)
- {
- LOG_ERROR("malloc memory size less than zero");
- return NULL;
- }
- pAddr = malloc(lSize);
- if(NULL == pAddr)
- {
- LOG_ERROR("malloc memory size error!, malloc size(%d)", lSize);
- return NULL;
- }
- memset(pAddr, 0, lSize);
- return pAddr;
- }
- void ufs_vFreeNormalMemory(void **pAddr)
- {
- if(NULL == *pAddr)
- return;
- else
- free(*pAddr);
- *pAddr = NULL;
- }
- void *trans_SingleThread(void *pData)
- {
- int iRet;
- void *pReadMemory = NULL;
- void *pWriteMemory = NULL;
- pWriteMemory = ufs_MallocMemory(20971520);
- if(NULL == pWriteMemory)
- {
- LOG_ERROR("Malloc Memory Failure!");
- ufs_vFreeNormalMemory(&pReadMemory);
- iRet = RT_FAILURE;
- pthread_exit((void *)&iRet);
- }
- pReadMemory = ufs_MallocMemory(20971520);
- if(NULL == pReadMemory)
- {
- LOG_ERROR("Malloc Memory Failure!");
- iRet = RT_FAILURE;
- pthread_exit((void *)&iRet);
- }
- sleep(5);
- LOG_DEBUG("Thread Will Quit [%ld]", pthread_self());
- ufs_vFreeNormalMemory(&pReadMemory);
- ufs_vFreeNormalMemory(&pWriteMemory);
- pthread_exit((void *)&iRet);
- }
- void *pFunc(void *pData)
- {
- int *iTime = (int *)pData;
- long lThreadID[10];
- int i;
- *iTime = 5;
- while(1)
- {
- for(i = 0; i < *iTime; i ++)
- {
- if(pthread_create(&lThreadID[i], NULL, trans_SingleThread, NULL) < 0)
- {
- printf("create error\n");
- exit(0);
- }
-
- LOG_DEBUG("Success Create Thread [%ld]", lThreadID[i]);
- }
- for(i = 0; i < *iTime; i ++)
- {
- pthread_join(lThreadID[i], NULL);
- LOG_DEBUG("Thread [%ld] Quit Success!", lThreadID[i]);
- }
- sleep(*iTime * 5);
- }
- }
- int main(int argc, char *argv[])
- {
- long lThreadID[10];
- int i;
- int iRet;
- int iThreadNums;
- iThreadNums = atoi(argv[1]);
- if(iThreadNums > 10 || argc != 2)
- {
- printf("less than 10, or input 2 args!\n");
- exit(0);
- }
- sleep(5);
- for(i = 0; i < iThreadNums; i ++)
- {
- iRet = pthread_create(&lThreadID[i], NULL, pFunc, &iThreadNums);
- if(iRet < 0)
- {
- printf("error ,exit!\n");
- exit(0);
- }
- LOG_DEBUG("Create Initinal Thread [%ld]", lThreadID[i]);
- }
- while(1)
- {
- sleep(2);
- }
- }
复制代码 makefile如下- all:TestMemory
- TestMemory:thread.o
- cc -o TestMemory thread.o -lpthread
- rm -f thread.o
- thread.c:thread.o
- cc -c thread.c
复制代码 |
|