hi-,all
I wrote a program on redhat 9.0, (kernel version: 2.4.20-9), but the apr_thread_cond_timedwait() caused deadlock, while apr_thread_cond_wait() worked. And when I compiled this program on Win32 platfrom, it worked properly. Then I rewrote all apr_thread_xxxs with native pthread_xxxs on Linux, the pthread_cond_timedwait worked, too.
Why? Can you tell me the reason? The APR lib I used is the one shipped with Apache 2.0.45.
thks,
James
/*------------------------source begin ----------------------------------------*/
typedef struct cond_t cond_t;
struct cond_t {
apr_thread_mutex_t *mutex;
apr_thread_cond_t *cond;
};
struct cond_t {
apr_thread_mutex_t *mutex;
apr_thread_cond_t *cond;
};
static void * APR_THREAD_FUNC test_thread(apr_thread_t *thd,
void *data)
{
cond_t *tcond = data;
apr_sleep(apr_time_from_sec(2));
apr_thread_mutex_lock(tcond->mutex);
printf("signal start...\n");
apr_thread_cond_signal(tcond->cond);
printf("signal done!\n");
apr_thread_mutex_unlock(tcond->mutex);
return NULL;
}
apr_thread_mutex_lock(tcond->mutex);
printf("signal start...\n");
apr_thread_cond_signal(tcond->cond);
printf("signal done!\n");
apr_thread_mutex_unlock(tcond->mutex);
return NULL;
}
static void test_cond(void)
{
apr_thread_t *thd;
apr_interval_time_t timeout = apr_time_from_sec(5);
apr_status_t rv;
{
apr_thread_t *thd;
apr_interval_time_t timeout = apr_time_from_sec(5);
apr_status_t rv;
cond_t *tcond = apr_pcalloc(p, sizeof(cond_t));
apr_thread_mutex_create(&tcond->mutex, APR_THREAD_MUTEX_DEFAULT, p);
apr_thread_cond_create(&tcond->cond, p);
apr_thread_cond_create(&tcond->cond, p);
apr_thread_create(&thd, NULL, test_thread, tcond, p);
apr_thread_mutex_lock(tcond->mutex);
printf("start waiting...\n");
printf("start waiting...\n");
rv = apr_thread_cond_timedwait(tcond->cond, tcond->mutex, timeout);// this line causes deadlock
//rv = pthread_cond_wait(tcond->cond, tcond->mutex);//this line works
if (rv == APR_SUCCESS)
printf("waiting ok\n");
else
else
printf("waiting timeout/failure!\n");
apr_thread_mutex_unlock(tcond->mutex);
}
/*------------------------source end ----------------------------------------*/
使用世界上最大的电子邮件系统― MSN Hotmail Get 2 months FREE*.
