-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_msgq.c
More file actions
98 lines (79 loc) · 2.21 KB
/
Copy pathtest_msgq.c
File metadata and controls
98 lines (79 loc) · 2.21 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
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
#include <stdio.h>
#include "sys/time.h"
#include "unistd.h"
#include "pthread.h"
#include "msgq.h"
///////////////////////////////////////////
#define err_msg(str, args...) fprintf(stderr, "%s[%3d]=> " str, __FUNCTION__, __LINE__, ## args)
static struct timeval g_start;
///////////////////////////////////////////
static void*
_thread_post(void *argv)
{
msgq_handle_t *pHMsgq = (msgq_handle_t*)argv;
struct timeval timeout;
while( (timeout.tv_sec - g_start.tv_sec) < 10 )
{
int *pValue = 0xab;
int ret = 0;
ret = msgq_post_msg(pHMsgq, pValue);
if( ret )
{
err_msg("return err (%d) !\n", ret);
continue;
}
err_msg("cur msg cnt = %d\n", pHMsgq->cur_msg_cnt);
gettimeofday(&timeout, NULL);
usleep(100000);
}
err_msg("leave\n");
pthread_exit(NULL);
return 0;
}
static void*
_thread_fetch(void *argv)
{
msgq_handle_t *pHMsgq = (msgq_handle_t*)argv;
struct timeval timeout;
while( (timeout.tv_sec - g_start.tv_sec) < 10 )
{
int *pValue = 0;
int ret = 0;
ret = msgq_fetch_msg(pHMsgq, (void**)&pValue);
if( !ret )
{
if( pValue != 0xab )
{
err_msg("fetch wrong data !\n");
}
err_msg("cur msg cnt = %d\n", pHMsgq->cur_msg_cnt);
}
gettimeofday(&timeout, NULL);
usleep(100000);
}
err_msg("leave\n");
pthread_exit(NULL);
return 0;
}
///////////////////////////////////////////
int main()
{
pthread_t t1, t2;
msgq_handle_t *pHMsgq = 0;
msgq_init_info_t init_info = {0};
init_info.max_msg_num = 5;
msgq_create_handle(&pHMsgq, &init_info);
gettimeofday(&g_start, NULL);
pthread_create(&t1, NULL, _thread_post, (void*)pHMsgq);
pthread_create(&t2, NULL, _thread_fetch, (void*)pHMsgq);
pthread_join(t1, NULL);
pthread_join(t2, NULL);
printf("\n=> cur msg cnt = %d\n", pHMsgq->cur_msg_cnt);
while( 0) //pHMsgq->cur_msg_cnt )
{
int *pValue = 0;
msgq_fetch_msg(pHMsgq, (void**)&pValue);
}
msgq_destroy_handle(&pHMsgq);
return 0;
}