Changeset 105 in libcf for trunk/test/log.c
- Timestamp:
- 05/29/13 14:10:04 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/log.c
r103 r105 4 4 */ 5 5 #include "cf_log.h" 6 #include "cf_thread.h" 7 #include "cf_debug.h" 8 6 9 #include <stdio.h> 10 11 CF_Mutex globalMutex; 12 13 #define LOG_MT 1 14 15 int test_log_mt (void * arg) 16 { 17 static int cnt = 0; 18 int i = 0; 19 int th = 0; 20 21 if (CF_Mutex_Lock (&globalMutex) < 0) { // for critical section 22 // error 23 } 24 th = cnt++; 25 if (CF_Mutex_Unlock (&globalMutex) < 0) { // for critical section 26 // error 27 } 28 29 CF_DEBUG_PRINT (stderr, "created %dth thread\n", th); 30 31 for (i = 0 ; i < 100000 ; i++) 32 { 33 CF_Log_Write (LOG_MT, "LOG_MT", "[%d] multi-threadedlogging test %d\n", th, i); 34 } 35 36 CF_DEBUG_PRINT (stderr, "end %dth thread\n", th); 37 38 return 0; 39 } 7 40 8 41 int main (void) 9 42 { 10 43 int i, j; 11 char logname[16] = {0x00,}; 44 char idname[16] = {0x00,}; 45 CF_Thread tid[10]; 12 46 13 if (CF_Log_Initialize (10, CF_LOG_NO_LEVEL) < 0) 47 /* initialize */ 48 if (CF_Log_Initialize (10, 2) < 0) 14 49 { 15 50 fprintf (stderr, "failed to init. log\n"); … … 19 54 for (i = 0 ; i < 10 ; i++) 20 55 { 21 sprintf ( logname, "logid%d.txt", i);22 if (CF_Log_Open (i, logname, CF_LOG_NO_BUFFER) < 0)56 sprintf (idname, "logid%d.txt", i); 57 if (CF_Log_Open (i, idname, CF_LOG_NO_BUFFER) < 0) 23 58 fprintf (stderr, "failed to open log\n"); 24 59 } … … 37 72 } 38 73 74 /* mt {{{ */ 75 if (CF_Log_Open (LOG_MT, "log_mt.txt", CF_LOG_DEFAULT_BUFFER) < 0) 76 CF_DEBUG_PRINT (stderr, "create log ctx error\n"); 77 78 if (CF_Log_SetMT (LOG_MT, CF_TRUE) < 0) 79 CF_DEBUG_PRINT (stderr, "set multi-threading mode error\n"); 80 81 if (CF_Mutex_Create (&globalMutex) < 0) { 82 // error 83 } 84 85 for (i = 0 ; i < 10 ; i++) 86 { 87 if (CF_Thread_Create (&tid[i], test_log_mt, &i) < 0) 88 { 89 CF_DEBUG_PRINT (stderr, "failed to create %dth thread\n", i); 90 return -2; 91 } 92 } 93 94 for (i = 0 ; i < 10 ; i++) 95 { 96 if (CF_Thread_Join (&tid[i]) < 0) 97 CF_DEBUG_PRINT (stderr, "failed to join %dth thread\n", i); 98 if (CF_Thread_Release (&tid[i]) < 0) 99 CF_DEBUG_PRINT (stderr, "failed to release %dth thread\n", i); 100 } 101 102 if (CF_Mutex_Destory (&globalMutex) < 0) { 103 // error 104 } 105 /* }}} mt */ 106 107 /* finalize */ 39 108 CF_Log_Finalize (); 40 109
Note:
See TracChangeset
for help on using the changeset viewer.