Re: [q-lang-users] OpenBSD, threading
Brought to you by:
agraef
From: <Dr....@t-...> - 2004-02-02 16:47:34
|
Julian Fondren wrote: > ;; OpenBSD [...] > > Everything compiles fine expect for > modules/clib/clib.c > -- there I had to add an "#include <unistd.h>", since > something > apparently failed to define HAVE_UNISTD_H That's *really* strange. Could you please check the generated config.h (in the toplevel directory of the sources), to see if it defines HAVE_UNISTD_H? > (I also get many complaints about including malloc.h > instead of stdlib.h, > but nothing seems hurt by this.) Yes, I noticed this on older (4.x) FreeBSD systems, too. I actually think that it's OpenBSD's fault, and since it's harmless I probably won't do anything about it. > ;; threading > [original source snipped] > I've already uglified it a bit, trying to figure out > the problem. On my > machine I get the "Got connection from: ..." message > on a connection, > and on subsequent connections, but the (thread > (echo_loop C)) never seems > to happen. I know that everything but the threading > works, because I > had this server running in exactly this form before I > started trying to > make it multithreaded. Hmm, I tried to run your example, but somehow I can't get it to work, the accept call in the server always fails with EINVAL for me. Here's the call sequence I used for initializing the server and the client, I guess that's correct so far? Maybe I'm missing something obvious? def S = server 5001 def C = socket AF_INET SOCK_STREAM 0 connect C ("localhost",5001) Anyway, the problem with your code is that you don't keep the handle for the echo_loop thread. You see, the thread will be canceled automatically as soon as the thread handle T is garbage collected. Here this happens when server_loop FD tail-reduces to itself (the local T variable is not used anymore and its value is therefore thrown away). The solution is to keep track of the thread handles, either in the server_loop function or in echo_loop itself. The latter solution would be something like the following: server_loop FD = printf "Got connection from: %s:%d\n" (H,P) || server_loop FD where (C:Int,(H,P)) = accept FD, _ = thread (echo_loop this_thread C); = perror "server(accept)" || server_loop FD otherwise; echo_loop T FD = printf "(loop %d)\n" FD || echo_loop T FD where MSG = recv FD 0 4096, () = echo FD MSG (bcmp MSG (bytestr "")); = closesocket FD otherwise; (As you'll notice I changed the second server_loop rule to track down the cause of the mysterious accept failures I encountered. Also note that you don't have to explicitly cancel this_thread on exit from echo_loop, in the second rule for echo_loop, as the thread will be terminated anyway when the loop ends.) Can you try whether this works for you? Cheers, Albert -- Dr. Albert Gr"af Email: Dr....@t-..., ag...@mu... WWW: http://www.musikwissenschaft.uni-mainz.de/~ag |