Thread_create/3 and test/edit/make of a single file Prolog program

Hi Forum!

I haven’t been able to find a nice way to start a named thread at the beginning of a single prolog file. I have been starting a thread in a single file using initialize/2, catch/3, setup_call_cleanup/3 and etc. , I could report a few weird behaviours when using these… but these weird behaviours cause problems (that can be fixed) only in development phase while doing test/re-edit/make cycles.

The named thread is doing things like watching changes in file system, polling these changes, so it is like a side-effect

Now I realized that the nicest way is to start a thread in a specific dedicated module and in main module use that module with use_module/3 Am I right?? Putting the named thread in a separate module, start it there and use that module in test/edit/make-cycle and make/0 takes care of things nicely??? But that is also sort of clumsy.

The initialization directive could have some option to start a named thread and ignore that it exists while doing test/edit/make cycles??

Thanks,
Pasi.

First of all, modules and threads are completely unrelated, so start in makes little sense. You can have it run a predicate that is defined in some module. I’m normally not a fan of starting threads through e.g., initialization/1 and prefer to split loading the code from running it. If possible I try to create things such as threads lazily when they are needed. This both avoids the start-at-load as well as the need to think about running it.

In most of these scenarios you still need something to start if it isn’t started. Simply use

catch(thread_create(start, _, [alias(x)]), error(permission_error(create, thread, x),_), true).

This is the only safe way to avoid double starting (from two threads) or not not starting.