On Sun, 16 Jun 2013 16:27:27 +0100, Gary Willoughby <d...@kalekold.net> wrote:

I'm writing a little program in D to perform some database operations and have a small question about design.

Part of my program watches a log file for changes and this involves code which is wrapped up in a class. So the usage is something like this:

auto fileWatcher = new FileWatcher(fileName);
fileWatcher.onChange(delegate);
fileWatcher.start();

Once the start method is called a loop is entered within the class and the file is watched. Changes are handle through calling the registered delegate. The loop uses different watch methods for different platforms.

What i need to be able to do is to stop the current watch and change the watched file.

Because this is in an infinite loop, i can't check externally i.e. outside of the class, if i need to break from the loop simply because control never returns to the caller of the start() method.

I would derive FileWatcher from Thread:
http://dlang.org/phobos/core_thread.html#.Thread

So, now control does return to the caller - so you can create these in you main control thread before going off to do other things.

Note that Thread does not have a stop() method. This is likely because ruthlessly terminating a thread is generally frowned upon. Instead you 'ask' the thread nicely to stop (first).

My preferred method of doing this is to have a boolean loop control variable, plus an event.

So your main thread loop checks the loop control variable, and when the thread wants to sleep/wait for any reason it waits on the event. In this way you can instantly wake it up by setting the event, and you can instantly stop it by setting the loop control variable and waking it up.

e.g.

class FileWatcher : Thread
{
private:
  bool stopping;
  <event> sleepEvent;

  void run()
  {
    while (!stopping)
    {
      // ..your main loop code here..
      // if you need to wait/sleep
      <wait on sleepEvent>
// always check stopping after waiting/sleeping if your loop processes more after this
      if (stopping)
        break;
      // ..more loop code here..
    }
  }

public:
  void stop()
  {
    stopping = true;
    <set the event>
  }
}

R

--
Using Opera's revolutionary email client: http://www.opera.com/mail/

Reply via email to