C ++ message queue message queue entry

Description :
When we have a plurality of threads at different speeds and we want to send information from one thread to another thread in a particular order, the message queue may be useful.

The idea is, the message sending thread push queue, the thread will receive a message pop-up at their own pace. As long as the message sending thread does not exceed the number average transmission receiving thread can be processed, the system can work. Because the queue acts as a buffer, so that messages may be transmitted burst and pop, in other words: as long as the average transmission rate over a time period less than the capacity of the recipient, the flow will reach a peak.


Routine :
The example shows an input thread that reads data from the console (CIN) and pushed to the message queue. Another thread (the recipient) checks the queue size, if the size is not zero, then the pop-up message queue, the message and the same image processing work.

Open Qt Creator, the new console application, choose MingW building components

 C++ Code 
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
 
 # ifndef MSGQUEUE_H
 # define MSGQUEUE_H
 
 # include < iostream >
 # include < cstdlib >
 # include < unistd.h >  // usleep
 # include < fcntl.h >  // threads
 # include < pthread.h >
 # include < string > 
// messages
 # include < queue >  // the message queue
 
 
using   namespace  std;
 
 pthread_mutex_t msgmutex = PTHREAD_MUTEX_INITIALIZER;
 
 queue < string > msgq;
 
 
void  *msgreceiver( void  *arg)
 {
     
long  qsize;
     string nextmsg;
     
while  ( true )
     {
         
if  (msgq.empty())
         {
             usleep(
10000 );  // sleep 0.01 sec before trying again
              continue ;
         }
 
         
// we end up here because there was something in the msg queue
         pthread_mutex_lock( & msgmutex);
         qsize = msgq.size();
         
if  (qsize >  5 )
             cout << 
"Queue size: "  << qsize << endl;
         nextmsg = msgq.front(); 
// get next message in queue
         msgq.pop();  // remove it from the queue
         pthread_mutex_unlock( & msgmutex);
 
         cout << 
"Processing value "  << nextmsg << endl;
         usleep(
2000000 );
     }
     pthread_exit((
void  * ) 0 );
 } 
// msgreceiver()
 
 
void  *msgtransmitter( void  *arg)
 {
     string nextmsg;
     
while  ( true )
     {
         cin >> nextmsg;
         pthread_mutex_lock( & msgmutex);
         msgq.push(nextmsg); 
// push message onto the queue
         pthread_mutex_unlock( & msgmutex);
     }
     pthread_exit((
void  * ) 0 );
 } 
// msgtransmitter()
 
 
int  test_msgqueue()
 {
     pthread_t thr;
 
     
// Create threads
      if  (pthread_create( & thr,  NULL , msgreceiver,  NULL ) ||
             pthread_create( & thr, 
NULL , msgtransmitter,  NULL ))
     {
         cout << 
" cannot make thread\n" ;
         exit(
1 );
     }
 
     
/*
      * At this point the main thread can perform its actions or end
      */

     cout << 
"** Main thread ends **\n" ;
     pthread_exit((
void  * ) 0 );
 
 }
 
 # endif 
// MSGQUEUE_H

Test shots :

 

Guess you like

Origin www.cnblogs.com/MakeView660/p/11813365.html