Yes, it's because on line 42 of your program, you're opening the database twice, and then on line 77, trying to write back different data to it from each of the 3 threads at once, without locking the tables.
That's what's causing your database corruption.
s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/