You are not logged in.

#1 2011-06-27 23:38:07

countd
Member
Registered: 2011-06-27
Posts: 2

[SOLVED] Stumpwm: Running a command at specific intervals

I have been dabbling in common lisp for a few months now, to the point where it became a replacement for bash scripts (which I never really "got" into, anyway). After using ratpoison for quite a while I stumbled upon Stumpwm - a, so far, perfect window manager (language for which I already know, unlike xmonad).

After playing around with the rc file for a few weeks, I run into a minor problem. It is thus: is there a way to, without freezing the wm, make a function run, do something akin to 'sleep 1000', and then run again? Kinda like launching a bash script or something with 'sleep' in it, and adding "&" at the end, if that makes sense...

Oh, and I am using SBCL.

Last edited by countd (2011-06-28 21:24:45)

Offline

#2 2011-06-28 21:00:03

jiyuu
Member
Registered: 2010-04-13
Posts: 63

Re: [SOLVED] Stumpwm: Running a command at specific intervals

SBCL has its own implementation of thread. It's in the sb-thread package.

Example of printing "Hello, World!" to stdout every 10 seconds, forever:

(sb-thread:make-thread
  (lambda ()
    (loop do
      (progn
        (sleep 10)
        (format t "Hello, World!~%")))))

But it's good practice to create a thread pool so you can terminate them or wait for them to finish when your program exits (i.e. when you quit stumpwm).

(use-package :sb-thread)                                                                                                                                            

(defparameter *threads* '())  ; we stock all threads here

;; Helper to create and stock thread
(defun do-in-thread (fn)
  (setf *threads*
        (cons
          (make-thread fn)
          *threads*)))

;; Create some threads that loop forever
(do-in-thread
  (lambda ()
    (loop do
          (progn
            (sleep 3)
            (format t "hello every 3sec~%")))))

(do-in-thread
  (lambda ()
    (loop do
          (progn
            (sleep 4)
            (format t "Bye every 4sec~%")))))

;; Call this function when quiting stumpwm
(defun kill-all-threads ()
  (loop for th in *threads*
        do
         (if (thread-alive-p th) (terminate-thread th))))

Last edited by jiyuu (2011-06-28 21:01:38)

Offline

#3 2011-06-28 21:24:14

countd
Member
Registered: 2011-06-27
Posts: 2

Re: [SOLVED] Stumpwm: Running a command at specific intervals

Thank you! I guess I should have paid more attention to the docs =\

Offline

Board footer

Powered by FluxBB