Wednesday, 2 October 2013

CFML: Another suggested tweak to CFScript: thread

G'day:
Now I think there's a chance I'm having a "senior moment" here (which I hoped I'd not be getting for another 20-odd years, but hey-ho), as I'm sure I've discussed this before, but I can find no evidence of having done so.

CFScript's support for <cfthread> is another one of those weirdly-syntaxed things like savecontent:

thread name="t1" action="run" someattribute="value" {
    // stuff here
}

What I think this should be like is this:

t1 = Thread.new({someattribute="value"}) {
    // stuff here
}

In this example the run is implicit... perhaps it could be decoupled? Would one want to do that? EG:

t1 = Thread.new({someattribute="value"}) {
    // stuff here
}
t1.run();

Obviously there's other modes to deal with here too.


Thread.join([t1,t2,t3]);

Note that this is not an object method, it's a class method (static method, whatever), ie it's not - following on from the code above:

t1.join([t2,t3]);

I'm not sure it makes sense for a join operation to be a method of a thread object? But it could be, I dunno.

That leaves sleep and and terminate. Easy:

t1.sleep(1000);
t1.terminate();

I think that's much better syntax than what we have now, and makes more sense.

Another thing that this opens up is that perhaps this means there could be other methods on the Thread object: checking its status ("running", "sleeping", "terminated", "finished" etc) and maybe other telemetry? Or being able to alter time outs and priority on the fly (is this possible though?) and stuff. Dunno. It adds scope for such enhancements anyhow.

I've been pushing for CFML to become more OO than it currently is: "Improving ColdFusion CFML's OO-ness", hence being OK with the OO-style syntax here. This is the direction CFML needs to move (and is moving... more on that in a follow-up article...), but if we had to stay all procedural about things, all the above could be dealt with with four new functions:

threadNew()
threadJoin()
threadSleep()
threadTerminate()

But I think we can agree that is not a way forward.

Thoughts? As per y/day, I'll raise an E/R in a minute, once I publish this so I can cross-ref back to it in the E/R.Done: 3644151.

--
Adam