#test trying to detach thread
-sub test4 { ok(6,1,"Detach test") }
+sub test4 { ok(6,1,"Detach test"); rmdir "thrsem" }
+
+# Just a sleep() would not guarantee that we sleep and will not
+# wake up before the just created thread finishes. Instead, let's
+# use the filesystem as a semaphore. Creating a directory and removing
+# it should be a reasonably atomic operation even over NFS.
+# Also, we do not want to depend here on shared variables.
+
+mkdir "thrsem", 0700;
my $thread1 = threads->create('test4');
$thread1->detach();
-sleep 2;
+sleep 1 while -d "thrsem";
ok(7,1,"Detach test");
ok(14, 0 == threads->self->tid(),"Check so that tid for threads work for main thread");
ok(15, 0 == threads->tid(),"Check so that tid for threads work for main thread");
-1;
-
-
-
-
-
-
+END {
+ 1 while rmdir "thrsem";
+}
+1;