• Stars
    star
    525
  • Rank 84,404 (Top 2 %)
  • Language
    Ruby
  • License
    Do What The F*ck ...
  • Created about 12 years ago
  • Updated almost 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Various extensions to the base thread library.

thread

Build Status

Various extensions to the thread library in ruby.

Installation

Add this line to your application's Gemfile:

gem 'thread'

Or install it yourself as:

$ gem install thread

Usage

Pool

All the implementations I looked at were either buggy or wasted CPU resources for no apparent reason, for example used a sleep of 0.01 seconds to then check for readiness and stuff like this.

This implementation uses standard locking functions to work properly across multiple Ruby implementations.

require 'thread/pool'

pool = Thread.pool(4)

10.times {
  pool.process {
    sleep 2

    puts 'lol'
  }
}

pool.shutdown

You should get 4 lols every 2 seconds and it should exit after 10 of them.

Channel

This implements a channel where you can write messages and receive messages.

require 'thread/channel'

channel = Thread.channel
channel.send 'wat'
channel.receive # => 'wat'

channel = Thread.channel { |o| o.is_a?(Integer) }
channel.send 'wat' # => ArgumentError: guard mismatch

Thread.new {
  while num = channel.receive(&:even?)
    puts 'Aye!'
  end
}

Thread.new {
  while num = channel.receive(&:odd?)
    puts 'Arrr!'
  end
}

loop {
  channel.send rand(1_000_000_000)

  sleep 0.5
}

Pipe

A pipe allows you to execute various tasks on a set of data in parallel, each datum inserted in the pipe is passed along through queues to the various functions composing the pipe, the final result is inserted in the final queue.

require 'thread/pipe'

p = Thread |-> d { d * 2 } |-> d { d * 4 }
p << 2

puts ~p # => 16

Process

A process helps reducing programming errors coming from race conditions and the like, the only way to interact with a process is through messages.

Multiple processes should talk with eachother through messages.

require 'thread/process'

p = Thread.process {
  loop {
    puts receive.inspect
  }
}

p << 42
p << 23

Promise

This implements the promise pattern, allowing you to pass around an object where you can send a value and extract a value, in a thread-safe way, accessing the value will wait for the value to be delivered.

require 'thread/promise'

p = Thread.promise

Thread.new {
  sleep 5
  p << 42
}

puts ~p # => 42

Future

A future is somewhat a promise, except you pass it a block to execute in another thread.

The value returned by the block will be the value of the promise.

By default, Thread.future executes the block in a newly-created thread.

Thread.future accepts an optional argument of type Thread.pool if you want the block executed in an existing thread-pool.

You can also use the Thread::Pool helper #future

require 'thread/future'

f = Thread.future {
  sleep 5

  42
}

puts ~f # => 42
require 'thread/pool'
require 'thread/future'

pool = Thread.pool 4
f    = Thread.future pool do
  sleep 5
  42
end

puts ~f # => 42
require 'thread/pool'
require 'thread/future'

pool = Thread.pool 4
f    = pool.future {
  sleep 5
  42
}

puts ~f # => 42

Delay

A delay is kind of a promise, except the block is called when the value is being accessed and the result is cached.

require 'thread/delay'

d = Thread.delay {
  42
}

puts ~d # => 42

Every

An every executes the block every given seconds and yields the value to the every object, you can then check if the current value is old or how much time is left until the second call is done.

require 'net/http'
require 'thread/every'

e = Thread.every(5) {
	Net::HTTP.get(URI.parse('http://www.whattimeisit.com/')).match %r{<B>(.*?)<BR>\s+(.*?)</B>}m do |m|
		{ date: m[1], time: m[2] }
	end
}

loop do
	puts ~e
end

