简体   繁体   中英

pthread input thread and worker thread synchronization

I have two pthreads one of them is reading from cin and putting it in a QUEUE and the other one is a worker thread checking the QUEUE every 2 seconds and printing something if there is something in it.

This is what's in my main:

#include <string>
#include <queue>
#include <iostream>
#include <stdio.h>
#include "Thread.h"
#include "Mutex.h"

using namespace std;

queue<string> lineup;
Mutex lock;

class InputReader:public Thread{
    private:
        string buffer;
    protected:
        virtual void run(){
            while(true){
                cout << "Please Enter some Text:\n" ;
                getline(cin,buffer);
                lock.lock();
                lineup.push(buffer);
                lock.unlock();
            }
        }
    public:
        InputReader(){}
        ~InputReader(){}
};


class Request: public Thread{
    protected:
        virtual void run(){
            while(true){
                sleep(2);
                lock.lock();
                if ((int)(lineup.size())>0){
                    cout << "Sending Request: " << lineup.front() << endl;
                    lineup.pop();
                }
                else{
                    cout << "Nothing to send!" <<endl;
                }
                lock.unlock();

            }
        }
    public:
        Request(){}
        ~Request(){}
};

int main(){
    Request rq;InputReader iread; 
    iread.start();  rq.start();
    iread.join(); rq.join();

    return 0;
}

Where Thread.h and Thread.cpp are:

#ifndef __THREAD_H__
#define __THREAD_H__
#include <pthread.h>

class Thread
{
    private:
        pthread_t thread;
        static void * dispatch(void *);
    protected:
        virtual void run() = 0;
    public:
        virtual ~Thread();
        void start();
        void join();
};

#endif

//  THREAD.CPP
#include "Thread.h"

Thread::~Thread(){}

void * Thread::dispatch(void * ptr)
{
    if (!ptr) return 0;
    static_cast<Thread *>(ptr)->run();
    pthread_exit(ptr);
    return 0;
}

void Thread::start(){
    pthread_create(&thread, 0, Thread::dispatch, this);
}

void Thread::join()
{
    pthread_join(thread, 0);
}

Mutex.h and Mutex.cpp:

#ifndef __MUTEX_H__
#define __MUTEX_H__
#include <pthread.h>

class Mutex
{
private:
    pthread_mutex_t mutex;
public:
    Mutex();
    ~Mutex();
    void lock();
    void unlock();
    bool trylock();
};

#endif

// MUTEX.CPP -----------------------
#include "Mutex.h"
Mutex::Mutex(){
       pthread_mutex_init(&mutex, 0);
   }
   Mutex::~Mutex(){
      pthread_mutex_destroy(&mutex);
  }
  void Mutex::lock(){
      pthread_mutex_lock(&mutex);
  }
  void Mutex::unlock(){
      pthread_mutex_unlock(&mutex);
  }
  bool Mutex::trylock()  {
      return (pthread_mutex_trylock(&mutex) == 0);
  }

The problem is once its in the infinite loop waiting for stdin in the iread thread, the rq thread never starts. In fact, whichever .start() comes first is the one it gets stuck in... any ideas?

Turned out that I needed to run g++ with the -lpthread option. Does anyone know why this isn't on by default?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM