-
Notifications
You must be signed in to change notification settings - Fork 1
/
test-mt.cc
104 lines (86 loc) · 1.65 KB
/
test-mt.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
test-mt: 多线程写日志测试
*/
#include <iostream>
#include <string>
#include <thread>
#include "logitf_si.h"
#include "tinylog.h"
using namespace tl;
const TCHAR *datfile = _T("test");
static long getsize(const TCHAR *filename)
{
struct stat st;
if (stat(filename, &st) < 0)
return -1;
return (long)(st.st_size);
}
void threadfunc1()
{
int i = 0;
for (; i < 5000; ++i)
{
LOGINFO(_T("t1--%04d"), i);
}
}
void threadfunc2()
{
int i = 0;
for (; i < 5000; ++i)
{
LOGINFO(_T("t2--%04d"), i);
// specify an 'i' value to test LOGDATDUMP:
if (i == 1234)
{
std::cout << std::endl << _T("BEGIN!!!") << std::endl;
long size = getsize(datfile);
if (size == -1)
{
std::cout << _T("size = -1") << std::endl;
continue;
}
FILE *file = fopen(datfile, "r");
unsigned char * buff = new unsigned char[size + 1];
fread(buff, 1, size, file);
buff[size] = 0x00;
fclose(file);
LOGDATDUMPMT(_T("file1234"), buff, size);
delete[] buff;
buff = nullptr;
std::cout << _T("DUMP DONE!") << std::endl << std::endl;
}
}
}
void threadfunc3()
{
int i = 0;
for (; i < 5000; ++i)
{
LOGINFO(_T("t3--%04d"), i);
}
}
void threadfunc4()
{
int i = 0;
for (; i < 5000; ++i)
{
LOGINFO(_T("t4--%04d"), i);
}
}
int _tmain(void)
{
std::cout << _T("main() start...") << std::endl;
g_log.set_max_size(1024);
g_log.start(_T("testmt"), _T(".log"), _T("./logs"));
std::thread th1(threadfunc1);
std::thread th2(threadfunc2);
std::thread th3(threadfunc3);
std::thread th4(threadfunc4);
th1.join();
th2.join();
th3.join();
th4.join();
g_log.finish();
std::cout << _T("main() exit.") << std::endl;
return 0;
}