multithreading - Problems with Immutable Data in Functional Programming -
i new functional programming. understood functional programming writing code using pure functions , without changing value of data.
instead of changing value of variables create new variables in functional programming when need update variable.
suppose have variable x
represents total number of http requests made program. if have 2 threads want threads increment x
whenever http request made thread. if both threads make different copy of variable x
how can synchronize value of x
. example: if thread 1 make 10 http requests , thread 2 made 11 http requests print 10 , 11 respectively how print 21.
i address haskell part. mvar
1 of communication mechanism threads. 1 of example taken simon marlow's book (the program self-explanatory):
main = m <- newemptymvar forkio $ putmvar m 'x'; putmvar m 'y' r <- takemvar m print r r <- takemvar m print r
the output above program be:
'x' 'y'
you can see in above example how mvar
value in variable m
shared between threads. can learn more these techniques in this book.
Comments
Post a Comment