apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Wong James" <jameswang0...@hotmail.com>
Subject apr_thread_cond_timedwait() caused deadlock on Linux
Date Sat, 10 May 2003 03:26:25 GMT
<html><div style='background-color:'><DIV>
<DIV>
<DIV><FONT size=2>hi-,all</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>&nbsp;&nbsp;I wrote a program on redhat 9.0, &nbsp;(kernel
version: 2.4.20-9), but the apr_thread_cond_timedwait() caused deadlock, while apr_thread_cond_wait()
worked. And when&nbsp;I&nbsp;compiled this program on Win32 platfrom, it worked properly.&nbsp;Then
I rewrote all apr_thread_xxxs with&nbsp;native pthread_xxxs on Linux, the pthread_cond_timedwait
worked, too.</FONT></DIV>
<DIV><FONT size=2>Why? Can you tell me the reason?&nbsp; The APR lib I used
is the one shipped with Apache 2.0.45.</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>thks,</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>James</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>/*------------------------source begin ----------------------------------------*/</FONT></DIV>
<DIV><FONT size=2>typedef struct cond_t cond_t;<BR>struct cond_t {<BR>&nbsp;apr_thread_mutex_t
*mutex;<BR>&nbsp;apr_thread_cond_t *cond;<BR>};</FONT></DIV><FONT
size=2>
<DIV><BR></FONT><FONT size=2>static void * APR_THREAD_FUNC test_thread(apr_thread_t
*thd,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
void *data)<BR>{<BR>&nbsp;cond_t *tcond = data;<BR></FONT></DIV>
<DIV><FONT size=2>&nbsp;apr_sleep(apr_time_from_sec(2));<BR>&nbsp;apr_thread_mutex_lock(tcond-&gt;mutex);<BR>&nbsp;printf("signal
start...\n");<BR>&nbsp;apr_thread_cond_signal(tcond-&gt;cond);<BR>&nbsp;printf("signal
done!\n");<BR>&nbsp;apr_thread_mutex_unlock(tcond-&gt;mutex);<BR>&nbsp;return
NULL;<BR>&nbsp;<BR>}</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>static void test_cond(void)<BR>{<BR>&nbsp;apr_thread_t
*thd;<BR>&nbsp;apr_interval_time_t timeout = apr_time_from_sec(5);<BR>&nbsp;apr_status_t
rv;</FONT></DIV>
<DIV><FONT size=2>&nbsp;cond_t *tcond = apr_pcalloc(p, sizeof(cond_t));</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>&nbsp;apr_thread_mutex_create(&amp;tcond-&gt;mutex,
APR_THREAD_MUTEX_DEFAULT, p);<BR>&nbsp;apr_thread_cond_create(&amp;tcond-&gt;cond,
p);<BR></FONT><FONT size=2></FONT></DIV>
<DIV><FONT size=2>&nbsp;apr_thread_create(&amp;thd, NULL, test_thread,
tcond, p);</FONT></DIV>
<DIV><FONT size=2></FONT>&nbsp;</DIV>
<DIV><FONT size=2>&nbsp;apr_thread_mutex_lock(tcond-&gt;mutex);<BR>&nbsp;printf("start
waiting...\n");</FONT></DIV>
<DIV><FONT size=2><BR>&nbsp;<U><FONT size=3><STRONG><FONT
color=#ff0000>rv = apr_thread_cond_timedwait(tcond-&gt;cond, tcond-&gt;mutex, timeout);//
this line causes deadlock</FONT></STRONG><BR></FONT></U>&nbsp;<STRONG><FONT
color=#008000>//rv = pthread_cond_wait(tcond-&gt;cond, tcond-&gt;mutex);//this
line works<BR></FONT></STRONG>&nbsp;if (rv == APR_SUCCESS)</DIV>
<DIV>&nbsp;&nbsp; &nbsp;printf("waiting ok\n");<BR>&nbsp;else
</DIV>
<DIV>&nbsp;&nbsp;&nbsp; printf("waiting timeout/failure!\n");</DIV>
<DIV><BR>&nbsp;apr_thread_mutex_unlock(tcond-&gt;mutex);</DIV>
<DIV>&nbsp;</DIV>
<DIV>}</DIV>
<DIV>&nbsp;</DIV>
<DIV>
<DIV><FONT size=2>/*------------------------source&nbsp;end ----------------------------------------*/</FONT></DIV></FONT></DIV></DIV></DIV></div><br
clear=all><hr>使用世界上最大的电子邮件系统―   <a href="http://g.msn.com/8HMVCNCN/2752??PS=">MSN
Hotmail </a> Get 2 months FREE*. </html>

Mime
View raw message