- define a string array containing three names
- define a function that takes a name, waits 1 second, and then greets (to stdout) the name
- call the greeting function in parallel with all three names so that all greetings hit stdout but the multiple 1 second waits happen in parallel
Last active
March 1, 2018 01:16
-
-
Save fallwith/c96d2e6db3ecbc8fcec50adfee687e75 to your computer and use it in GitHub Desktop.
Parallel processing examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <unistd.h> | |
#include <pthread.h> | |
void *greet(void *name_ptr) { | |
sleep(1); | |
printf("Hello, %s\n", name_ptr); | |
return NULL; | |
} | |
int main() { | |
char* names[3]; | |
names[0] = "Fred"; | |
names[1] = "Albert"; | |
names[2] = "Simon"; | |
pthread_t threads[3]; | |
for( int i = 0; i < 3; i = i + 1 ){ | |
if(pthread_create(&threads[i], NULL, greet, names[i])) { | |
fprintf(stderr, "Error creating thread\n"); | |
return 1; | |
} | |
} | |
for( int i = 0; i < 3; i = i + 1 ){ | |
if(pthread_join(threads[i], NULL)) { | |
fprintf(stderr, "Error joining thread\n"); | |
return 2; | |
} | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Crystal example provided by Ylan Segal (https://github.com/ylansegal) | |
class Greeter | |
property name : String | |
def initialize(@name) | |
end | |
def slow_hello | |
sleep(1) | |
"Hello, #{name}" | |
end | |
end | |
names = ["Fred", "Albert", "Simon"] | |
channel = Channel(String).new | |
names.each do |name| | |
spawn do | |
channel.send(Greeter.new(name).slow_hello) | |
end | |
end | |
names.map { | |
puts channel.receive | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Elixir example provided by Ylan Segal (https://github.com/ylansegal) | |
defmodule Greeter do | |
def hello(name) do | |
Process.sleep(1_000) | |
"Hello, #{name}!" | |
end | |
end | |
["Fred", "Albert", "Simon"] | |
|> Enum.map(&Task.async(fn -> Greeter.hello(&1) end)) | |
|> Enum.map(&Task.await/1) | |
|> Enum.map(&IO.puts(&1)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sync" | |
"time" | |
) | |
func greet(name string) { | |
time.Sleep(time.Second) | |
fmt.Println("Hello,", name) | |
} | |
func main() { | |
var names = [3]string{"Fred", "Albert", "Simon"} | |
var wg sync.WaitGroup | |
wg.Add(len(names)) | |
for _, name := range names { | |
go func(n string) { | |
defer wg.Done() | |
greet(n) | |
}(name) | |
} | |
wg.Wait() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
'use strict' | |
function greet (name) { | |
setTimeout(() => { | |
console.log(`Hello, ${name}`) | |
}, 1000) | |
} | |
const names = ['Fred', 'Albert', 'Simon'] | |
for (let idx in names) { | |
greet(names[idx]) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use threads; | |
sub greet { | |
sleep 1; | |
my $name = $_[0]; | |
print "Hello, $name!\n" | |
} | |
my @names = qw(Fred Albert Simon); | |
my @threads; | |
for my $name (@names) { | |
push @threads, threads->create(sub { greet($name) }) | |
} | |
map {$_->join()} @threads; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import threading | |
import time | |
def greet(name): | |
time.sleep(1) | |
print(f'Hello, {name}') | |
names = ["Fred", "Albert", "Simon"] | |
threads = [] | |
for name in names: | |
t = threading.Thread(target=greet, args=(name,)) | |
threads.append(t) | |
t.start() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
def greet(name) | |
sleep(1) | |
puts "Hello, #{name}" | |
end | |
names = %w[Fred Albert Simon] | |
threads = [] | |
names.each do |name| | |
threads << Thread.new { greet(name) } | |
end | |
threads.map(&:join) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::{thread, time}; | |
fn greet(name: &str) { | |
thread::sleep(time::Duration::from_millis(1000)); | |
println!("Hello, {}", name); | |
} | |
fn main() { | |
let names = ["Fred", "Albert", "Simon"]; | |
let mut threads = vec![]; | |
for i in 0..names.len() { | |
threads.push(thread::spawn(move || { | |
greet(names[i]); | |
})); | |
} | |
for thread in threads { | |
let _ = thread.join(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Bash example provided by Seren Thompson (https://github.com/seren) | |
func_slow_hello () { | |
sleep 1 | |
echo "Hello $1" | |
} | |
names=( Fred Albert Simon ) | |
pids=() | |
for name in "${names[@]}"; do | |
func_slow_hello "$name" & | |
pids[${#pids[@]}]=$! | |
done | |
for pid in ${pids[*]}; do | |
wait $pid | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment