-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_threads.cpp
More file actions
57 lines (45 loc) · 1.24 KB
/
test_threads.cpp
File metadata and controls
57 lines (45 loc) · 1.24 KB
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
#include "logger.h"
#include <cstring>
#include <pthread.h>
#include <iostream>
#include <chrono>
#include <thread>
//
// Multi threaded logging.
//
int log_handle;
#define NUM_THREADS 4
void *log_msg(void* ptr) {
char* msg = (char*) ptr;
for(auto i = 0; i < 5; i++) {
log_msg(log_handle, (uint8_t*) msg, strlen(msg));
std::this_thread::sleep_for(std::chrono::seconds(rand() % 3));
}
pthread_exit(0);
}
int main() {
log_handle = log_init("test2.bin");
char* msgs[NUM_THREADS];
for(auto i = 0;i < NUM_THREADS; i++) {
msgs[i] = (char*) malloc(256);
if(i % 2) {
strcpy(msgs[i], "Hello World");
}
else {
strcpy(msgs[i], "Bye World");
}
}
std::cout << "Creating " << NUM_THREADS << " threads." << std::endl;
pthread_t thread_ids[NUM_THREADS];
for(auto i = 0; i < NUM_THREADS; i++) {
if(pthread_create(&thread_ids[i], NULL, log_msg, msgs[i])) {
std::cout << "Creating thread failed" << std::endl;
return -1;
}
}
std::cout << "Waiting for all threads to finish" << std::endl;
for(auto i = 0; i < NUM_THREADS; i++) {
pthread_join(thread_ids[i], NULL);
}
return 0;
}