ocaml threads grrr

2016-Dec-07, Wednesday 22:19
mindstalk: (Default)
[personal profile] mindstalk
So for a coding test I had to make a chat client-server pair in ocaml. Done, based on a select loop. Well, mostly done, I didn't address blocking writes. I decided to redo the thing with threads, for the practice. Getting the basic functionality wasn't hard: synchronized queue for communication, simpler buffered socket readers than the first version had, and such.

Getting the threads to quit gracefully, though, that took a lot longer. I think part is due to ocaml's POSIX threads implentation: I'm told that "no one" uses it, they all use LWT (lightweight threads) instead. I can confirm some deficiencies: no Thread.detach functionality, for example, which was a big roadblock. And Thread.kill is in the API documentation, but when I tried to use it, got

Thread 2 killed on uncaught exception Invalid_argument("Thread.kill: not
implemented")


Throwing an exception from the worker threads to notify the main program to join() them didn't work either, though I'm told I shouldn't expect that to work anyway. I think it works now, with a fair bit of polling -- not busy wait spinning, but reads timing out every 0.1 seconds to check for changes of state -- but that feels messy. As for whether it's simpler... well, it is fewer lines than the select() version, and should handle large (potentially blocking) messages better, so I guess so. Hmm, well, the core chat module is slightly shorter, but main is longer, and there's an extra SQueue module. Still, being more correct is good.

March 2019

S M T W T F S
      1 2
34 5678 9
101112 1314 15 16
17181920212223
24252627282930
31      

Expand Cut Tags

No cut tags

Style Credit

Most Popular Tags