zhangmeng
2024-01-18 8fc23a3bb9f49e88478a2505fa7dee434ec50c16
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
 
#include "usg_common.h"
 
#define THREADS 4
 
typedef struct shm_packet_t {
    int key;
    size_t size;
    void * buf;
    char uuid[64];
    int action;
 
} shm_packet_t;
 
 
void *_run_(void *arg) {
    std::map<int, shm_packet_t> * amap = (std::map<int, shm_packet_t> *)arg;
    std::map<int, shm_packet_t>::iterator iter;
    iter = amap->find(2);
    while(  iter  != amap->end() ) {
        amap->erase(iter);
        iter = amap->find(2);
        sleep(1);
    }
 
}
 
int main() {
    int i;
    pthread_t tids[THREADS];
    std::map<int, shm_packet_t> * amap = new std::map<int, shm_packet_t>;
 
    for (i = 0; i < THREADS; i++) {
    
    pthread_create(&tids[i], NULL, _run_, (void *)&amap);
  }
 
  shm_packet_t pack;
  pack.key = 2;
    
    while(true) {
        sleep(1);
        amap->insert({2, pack});
    }
 
 
  for (i = 0; i < THREADS; i++) {
    if (pthread_join(tids[i], NULL) != 0) {
      perror(" pthread_join");
    }
  }
 
}