Skip to content

Commit 79c14b1

Browse files
committed
add parallel threads test
1 parent 1b1a26e commit 79c14b1

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

test/testSimpleLog.cxx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@
1515

1616
#include <Common/SimpleLog.h>
1717
#include <unistd.h>
18+
#include <thread>
19+
#include <vector>
20+
21+
void logWorker(SimpleLog& log, int threadId, int count) {
22+
for (int i = 0; i < count; ++i) {
23+
log.info("Thread %d - test message %d", threadId, i);
24+
}
25+
}
1826

1927
int main()
2028
{
@@ -25,5 +33,18 @@ int main()
2533
theLog.info("test message %d", i);
2634
}
2735
// sleep(10);
36+
37+
// test parallel threads
38+
theLog.setLogFile("/tmp/testthread.log", 1000, 10, 0);
39+
const int numThreads = 10;
40+
const int messagesPerThread = 10;
41+
std::vector<std::thread> threads;
42+
for (int t = 0; t < numThreads; ++t) {
43+
threads.emplace_back(logWorker, std::ref(theLog), t, messagesPerThread);
44+
}
45+
for (auto& th : threads) {
46+
th.join();
47+
}
48+
2849
return 0;
2950
}

0 commit comments

Comments
 (0)