Search code examples
gnu-parallel

GNU Parallel: thread id


In GNU Parallel with the -j option it is possible to specify the number concurrent jobs.

Is it possible to get an id of the thread running the job?. With thread id I mean a number from 1 to 12 on my machine with 12 threads. As of now I use the following workaround:

doit() {
    let var=$1*12+$2
    echo $var $2
}
export -f doit
for ((i=0;i<2;++i))
do
    parallel -j12 doit ::: $i ::: {1..12} 
done

This has the problem that every iteration of the loop waits for all 12 threads to finish. I am only interested in not running iterations with the same thread id concurrently.

My motivation for this is that every thread uses a writelock on one of 12 files. I got exactly 12 files and if a thread on one file finishes, the next thread could immediately use this file again.


Solution

  • As @MarkSetchell writes you should use the replacement string {%} which gives the jobslot number:

    parallel --line-buffer -j12 'echo starting job {#} on {%}; sleep {=$_=rand()*30=}; echo finishing job {#} on {%}' ::: {1..50}