0

我正在尝试开发一个控制台应用程序,我将在其中实时显示系统日期和时间(或尽可能真实)。这是简单的部分。困难的部分是我还必须有光标可供用户输入信息。我不能在我的应用程序中使用 NCurses,也不能在 vanilla GCC 4.4 中使用它不包含的任何其他库(有提升!Noooo....)

到目前为止,这是我的代码:

实时类,我将 Jeremy Friesner 在这里给出的解决方案合并到pthreads in c++ inside classes

#ifndef _REALTIME_H_
#define _REALTIME_H_
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>    

class MyThreadClass
{
public:
   MyThreadClass() {/* empty */}
   virtual ~MyThreadClass() {/* empty */}

   /** Returns true if the thread was successfully started, false if there was an error starting the thread */
   bool startMainThread()
   {
      return (pthread_create(&_mainThread, NULL, mainRunnerFunc, this) == 0);
   }

   bool startDisplayThread()
   {
      return (pthread_create(&_displayThread, NULL, displayThreadFunc, this) == 0);
   }

   /** Will not return until the main thread has exited. */
   void waitForMainThreadToExit()
   {
      (void) pthread_join(_mainThread, NULL);
   }

   void waitForDisplayThreadToExit()
   {
      (void) pthread_join(_displayThread, NULL);
   }

protected:
   /** Implement this method in your subclass with the code you want your thread to run. */
   virtual void mainRunner() = 0;
   virtual void displayTime() = 0;

private:
   static void * mainRunnerFunc(void * This) {((MyThreadClass *)This)->mainRunner(); return NULL;}
   static void * displayThreadFunc(void * This) {((MyThreadClass *)This)->displayTime(); return NULL;}
   pthread_t _mainThread;
   pthread_t _displayThread;
};

class DynamicTime : public MyThreadClass
{
private:
    const string currentDate();
    void gotoxy(int,int);
    void displayTime();
    void mainRunner();
    pthread_mutex_t mutex1;
public:
//    pthread_mutex_t mutex1;
    DynamicTime();
    unsigned int lifeTime;
    unsigned int updateTime;
    void start();
    int Exit;
};

const string DynamicTime::currentDate()
{
    time_t now = time(0);
    struct tm tstruct;
    char buf[80];
    tstruct = *localtime(&now);
    strftime(buf,sizeof(buf),"%I:%M:%S %p, %d-%m-%y",&tstruct);
    return buf;
}

DynamicTime::DynamicTime()
{
    pthread_mutex_init(&(mutex1),NULL);
    lifeTime=-1; /* 100 seconds */
    updateTime = 1; /* 5 seconds interval */
    Exit=1;
}

void DynamicTime::gotoxy(int x,int y)
{
    /* go to location */
    printf("\033[%d;%df",y,x);
}

void DynamicTime::displayTime()
{
    pthread_mutex_lock(&mutex1);
    /* save the cursor location */
    printf("\033[s");
    gotoxy(75,30);
    cout << "Date : " << currentDate() << endl;
    /* restore the cursor location */
    printf("\033[u");
    pthread_mutex_unlock(&mutex1);
}

void DynamicTime::mainRunner()
{
    unsigned long iterate, iterate2;
    int iret1,iret2;
    if(lifeTime!=-1)
    {
        for(iterate=0;iterate<lifeTime*100000;iterate++)
        {
            if(iterate%(updateTime*50)==0)
            {
                iret2 = startDisplayThread();
                waitForDisplayThreadToExit();
            }
            for(iterate2=0;iterate2<100000;iterate2++);
        }
        std::cout << "Ending main thread..." << endl;
    }
    else
    {
        while(1&Exit) /* infinitely */
        {
            iret2 = startDisplayThread();
            waitForDisplayThreadToExit();
            for(iterate2=0;iterate2<100000;iterate2++);
        }
        std::cout << "Exiting Application.... " << endl;
    }
}

void DynamicTime::start()
{
    //system("clear");
    //cout << "Starting...."  << endl;
    if(startMainThread()==false)
    {
        std::cerr << "Coudln't start main Thread! " << endl;
    }
    /* call this function in the main program
     * else
    {
        waitForMainThreadToExit();
    }*/
}

/* Example
 * on how to use the program
 * int main()
{
    DynamicTime DT;
    DT.lifeTime = 100;
    DT.start();
    return 0;
}
*/
#endif

和我的示例程序,我试图从用户那里读取数据,同时显示时间:

//#include <iostream>
#include "realtime2.h"   

int main()
{
    DynamicTime DT;
    string temp="abcd";
    DT.start();
    while(temp!="exit")
    {
        std::cout << "$> " ;
        std::cin >> temp;
    }
    DT.waitForMainThreadToExit();
    return 0;
}

如果我能让用户在不中断显示线程的情况下输入数据,这将被称为全功能程序。关于如何解决这个问题的任何想法?或者如果我无法解决这个问题,那么正确的方法是什么?

4

0 回答 0