source: libcf/trunk/test/log.c

Last change on this file was 157, checked in by cheese, 10 years ago

#1 fix memory leakage

File size: 2.2 KB
RevLine 
[35]1/**
2 * @file log.c
3 * @author myusgun <myusgun@gmail.com>
4 */
5#include "cf_log.h"
[148]6#include "cf_mutex.h"
[105]7#include "cf_thread.h"
8#include "cf_debug.h"
9
[35]10#include <stdio.h>
11
[151]12#define COUNT 5
[105]13
[151]14cf_ctx globalLog;
15cf_ctx globalMutex;
[105]16
[151]17int worker (void * arg)
[105]18{
19 static int cnt = 0;
20 int i = 0;
21 int th = 0;
22
[125]23 if (CF_Mutex_Lock (globalMutex) < 0) { // for critical section
[105]24 // error
25 }
26 th = cnt++;
[125]27 if (CF_Mutex_Unlock (globalMutex) < 0) { // for critical section
[105]28 // error
29 }
30
31 CF_DEBUG_PRINT (stderr, "created %dth thread\n", th);
32
[121]33 for (i = 0 ; i < 10000 ; i++)
[105]34 {
[151]35 CF_Log_Write (globalLog, "LOG_MT", "[%d] multi-threadedlogging test %d\n", th, i);
[105]36 }
37
38 CF_DEBUG_PRINT (stderr, "end %dth thread\n", th);
39
40 return 0;
41}
42
[35]43int main (void)
44{
[151]45 int i, j;
46 cf_ctx tid[COUNT];
[35]47
[105]48 /* initialize */
[157]49 /* single thread {{{ */
[151]50 if (CF_Log_Create (&globalLog, "log.txt", CF_LOG_NO_BUFFER) < 0)
51 CF_DEBUG_PRINT (stderr, "failed to open log\n");
[35]52
[151]53 for (j = 0 ; j < 10000 ; j++)
[35]54 {
[151]55 int result = CF_Log_Write (globalLog, "LOG_ID_TEST", "turn %d\n", j);
56 if (result < 0)
57 CF_DEBUG_PRINT (stderr, "failed to write log %d\n", result);
[35]58 }
59
[157]60 CF_Log_Destroy (globalLog);
61 /* }}} single thread */
62
[105]63 /* mt {{{ */
[151]64 if (CF_Log_Create (&globalLog, "log_mt.txt", CF_LOG_NO_BUFFER) < 0)
[105]65 CF_DEBUG_PRINT (stderr, "create log ctx error\n");
66
[151]67 if (CF_Mutex_Create (&globalMutex) < 0) {
[105]68 // error
69 }
70
[151]71 if (CF_Log_SetMultiThread (globalLog) < 0)
72 CF_DEBUG_PRINT (stderr, "set multi-threading mode error\n");
73
74 for (i = 0 ; i < COUNT ; i++)
[105]75 {
[151]76 if (CF_Thread_Create (&tid[i], worker, &i) < 0)
[105]77 {
78 CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", i);
79 return -2;
80 }
81 }
82
[151]83 for (i = 0 ; i < COUNT ; i++)
[105]84 {
[122]85 if (CF_Thread_Start (tid[i]) < 0)
86 {
87 CF_DEBUG_PRINT (stderr, "failed to start %dth thread\n", i);
88 return -3;
89 }
90 }
91
[151]92 for (i = 0 ; i < COUNT ; i++)
[122]93 {
94 if (CF_Thread_Join (tid[i]) < 0)
[105]95 CF_DEBUG_PRINT (stderr, "failed to join %dth thread\n", i);
[151]96 if (CF_Thread_Destroy (tid[i]) < 0)
[105]97 CF_DEBUG_PRINT (stderr, "failed to release %dth thread\n", i);
98 }
99
[151]100 if (CF_Log_UnsetMultiThread (globalLog) < 0)
101 CF_DEBUG_PRINT (stderr, "set multi-threading mode error\n");
102
103 if (CF_Mutex_Destory (globalMutex) < 0) {
[105]104 // error
105 }
106 /* }}} mt */
107
108 /* finalize */
[151]109 CF_Log_Destroy (globalLog);
[35]110
111 return 0;
112}
Note: See TracBrowser for help on using the repository browser.