hi-
I can't get apr_thread_timedwait work on Redhat 9.0.
Envoriment:
Redhat 9.0, glibc 2.3.2, apr source from 0.93 to the lastest snapshot of
9/14.
My source:
I inserted the following code into apr/test/testlock.c
/*sourc begin -----------------------------------------------------------*/
apr_thread_mutex_t *mymutex;
apr_thread_cond_t *mycond;
static void *APR_THREAD_FUNC mythd(apr_thread_t *thd, void *data)
{
apr_sleep(apr_time_from_sec(2));
apr_thread_mutex_lock(mymutex);
apr_thread_cond_signal(mycond);
apr_thread_mutex_unlock(mymutex);
return NULL;
}
void mytest(void)
{
apr_status_t rv;
apr_thread_t *thd;
apr_status_t s0;
rv = apr_thread_mutex_create(&mymutex, APR_THREAD_MUTEX_UNNESTED, p);
rv = apr_thread_cond_create(&mycond, p);
s0 = apr_thread_create(&thd, NULL, mythd, NULL, p);
apr_thread_mutex_lock(mymutex);
printf("Enter waiting...\n");
rv = apr_thread_cond_timedwait(mycond, mymutex, apr_time_from_sec(5));
//rv = apr_thread_cond_wait(mycond, mymutex);// This function works
if (rv == APR_SUCCESS)
printf("Waiting successfully!\n");
else
printf("Waiting timeout!\n");
apr_thread_mutex_unlock(mymutex);
}
/*sourc end -------------------------------------------------------*/
The above code was blocked at apr_thread_cond_timedwait.
Then I change all apr_thread_* to pthread_* just like the following:
/*sourc begin -----------------------------------------------------------*/
pthread_mutex_t mypmutex;
pthread_cond_t mypcond;
static void *APR_THREAD_FUNC mypthd(apr_thread_t *thd, void *data)
{
apr_sleep(apr_time_from_sec(2));
pthread_mutex_lock(&mypmutex);
pthread_cond_signal(&mypcond);
pthread_mutex_unlock(&mypmutex);
return NULL;
}
void myptest(void)
{
apr_thread_t *thd;
struct timespec abstime;
apr_time_t then;
pthread_mutexattr_t mattr;
apr_status_t rv;
then = apr_time_now() + apr_time_from_sec(5);
abstime.tv_sec = apr_time_sec(then);
abstime.tv_nsec = apr_time_usec(then) * 1000; /* nanoseconds */
pthread_mutexattr_init(&mattr);
pthread_mutex_init(&mypmutex, &mattr);
pthread_mutexattr_destroy(&mattr);
pthread_cond_init(&mypcond, NULL);
apr_thread_create(&thd, NULL, mypthd, NULL, p);
pthread_mutex_lock(&mypmutex);
printf("Enter waiting...\n");
rv = pthread_cond_timedwait(&mypcond, &mypmutex, &abstime);
//rv = apr_thread_cond_wait(mycond, mymutex);
if (rv == APR_SUCCESS)
printf("Waiting successfully!\n");
else
printf("Waiting timeout!\n");
pthread_mutex_unlock(&mypmutex);
}
/*sourc end -------------------------------------------------------*/
It worked! Can anyone tell me the reason? Thank you very much!
best regards,
James
_________________________________________________________________
与联机的朋友进行交流,请使用 MSN Messenger: http://messenger.msn.com/cn
|