c++ - Writing at multiple positions in real-time -


i trying develop console application, display system date , time in real time (or real can get). easy part. hard part must have cursor available user enter information through. can't use ncurses in application, nor other library not included in vanilla gcc 4.4 (there goes boost! noooo....)

this code far:

the realtime class, incorporating solution given jeremy friesner here 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 thread started, false if there error starting thread */    bool startmainthread()    {       return (pthread_create(&_mainthread, null, mainrunnerfunc, this) == 0);    }     bool startdisplaythread()    {       return (pthread_create(&_displaythread, null, displaythreadfunc, this) == 0);    }     /** not return until main thread has exited. */    void waitformainthreadtoexit()    {       (void) pthread_join(_mainthread, null);    }     void waitfordisplaythreadtoexit()    {       (void) pthread_join(_displaythread, null);    }  protected:    /** implement method in subclass code want thread 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 = 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 location */     printf("\033[%d;%df",y,x); }  void dynamictime::displaytime() {     pthread_mutex_lock(&mutex1);     /* save cursor location */     printf("\033[s");     gotoxy(75,30);     cout << "date : " << currentdate() << endl;     /* restore 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 function in main program      * else     {         waitformainthreadtoexit();     }*/ }  /* example  * on how use program  * int main() {     dynamictime dt;     dt.lifetime = 100;     dt.start();     return 0; } */ #endif 

and example program, trying read data user, while showing time @ same time:

//#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; } 

this called fully-functional program, if user enter data without interruption display thread. ideas how around ? or if can't around this, proper way ?


Comments

Popular posts from this blog

matlab - Deleting rows with specific rules -

jquery - How would i go about shortening this code? And to cancel the previous click on click of new section? -