Skip to content

Instantly share code, notes, and snippets.

@wstucco
Last active September 27, 2017 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wstucco/89b9d0f1ed32f7f75311f17e846974c7 to your computer and use it in GitHub Desktop.
Save wstucco/89b9d0f1ed32f7f75311f17e846974c7 to your computer and use it in GitHub Desktop.
One million threads VS one million Erlang processes
defmodule Processes do
@compile :native
def create_and_forget do
1..1_000_000
|> Enum.each(fn x ->
Task.async(fn -> x + 1 end) |> Task.await
end)
IO.puts("Maximum number of threads per process is = 1000000")
end
def create_and_wait do
thread_number = 1..1_000_000
|> Enum.map(fn x ->
Task.async(fn -> x end)
end)
|> Enum.map(&Task.await/1)
|> List.last
IO.puts("Maximum number of threads per process is = #{thread_number}")
end
def create_and_return do
thread_number = 1..1_000_000
|> Enum.map(fn x ->
Task.async(fn -> x end)
end)
|> Task.yield_many(10_000)
|> List.last
|> elem(1)
|> elem(1)
IO.puts("Maximum number of threads per process is = #{thread_number}")
end
def create_and_count do
{:ok, pid} = Agent.start_link(fn -> 0 end)
1..1_000_000 |> Enum.map(fn _ ->
Task.async(fn -> Agent.update(pid, fn state -> state + 1 end) end)
end)
|> Enum.each(&Task.await/1)
thread_number = Agent.get(pid, fn state -> state end)
IO.puts("Maximum number of threads per process is = #{thread_number}")
end
end
#include <pthread.h>
#include <stdio.h>
static unsigned long long thread_nr = 0;
pthread_mutex_t mutex_;
void* inc_thread_nr(void* arg) {
/* int arr[1024][1024]; */
(void*)arg;
pthread_mutex_lock(&mutex_);
thread_nr ++;
pthread_mutex_unlock(&mutex_);
}
int main(int argc, char *argv[])
{
int err;
int cnt = 0;
pthread_mutex_init(&mutex_, NULL);
while (cnt < 1000000) {
pthread_t pid;
err = pthread_create(&pid, NULL, (void*)inc_thread_nr, NULL);
if (err != 0) {
break;
}
pthread_join(pid, NULL);
cnt++;
}
pthread_mutex_destroy(&mutex_);
printf("Maximum number of threads per process is = %llu\n", thread_nr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment