Microsoft Face Test resolution: Using multithreading to implement a queue

Source: Internet
Author: User
Tags bool empty include int size mutex sleep thread usleep

Topic: Implementing a Queue

The queue is applied to the following scenarios:

A producer thread row the number of int types, and a consumer thread moves the number of int type out.

Analysis:

First you have to design a queue, and preferably a loop queue, otherwise the space inside the queue is easy to use the finished.

The topic requires the use of producer and consumer threads, so it is designed to be thread-protected, otherwise it is easy to mistake data and push data. You can use mutex variables for threads: pthread_mutex_t

Add Pthread_mutex_lock Lock, Unlock: Pthread_mutex_unlock

For example: producer thread every 1s push: 1,2,3,4,5,6,7,8,9,10 these 10 digits into the queue, consumers every 2s from this queue to take 1 digits, until the end.

Implemented as follows:

#include <iostream> #include <pthread.h> using namespace std;  
        Template <typename T, int size=1024> class Circularqueue {public:t queue[size];  
        int begin, END;  
                Circularqueue (): Begin (0), end (0) {} ~circularqueue () {} bool Empty () {  
        return begin = = END;  
        BOOL Full () {return (end + 1)%size = = begin;  
                } void Waitfull () {int st = 1;  
                        while (full ()) {usleep (ST);  
                st = min (1000, st*2);  
                } void Waitempty () {int st = 1;  
                        while (empty ()) {usleep (ST);  
                st = min (1000, st*2); } void push (const T& T) {waitfull ();  
                Queue[end] = t;  
        End = (end + 1)%size;  
                BOOL Pop (t& T) {//waitempty ();  
                if (empty ()) return false;  
                t = Queue[begin];  
                Begin = (begin + 1)%size;  
        return true; 
                }/* t pop () {t; 
        return pop (t);  
      
} 
        */
};  
        Template <typename T, int SIZE = 1024> class Lockedqueue {circularqueue<t, size+1> cq;  
pthread_mutex_t Mutex;  
                Public:lockedqueue () {cq.begin = 0;  
                cq.end = 0;  
        Pthread_mutex_init (&mutex, NULL);  
        } ~lockedqueue () {Pthread_mutex_destroy (&mutex);  
   } void push (const t& T) {             Pthread_mutex_lock (&mutex);  
                Cq.push (t);  
        Pthread_mutex_unlock (&mutex);  
                BOOL Pop (t& T) {pthread_mutex_lock (&mutex);  
                BOOL BP = Cq.pop (t);  
                Pthread_mutex_unlock (&mutex);  
                if (!BP) return false;  
      
      
        return true; 
                }/* t pop () {t; 
                Pop (t); 
        return t;  
      
}*/
};  
        void *product_function (void *arg) {lockedqueue<int, 1024>* FLQ = (lockedqueue<int, 1024>*) arg;  
        int i = 0;  
                while (I <) {i + +;  
                Flq->push (i);  
                cout << "Product:" << i << Endl;  
        Sleep (1); } void *consume_function (void *arg) {lockedqueue<int, 1024>* FLQ = (lockedqueue<int, 1024>*) arg;  
                while (1) {int a = 0;  
                Sleep (2);  
                if (Flq->pop (a)) cout << "consume:" << a << Endl;  
                else cout << "Queue empty!" << Endl;  
        if (a >=) break;  
        int main () {//test productor, Consumor pthread_t thread1, thread2;  
        Lockedqueue<int, 1024> LQ;  
        Pthread_create (&thread1, NULL, Product_function, &AMP;LQ);  
        Pthread_create (&thread2, NULL, Consume_function, &AMP;LQ);  
      
        Pthread_join (Thread1, NULL);  
        Pthread_join (Thread2, NULL);  
return 0; }

Compile using g++:

g++ 29.cpp-o 29-lpthread

Output results:

Product:1
Product:2
Consume:1
Product:3
Product:4
Consume:2
Product:5
Product:6
Consume:3
Product:7
Product:8
Consume:4
Product:9
Product:10
Consume:5
Consume:6
Consume:7
Consume:8
Consume:9
Consume:10

Author: csdn Blog hhh3h

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.