Skip to content

Bash multi-threading library similar to posix threads

Notifications You must be signed in to change notification settings

alienrobotarmy/bashthreads

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

Bash multi-threading library

Copyright (c) 2014 Jess Mahan

This simple to use library provides multi-threading capability for your bash scripts in a posix threads style.

Getting Started

Include this into your script:

#!/bin/bash
source ~/threads.sh

Library Functions

thread_init

Initialize the library, limiting the maximum thread count to Defining max threads is important, otherwise you could potentially fork bomb yourself.

This function MUST be called ONLY ONCE, and BEFORE any other thread calls

Create no more than this many threads

thread_create

Create a single thread of with

thread_wait should be called immediately after this.

A command to run in a new thread / process Arguments to pass to the command

thread_wait (final)

Block until we are no longer at

When called with an argument (such as "thread_wait final"), blocks parent until all threads are complete

Example:

In this example, we have a file called "hosts.txt" which contains a single hostname per line. We then create 64 simulaneous worker threads to each resolve the hostname and then exit.

#!/bin/bash

. ./threads.sh

function nslkup {
     dig +short $1
}
thread_init 64
for i in $(cat hosts.txt)
do
  thread_create nslkup ${i}
  if [ $? -eq 1 ]; then
    thread_wait
    thread_create nslkup ${i}
  fi
done
thread_wait final

About

Bash multi-threading library similar to posix threads

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages