简体   繁体   English

pthread输入线程和工作线程同步

[英]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. 我有两个pthread,其中一个是从cin读取并将其放入队列,另一个是工作线程,每隔2秒检查一次QUEUE,并在其中包含某些内容时进行打印。

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: 其中Thread.h和Thread.cpp是:

#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: Mutex.h和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. 问题是一旦它在无限循环中等待iread线程中的stdin,rq线程就永远不会启动。 In fact, whichever .start() comes first is the one it gets stuck in... any ideas? 实际上,无论哪个.start()首先出现,都是那个被卡住的...有什么想法吗?

Turned out that I needed to run g++ with the -lpthread option. 原来,我需要使用-lpthread选项运行g ++ Does anyone know why this isn't on by default? 有谁知道为什么默认情况下不启用此功能?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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