多线程安全队列

Posted susidian

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多线程安全队列相关的知识,希望对你有一定的参考价值。

  1 struct QNode
  2 {
  3     struct Node *front;
  4     struct Node *tail;
  5     unsigned int len;
  6 };
  7 
  8 
  9 struct Qlist
 10 {
 11     struct QNode *qlist;
 12     pthread_mutex_t m_lock;
 13 };
 14 
 15 
 16 class MyQueue
 17 {
 18    public:
 19        MyQueue();
 20        int Push(void *pData);
 21        void* Pop();
 22        ~MyQueue();
 23    private:
 24        struct Qlist *m_qlist;
 25 };
 26 
 27 
 28 MyQueue::MyQueue()
 29 {
 30     m_qlist = (struct Qlist *)malloc(sizeof(struct Qlist));
 31     m_qlist->qlist = (struct QNode *)malloc(sizeof(struct QNode));
 32 
 33 
 34     cout<< "Create Qlist" <<endl;
 35 
 36 
 37     if(m_qlist->qlist != NULL)
 38     {
 39         m_qlist->qlist->front = NULL;
 40         m_qlist->qlist->tail = NULL;
 41         m_qlist->qlist->len = 0;
 42     }
 43     pthread_mutex_init(&m_qlist->m_lock,NULL);
 44 }
 45 
 46 
 47 int MyQueue::Push(void *pData)
 48 {
 49     struct Node *INode = NULL;
 50     if(NULL == m_qlist )
 51     {
 52         cout<< "Push Tail please initialize the list"<<endl;
 53         return -1;
 54     }
 55     if(NULL == m_qlist->qlist)
 56     {
 57         m_qlist->qlist = (struct QNode *)malloc(sizeof(struct QNode));
 58     }
 59 
 60 
 61     INode = (struct Node *)malloc(sizeof(struct Node));
 62     INode->data = pData;
 63     INode->next = NULL;
 64     cout<< "push lock "<<endl;
 65     pthread_mutex_lock(&m_qlist->m_lock);
 66     cout<<"push "<<(char*)INode->data;
 67     if(m_qlist->qlist->tail != NULL)
 68     {
 69         m_qlist->qlist->tail->next = INode;
 70         m_qlist->qlist->tail = INode;
 71     }
 72     else //if(m_qlist->qlist->tail == NULL)
 73     {
 74         m_qlist->qlist->front = m_qlist->qlist->tail =  INode;
 75     }
 76     m_qlist->qlist->len++;
 77     pthread_mutex_unlock(&m_qlist->m_lock);
 78     cout<<"push unlock"<<endl;
 79     cout<<endl; 
 80     return m_qlist->qlist->len;
 81 }
 82 
 83 
 84 void* MyQueue::Pop()
 85 {
 86     void *pData;
 87     struct Node *pNode;
 88 
 89 
 90     if((NULL == m_qlist) || (0 >= m_qlist->qlist->len) || (m_qlist->qlist == NULL))
 91     {
 92         return NULL;
 93     }
 94 
 95 
 96     cout<< "Pop Head" <<endl;
 97     pthread_mutex_lock(&m_qlist->m_lock);
 98     
 99     m_qlist->qlist->len -= 1;
100     pNode = m_qlist->qlist->front;
101     pData = pNode->data;
102     cout<<"pop "<<(char *)pData;
103     m_qlist->qlist->front = m_qlist->qlist->front->next;
104 
105 
106     if(m_qlist->qlist->len == 0)
107     {
108         m_qlist->qlist->tail = NULL;
109     }
110     free(pNode);
111     pthread_mutex_unlock(&m_qlist->m_lock);
112     cout<< "pop un_lock" <<endl;
113     cout<<endl;
114     return pData;
115 }
116 
117 
118 MyQueue::~MyQueue()
119 {
120     struct Node *pNode = NULL;
121     if(m_qlist == NULL)
122         return ;
123     
124     cout<<"Destroy Queue list"<<endl;
125 
126 
127     if(m_qlist != NULL)
128     {
129     
130         while(m_qlist->qlist->len > 0)
131         {
132 
133 
134             pNode = m_qlist->qlist->front;
135 
136 
137             cout<<"delete front: "<<(char *)(m_qlist->qlist->front->data);
138             if(pNode->data != NULL)
139             {
140 
141 
142                 free(pNode->data);
143             }
144             m_qlist->qlist->front = m_qlist->qlist->front->next;
145             m_qlist->qlist->len--;
146             free(pNode);
147         }
148 
149 
150         free(m_qlist->qlist);
151         free(m_qlist);
152     }
153     pthread_mutex_destroy(&m_qlist->m_lock);
154 }
155 
156 
157 void *Thread_push(void *arg)
158 {
159     MyQueue *tasklist = (MyQueue *)arg;
160     char *buff;
161     
162     for(int i=0;i<4;i++)
163     {
164         buff = new char(10);
165         sprintf(buff,"Msg %d
",i);
166         tasklist->Push(buff);
167         sleep(2);
168     }
169     return (void *)0;
170 }
171 
172 
173 void *Thread_pop(void *arg)
174 {
175     MyQueue *tasklist = (MyQueue *)arg;
176     char *buff;
177 
178 
179     for(int i=0;i<3;i++)
180     {
181         buff = (char *)tasklist->Pop();
182         sleep(3);
183     }
184     return (void *)0;
185 }
186 
187 字符串中最长重复子串长度
188 利用KMP中求next数组,找到最大的next值即位最长重复子串长度
189 void MaxLenRepeatStr(string a)
190 {
191 int n = a.size();
192 int max = -1;
193 for(int i=0;i<n;i++)
194 {
195 string b=a.substr(i,n-i+1);
196 int *next = new int[n-i+1];
197 Next(b,n-i+1,next);
198 for(int j=0;j<n-i+1;j++)
199 {
200 if(next[j] > max)
201 max = next[j];
202 }
203 delete []next;
204 }
205 cout<<max<<endl;
206 }

 

以上是关于多线程安全队列的主要内容,如果未能解决你的问题,请参考以下文章

java多线程 --ConcurrentLinkedQueue 非阻塞 线程安全队列

为什么我的多进程队列看起来不是线程安全的?

java多线程之队列

python端口扫描用多线程+线程安全的队列+Thread类实现

线程安全的有限大小队列

并发队列ConcurrentLinkedQueue和阻塞队列LinkedBlockingQueue用法