Showing posts with label boost. Show all posts
Showing posts with label boost. Show all posts

Thursday, July 19, 2012

Multithreading in Fuzuli Programming Language

Since our last commit, Fuzuli supports multithreading. This is the latest milestone that Fuzuli reached in revision tree of 0.1.x.

Fuzuli's multithreading capability stands on the well known boost threading library which will be a built-in package in next C++ standard.

Creating and running a thread in Fuzuli is easy. Define a function and create a thread for this function then call the thread_join method. The join method will wait until the function finishes its job. More than one threads can run at the same time in a time-sharing manner. Multithreading functions are stored thread.nfl, which is now a standard in Fuzuli API.

Lets give an example:


(require "/usr/lib/fuzuli/nfl/thread.nfl")

(function f (params)
 (block
  (print "F started\n")
  (foreach i in (: 1 10)
   (print "f\n")
   (thread_yield)
   (thread_sleep 100)
  )
  (return 0)
 )
)

(function g (params)
 (block
  (print "G started\n")
  (foreach i in (: 1 10)
   (print "g\n")
   (thread_yield)
   (thread_sleep 100)
  )
  (return 0)
 )
)

(function h (params)
 (block
  (print "H started\n")
  (foreach i in (: 1 10)
   (print "h\n")
   (thread_yield)
   (thread_sleep 100)
  )
  (return 0)
 )
)

(let t0 (thread "f"))
(let t1 (thread "h"))
(let t2 (thread "g"))

(thread_join t0)


In the example given above, we have three function f(), g() and h(). Those functions print messages "F started", "G started" and "H started" at the top of their bodies. A foreach loop then count from 1 to 10 and wait 100 milliseconds after each step. The output is shown below:

F started
H started
G started
g
h
f
g
h
f
g
h
f
g
h
f
g
h
f
f
g
h
f
h
g
g
f
h
g
h
f
f
g
h

Functions in this example run simultaneously. thread_join was called on t0, so whenever the function f() finishes its job, program ends. thread_yield is called for giving a chance to run to another threads. thread_sleep waits for given milliseconds if needed.That's all.