Still no thread possibility?

Started by dexter, October 16, 2013, 11:47:10 PM

Previous topic - Next topic

dexter

Can it just have a function like spawn or fork to work with thread?



like (thread xx (func)) ?

Lutz

#1
Process based fork and spawn together with the message functions send and receive enable a much cleaner and easier programming of concurrency than would be possible with threads which by default share memory and file descriptors. newLISP's very small startup time in part compensates for the quicker startup time of threads.

dexter

#2
haha Thanks lutz



I am doing a project on tplink tlwr703n with openwrt



I use newlisp ,I know it's very simple and easy



I think if I can user thead/pthread, I can have more than process to do my job



right?



such as 20 threads to 10 processes



BTW: can newlisp import pthread libs to do thread jobs?

Lutz

#3
Yes, threads use less memory than processes with spawn or fork, but you should be able to fit at leats 2 - 3 into 1 Mbyte of memory. I think a small travelrouter like the TP-Link TL-WR703N has 32 Mbyte of memory, but I don't know how much is left over when the Linux OS is running. There also may be a limit of processes set by the Linux startup configuration.



Importing the Pthreads library will not help you much, as it only can call C-functions, but not newLISP functions.



In any case, running newLISP on a travel router sounds like a fun project :)

dexter

#4
Yeah lutz



I tested it, WR703N



seems can fork about 60-65 newlisp processs Max



Newlisp functions are so different to C-functions? I thought they are almost the same thing...



Is it really that support thread is not easy for newlisp or you just dont like thread design?



I think make a thread in newlisp is not like in elisp or common lisp, after all ,newlisp is very very simple but powerful



Am I right??

Lutz

#5
With threads you have to either protect certain data structures in the interpreter against access from different threads or you have to copy these data for each thread. When using fork or the fork based spawn, the whole process and data are copied by the underlying OS and there is very little you have to implement your self.



The OS also takes care of distributing the different processes optimal onto different CPUs or cores. And they say that Linux is especially efficient at that.



Threads put more burden on either the implementer or the user. With processes most of the work is done by the OS. Threads are for low level programming work, not something high level programmers should be bothered with.



How did you make the newLISP executable for OpenWRT? Did you cross compile on a desktop computer or did you just use gcc directly under OpenWRT on the WR703N?

dexter

#6

# makefile for newLISP v.10.x.x on Openwrt LINUX without readline support
# I use upx to compress as hell as I can on newlisp
# Note, that readline support may require different libraries on different OSs
#

OBJS = newlisp.o nl-symbol.o nl-math.o nl-list.o nl-liststr.o nl-string.o nl-filesys.o
        nl-sock.o nl-import.o nl-xml-json.o nl-web.o nl-matrix.o nl-debug.o pcre.o

#CFLAGS = -Wall -pedantic -Wno-uninitialized -c -O2 -g -DLINUX
CFLAGS = -Wall -Wl,--gc-sections  -ffunction-sections -fdata-sections  -c -Os  -fno-threadsafe-statics  -DLINUX -I$(TARGET_DIR)/usr/include/
LDFLAGS = -L$(TARGET_DIR)/usr/lib/  -W1,--gc-sections -lm -ldl
CC = mips-openwrt-linux-gcc
LD = mips-openwrt-linux-ld


default: $(OBJS)
$(CC) $(OBJS)  -o newlisp $(LDFLAGS)   #for openwrt
#    $(CC) $(OBJS) -g -lm -ldl -lreadline -ltermcap -o newlisp # slackware
#    $(CC) $(OBJS) -g -lm -ldl -lreadline -lncurses -o newlisp # other Linux Dist
#    $(CC) $(OBJS) -g -lm -ldl -o newlisp # without readline support
$(STRIP) newlisp
upx --best -o newlisp_s newlisp
rm newlisp
mv newlisp_s newlisp

.c.o:
$(CC) $(CFLAGS) $<

$(OBJS): primes.h protos.h makefile_linux_openwrt


Cross compile it, by openwrt's SDK



This is my makefile for newlisp on openwrt



Then make a package follow openwrt's package instruction

then I use upx to compress the binany file to 120 kb