Giter Site home page Giter Site logo

fp1's Introduction

Conor Finegan

[email protected]

3/12/2016

For my fist exploration, I used a multi-threading library called job-queue. job-queue is a multi-threading library that allows the user to create a queue with a specified number of worker threads. Jobs, which are just parameterless procedures, can then be send to the queue and executed asynchronously. To demonstrate this functionality, I created a small program that finds and outputs all the prime numbers below a given value. Each test number is represented by a job, so a higher number of worker threads means faster execution.

Here is the source code for my project:

; prints the given value if it is prime
(define (print-if-prime n)
  (unless (not (prime? n)) (display n) (newline)))

; creates a closure around a procedure that prints the given value if it is prime
(define (create-prime-proc n)
  (λ () (print-if-prime n)))

; calculates and displays all primes between 2 and the given max-prime
; uses multi-threading with a maximum number of threads specified by max-threads
(define (calc-primes max-prime max-threads)
  (define primes-queue (make-job-queue max-threads))
  (for ([i (+ max-prime 1)])
    (submit-job! primes-queue (create-prime-proc i))))

If called with (calc-primes 50 4), the following output is generated:

> (calc-primes 50 4)
2
3
5
7
11
13
17
19
23
31
29
43
37
41
47

Note that some numbers were printed out of order. A less naive implementation might store the primes in a list, and sort them before outputing.

Here is a diagram outlining how the algorithm processes data:

job-queue diagram

fp1's People

Contributors

cfinegan avatar marksherman avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.