Date: Wed, 9 Nov 2011 06:06:00 -0800 (PST) From: Unga <unga888@yahoo.com> To: "freebsd-stable@freebsd.org" <freebsd-stable@freebsd.org> Subject: pthreads mostly hang when signal Message-ID: <1320847560.49916.YahooMailNeo@web160114.mail.bf1.yahoo.com>
next in thread | raw e-mail | index | archive | help
Hi all=0A=0AI have a C program with pthreads.=0A=0AThis program creates tho= usands of detached pthreads for short jobs at the beginning. They all come = and go within the first 10 seconds.=0A=0A=0ABut it has 3 permanently runnin= g pthreads.=0A=0AI signal these 3 permanently running pthreads to stop proc= essing. I send SIGUSR1 for this purpose.=0A=0AIf I don't interrupt these 3 = permanently running pthreads, they run without any issue and do their job.= =0A=0AIf I send the SIGUSR1 to these 3 permanently running pthreads, they r= arely work. That is, these threads immediately receive SIGUSR1 signals.=0A= =0ABut when I signal, they mostly hang. That is, these threads don't receiv= e the signal.=0A=0A=0AFollowing code fragment shows how I send the signal a= nd wait till they stop:=0A=A0=A0=A0=A0 LockMutex(threadCountMutex);=A0=A0 = =0A=A0=A0=A0=A0 tCount =3D threadCount;=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0= =A0 =0A=A0=A0=A0=A0 UnlockMutex(threadCountMutex); =0A=A0=0A=A0=A0=A0=A0 pr= intf("B4 stop Thread_1. Threads: %d\n", tCount); =0A=A0=A0=A0=A0 =0A=A0=A0= =A0=A0 LockMutex(Thread_1varMutex);=0A=A0=A0=A0 Thread_1var.Thread_1Stopped= =3D 0; // Thread_1 not stopped yet.=0A=A0=A0=A0=A0 UnlockMutex(Thread_1var= Mutex);=0A=0A=0A=A0=A0=A0=A0 if (pthread_kill(tid1, SIGUSR1) !=3D 0) // Sen= d a signal to the thread=0A=A0=A0=A0=A0=A0=A0=A0 {=A0=A0=A0 =A0=A0=A0 =A0= =A0=A0 =A0=A0=A0 =A0=A0=A0 =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 // to stop proces= sing.=0A=A0=A0=A0=A0=A0=A0=A0=A0 fprintf(stderr, "pthread_kill failed for T= hread_1!\n");=0A=A0=A0=A0=A0=A0=A0=A0=A0 exit(1);=0A=A0=A0=A0=A0=A0=A0=A0 }= =0A=A0=A0=A0 Delay(25); // Let Thread_1 to settle.=0A=0A=0A=A0=A0=A0=A0 // = Check now whether the Thread_1 thread received the signal and stopped proce= ssing.=0A=A0=A0=A0=A0 for (threadActivateDelay=3D0 ;threadActivateDelay < t= hreadActivateTimeOut; threadActivateDelay +=3D 50)=0A=A0=A0=A0=A0 {=0A=A0= =A0=A0=A0=A0 LockMutex(Thread_1varMutex);=0A=A0=A0=A0=A0=A0 Thread_1Stopped= =3D Thread_1var.Thread_1Stopped;=0A=A0=A0=A0=A0=A0 UnlockMutex(Thread_1var= Mutex);=0A=A0=A0=A0=A0=A0 =0A=A0=A0=A0=A0=A0 if (Thread_1Stopped)=0A=A0=A0= =A0=A0=A0=A0=A0=A0 break;=0A=A0=A0=A0=A0=A0 else=0A=A0=A0=A0=A0=A0=A0=A0=A0= Delay(50); // Let Thread_1 thread to settles. 50ms=0A=0A=A0=A0=A0=A0=A0 Lo= ckMutex(threadCountMutex);=A0=A0 =0A=A0=A0=A0=A0=A0 tCount =3D threadCount;= =A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0 =0A=A0=A0=A0=A0=A0 UnlockMutex(thre= adCountMutex); =0A=A0=A0=A0 =A0=0A=A0=A0=A0=A0=A0 printf("Wait till Thread_= 1 stopped, Threads: %d=A0 Delay: %d\n", tCount, threadActivateDelay+50); = =A0=A0=A0 =0A=A0=A0=A0=A0 }=0A=0A=A0=A0=A0=A0 printf("Came out of Thread_1 = loop, threadActivateDelay: %d, threadActivateTimeOut: %d\n", threadActivate= Delay, threadActivateTimeOut); =0A=A0=A0=A0=A0 if (threadActivateDelay >=3D= threadActivateTimeOut) // Something is wrong. Thread may be dead.=0A=A0=A0= =A0=A0=A0=A0=A0 {=0A=A0=A0=A0 =A0fprintf(stderr, "Time out. Thread_1 may be= dead!\n");=0A=A0=A0=A0=A0=A0=A0=A0=A0 exit(1);=0A=A0=A0=A0=A0=A0=A0=A0 }= =0A=0A=0ANote, Thread_1var.Thread_1Stopped is set to 1 by the Thread_1 once= it receive the SIGUSR1.=0A=0AResult of two runs of the program is as follo= ws (values are in milliseconds):=0A./prog=0AB4 stop Thread_1. Threads: 3=0A= Thread_1 cought SIGUSR1=0AWait till Thread_1 stopped, Threads: 3=A0 Delay: = 50=0ACame out of Thread_1 loop, threadActivateDelay: 50, threadActivateTime= Out: 3000=0AB4 stop Thread_2. Threads: 3=0AThread_2 cought SIGUSR1=0ACame o= ut of Thread_2 loop, threadActivateDelay: 0, threadActivateTimeOut: 3000=0A= B4 stop Thread_3. Threads: 3=0AWait till Thread_3 stopped, Threads: 3=A0 De= lay: 50=0AWait till Thread_3 stopped, Threads: 3=A0 Delay: 100=0AWait till = Thread_3 stopped, Threads: 3=A0 Delay: 150=0A:=0A:=0AWait till Thread_3 sto= pped, Threads: 3=A0 Delay: 3000=0ACame out of Thread_3 loop, threadActivate= Delay: 3000, threadActivateTimeOut: 3000=0ATime out. Thread_3 may be dead!= =0A=0A=0A=0A./prog=0AB4 stop Thread_1. Threads: 3=0AWait till Thread_1 stop= ped, Threads: 3=A0 Delay: 50=0AWait till Thread_1 stopped, Threads: 3=A0 De= lay: 100=0AWait till Thread_1 stopped, Threads: 3=A0 Delay: 150=0A:=0A:=0AW= ait till Thread_1 stopped, Threads: 3=A0 Delay: 3000=0ACame out of Thread_1= loop, threadActivateDelay: 3000, threadActivateTimeOut: 3000=0ATime out. T= hread_1 may be dead!=0A=0A=0AI have tested this program on FreeBSD 8.1 and = 9.0 RC1, both i386. Different runs hang different threads. Also as I mentio= n earlier, rarely all three threads stop immediately.=0A=0AMy issue is quit= e similar to the problem: http://security.freebsd.org/advisories/FreeBSD-EN= -10:02.sched_ule.asc=0A=0ABut it doesn't freeze the system.=A0=0A=0AIncreas= e threadActivateTimeOut to 60000ms also doesn't work once hang.=0A=0APlease= also note, once receive a SIGUSR1, the thread wait on sigwait() till it re= ceive another signal.=0A=0A=0ASo what have I hit with? Is it a programming = error in my side or scheduling error or something else?=0A=0AAppreciate ver= y much if FreeBSD guys could help me to solve this issue.=0A=0AMany thanks = in advance.=0A=0ABest regards=0AUnga
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?1320847560.49916.YahooMailNeo>