Contributing

  1. Fork it ( https://github.com/meh/ruby-thread/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Verify new and old specs are green (rake)
  4. Commit your changes (git commit -am 'Add some feature')
  5. Push to the branch (git push origin my-new-feature)
  6. Create a new Pull Request

More Repositories

1

amnesia

Mnesia wrapper for Elixir.
Elixir
682
star
2

elixir-socket

Socket wrapping for Elixir.
Elixir
679
star
3

ruby-tesseract-ocr

A Ruby wrapper library to the tesseract-ocr API.
Ruby
627
star
4

rust-ffmpeg

Safe FFmpeg wrapper.
Rust
454
star
5

rust-tun

TUN device creation and handling.
Rust
215
star
6

elixir-datastructures

Datastructures for Elixir.
Elixir
212
star
7

rust-ffmpeg-sys

moved to meh/rust-ffmpeg
Rust
146
star
8

cancer

It's terminal.
Rust
129
star
9

urna

REST in peace.
Elixir
95
star
10

smart-referer

MOVED to GitLab: https://gitlab.com/smart-referer/smart-referer/
JavaScript
94
star
11

reagent

You need more reagents to conjure this server.
Elixir
92
star
12

dux

DUX MEA LUX
Rust
89
star
13

exquisite

LINQ-like match_spec generation for Elixir.
Elixir
78
star
14

rust-packet

Network packet handling for Rust.
Rust
76
star
15

cauldron

I wonder what kind of Elixir is boiling in there.
Elixir
74
star
16

jazz

Yet another library to handle JSON in Elixir.
Elixir
61
star
17

rorschach

And I'll look down and whisper "GNO."
Rust
59
star
18

screenruster

X11 screen saver and locker.
Rust
53
star
19

miserve

Servo based minimal browser.
PostScript
48
star
20

blender

Blend in the crowd by faking to be the most common Firefox browser version, operating system and other stuff.
Ruby
42
star
21

rust-terminfo

Terminal information for Rust.
Rust
40
star
22

rust-uinput

Linux uinput wrapper.
Rust
39
star
23

lissio

E vai col lissio!
Ruby
39
star
24

cesso

CSV handling library for Elixir.
Elixir
26
star
25

rust-xcb-util

Rust bindings and wrappers for XCB utility functions.
Rust
26
star
26

ruby-mbox

A Ruby mbox parser.
Ruby
23
star
27

steamy

Steam controller handling.
Rust
21
star
28

ruby-threadpool

A simple no-wasted-resources thread pool implementation.
Ruby
21
star
29

ruby-x11

X11 bindings for Ruby
Ruby
20
star
30

wrong

The most wrong build system and package manager for C and C++.
Makefile
19
star
31

elixir-benchmark

Elixir benchmark utilities.
Elixir
18
star
32

mad

Managers & Developers
18
star
33

rust-mailbox

MBOX reader.
Rust
17
star
34

rust-hid

HID access library.
Rust
15
star
35

moc

Mirror of the moc project, with some of my own changes
C
15
star
36

rust-shmem

Shared memory for Rust.
Rust
14
star
37

ruby-clj

Like json, but with clojure sexps.
Ruby
14
star
38

NetCommander

NetCommander - An easy to use arp spoofing tool.
Python
14
star
39

miniLOL

Not just a CMS.
JavaScript
12
star
40

derp

Elixir error logger.
Elixir
12
star
41

continuum

Spaaaaceeeeeeeeeeeee.
Elixir
12
star
42

nucular

A reactor in D.
D
12
star
43

ruby-torchat

Torchat implementation in Ruby, event-driven EventMachine based library.
Ruby
12
star
44

rust-sixel

Sixel encoder/decoder for Rust.
Rust
11
star
45

ruby-ffi-inline

Inline C/C++ in Ruby easily and cleanly.
Ruby
11
star
46

rust-lzma

LZMA handling library.
Rust
10
star
47

fag

Forums Are Gay.
Ruby
10
star
48

clj.js

parse/dump Clojure like you'd do with JSON
JavaScript
10
star
49

vimmeh

Vim Script
9
star
50

rust-bdf

BDF format handling.
Rust
9
star
51

bitlbee-omegle

Omegle plugin for BitlBee.
C
9
star
52

neovimmeh

Fennel
8
star
53

elixir-operators

Ever wanted operators which are as slow as can be? Now you can!
Elixir
8
star
54

arpoon

Man the harpoons, kill the spoofer!
Ruby
8
star
55

lulzKey

lulzKey. Let's close those effin Windows so penguins won't enter.
C++
8
star
56

exts

Elixir Terms Storage, ets wrapper.
Elixir
8
star
57

failirc

Fail IRC library in Ruby.
Ruby
7
star
58

lulzhttpd

A C++ web server.
C++
7
star
59

screenruster-saver

Helper library to create screen savers.
Rust
7
star
60

rust-uinput-sys

Linux uinput definitions.
Rust
7
star
61

elixir-managed_process

Garbage collected processes.
Elixir
7
star
62

npapi-mumble-link

Mumble Link wrapped as an NPAPI plugin.
C
7
star
63

expo

XPosed for Kotlin.
Kotlin
7
star
64

opentyrian

opentyrian mirror with some of my own changes
C
7
star
65

rust-picto

Image handling library for Rust.
Rust
6
star
66

shumei

Shumei brings an actor system and other concurrency plumbing primitives to the web.
TypeScript
6
star
67

ruby-fuse

Bindings and wrapper for FUSE with FFI.
Ruby
6
star
68

elixir-finalizer

Define finalizers using the cool NIF hack.
Elixir
6
star
69

random

Random codes.
Assembly
6
star
70

httprot

Prot prot prot.
Elixir
6
star
71

screenruster-saver-laughing_man

I thought what I'd do was, I'd pretend I was one of those deaf-mutes.
Rust
6
star
72

ruby-call-me

Various calling things, overload, pattern matching, memoization and such.
Ruby
6
star
73

rust-openal

OpenAL wrapper
Rust
6
star
74

orgasm

ORGanized ASseMbly: a Ruby (dis)?assembler library (NOT a (dis)?assembler FOR Ruby, a (dis)?assembler IN Ruby)
Ruby
6
star
75

llvm-lol

LOLCODE LLVM implementation.
C++
5
star
76

decimex

erlang-decimal wrapper for Elixir
Elixir
5
star
77

ruby-glyr

Ruby wrapper for glyr.
Ruby
5
star
78

nohomo

Homoglyphs begone!
Vim Script
5
star
79

clj-sockets

POSIX sockets for Clojure.
Clojure
5
star
80

rust-hwaddr

MAC address handling.
Rust
5
star
81

colorb

Colorify your strings
Ruby
5
star
82

rust-xkb

Rusty wrapper to libxkbcommon.
Rust
5
star
83

boolean-expression

A simple library to evaluate boolean expressions.
Ruby
5
star
84

bitlbee-torchat

Bitlbee plugin for using torchat.
C
5
star
85

amirite

Include and forget testing framework for C++11
C++
5
star
86

dexts

Disk Elixir Terms Storage, dest wrapper.
Elixir
5
star
87

nokku

Knock knock knock, hitori bocchi.
Rust
4
star
88

rust-xkbcommon-sys

Bindings to libxkbcommon.
Rust
4
star
89

screenruster-saver-hacks

ScreenRuster support for XScreenSaver hacks.
Rust
4
star
90

libcss

A library to parse and work with CSS
C
4
star
91

ela

Embedded Linear Algebra
C++
4
star
92

faildns

Fail DNS library in Ruby
Ruby
4
star
93

rust-curses-sys

Rust
3
star
94

ruby-clit

Simple library to make nice themeable CLI output.
Ruby
3
star
95

rft

Shitty Rust FFT implementation.
Rust
3
star
96

fagadget

New cross-toolkit and cross-platform desktop-gadget prototype.
C++
3
star
97

zwm

Shell
3
star
98

cryptsetup

Mirror of cryptsetup with some of my own changes
C
3
star
99

LOLastfm

LOL it's a scrobbler.
Ruby
3
star
100

parsepples

A parslet-like parser for C++
C++
3
star