bug-commoncpp
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Why is Thread::terminate() protected?


From: Curtis Magyar
Subject: Re: Why is Thread::terminate() protected?
Date: Wed, 04 Aug 2004 11:09:27 -0500

On Wed, 2004-08-04 at 14:47 +0200, Byrial Jensen wrote:
> Hallo,
> 
> I am using threads in CommonC++ and have a situation
> where I want one thread to create another thread and
> later terminate it again. I thougt it would be possible
> to do it as outlined in the example code below:

I generally haven't had very much luck doing what you're trying to do.
I've attached a runnable test app based on your code that works more
like the approach I've had great success with.

--
Curtis Magyar
#include <cc++/thread.h>
#include <iostream>
#include <unistd.h>
#include <termios.h>

using ost::Thread;
using namespace std;

void wait_for_something ();

class MyThread : public Thread
{
private:
        bool    exitflag;

public:
        MyThread () : exitflag(false) 
                 {};
            
        virtual ~MyThread ()
                 {}; 
        
        void    quit     (void)
                 {exitflag = true;}
        
        bool    isrunning(void)
                 {return (!exitflag);}
        
protected:
        virtual void run ()
        {
                while (!exitflag)
                {
                        do_something ();
                        do_something_else ();
                };
            // Do something here on the way out if you like.
        };
        
        void    do_something()
                    {   cout << "Doing something \t... " << flush;
                        Thread::sleep(2*1000);
                        cout << "Done something" << endl; 
                    };
        
        void do_something_else ()
                    {   if (exitflag) return;
                        cout << "Doing something else \t... " << flush;
                        Thread::sleep(3*1000);
                        cout << "Done something else" << endl;
                        };
};

int main ()
{
        MyThread *mt = new MyThread ();
        mt->start ();
        wait_for_something ();
        mt->quit ();
        while (mt->isrunning()) {
            sleep(1);
            }
        delete (mt);
        return 0;
}

void wait_for_something () {
    struct termios      newtios,
                        oldtios;
    bool                exitflag    = false;
    char                key         = 0;
    
    tcgetattr(STDIN_FILENO, &oldtios);
    newtios = oldtios;
    newtios.c_lflag &= ~ICANON;
    tcsetattr(STDIN_FILENO, TCSANOW, &newtios);
    
    cout << "Press Q to quit." << endl;
    
    while (!exitflag) {
        read(STDIN_FILENO, &key, 1);
        if (toupper(key) == 'Q') {
            exitflag = true;
            }
        usleep (500);
        }
    
    cout << "\nExiting" << endl;
    tcsetattr(STDIN_FILENO, TCSANOW, &oldtios);
    }

reply via email to

[Prev in Thread] Current Thread [Next in Thread]