Re: Multithreading stuff



The usual way is to send a signal to the thread which tells him to stop and wait for him to finish. Some libraries provide methods to throw exceptions within the thread, which stops him of course. I think you can rebuild this behavour by sharing one Dispatcher which gets bound by the thread to
one function throwing an exception.


Glib::Dispatcher signal_kill;

class AbortException : public std::runtime_exception {};

void kill_me()
{
    throw(new AbortException() );
}

void thread_func()
{
  signal_kill.connect(sigc::ptr_fun(&kill_me));

  doSomethingTricky();
}

int main()
{
Glib::RefPtr<Glib::Thread> thr = Glib::Thread::create(sigc::ptr_fun(&thread_func), true);

  Glib::usleep(2000);

  if(thr->joinable())
  {
    signal_kill();
    thr->join();
  }
  return 0;
}


Hello everyone.
I'd like to write a program which runs a thread in the background, which
runs a loop, accessing devices, and uses usleep inside the loop. I also
want a way to abort this thread when button is pressed. What is the best
(most efficient, most correct) way to do it?
What is the best way to run abortable background task using only what
Gtkmm provides?
There is no demand to run multiple tasks, only one.



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