how to restart a thread?

F

fabioscime

Hi, I'm writing a background timer using /cc++/thread.h.
this is my source code:

timer.h
-----------------------------------------------
#ifndef TIMER_H
#define TIMER_H
#include <cc++/thread.h>

using namespace std;
using namespace ost;

class Timer : public Thread
{
private:
int timeout;
public:
Timer();
~Timer();
void setTimer(float t);
void run();
};

#endif

timer.cpp
-------------------------------------
#include "timer.h"
#include <iostream>

Timer::Timer()
{

}

Timer::~Timer()
{
terminate();

}

void Timer::setTimer(float t)
{
timeout = (int)(t * 1000);

}

void Timer::run()
{
sleep(timeout);
exit();

}

I use my timer in this way:

Timer *t1 = new Timer();
float oldTimeout = 3;
t1->setTimer(oldTimeout);
t1->detach();

and then I want to restart it when expired:

oldTimeout = 2*oldTimeout;
t1->setTimer(oldTimeout);
int i = t1->detach();

but i == -1 (error) and timer does not start again.

what's wrong?
 
M

mlimber

Hi, I'm writing a background timer using /cc++/thread.h.
this is my source code:

timer.h
-----------------------------------------------
#ifndef TIMER_H
#define TIMER_H
#include <cc++/thread.h>

using namespace std;
using namespace ost;

class Timer : public Thread
{
private:
int timeout;
public:
Timer();
~Timer();
void setTimer(float t);
void run();

};

#endif

timer.cpp
-------------------------------------
#include "timer.h"
#include <iostream>

Timer::Timer()
{

}

Timer::~Timer()
{
terminate();

}

void Timer::setTimer(float t)
{
timeout = (int)(t * 1000);

}

void Timer::run()
{
sleep(timeout);
exit();

}

I use my timer in this way:

Timer *t1 = new Timer();
float oldTimeout = 3;
t1->setTimer(oldTimeout);
t1->detach();

and then I want to restart it when expired:

oldTimeout = 2*oldTimeout;
t1->setTimer(oldTimeout);
int i = t1->detach();

but i == -1 (error) and timer does not start again.

what's wrong?

We have no idea. Threads are not part of standard C++, and we don't
know anything about your thread library. Ask in a group that deals
with threads (e.g., comp.programming.threads) or in a forum for your
thread library. See:

http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.9

Cheers! --M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top