Bug 14586

Summary: a problem with thread synchronization
Product: [Retired] Red Hat Linux Reporter: Need Real Name <niu_wqiang>
Component: gccAssignee: Jakub Jelinek <jakub>
Status: CLOSED WORKSFORME QA Contact:
Severity: medium Docs Contact:
Priority: high    
Version: 6.0   
Target Milestone: ---   
Target Release: ---   
Hardware: i386   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2002-12-15 00:24:21 UTC Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---

Description Need Real Name 2000-07-25 03:40:27 UTC
see this program:
when the pthread_cond_timedwait() in notify_f() return by timeout, the 
following notify pthread_cond_broadcast() can't awake the pthread_cond_wait
() in wait_f().
if call the notify_f() in a child thread just like the commented, not call 
it in the main process, then it will run rightly.
is this a bug? i wish to get help soon.

/*****************************************************/
#include <iostream.h>
#include <sys/time.h>
#include <unistd.h>
#include <pthread.h>

pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;

void* wait_f(void*);
void* notify_f(void*);

main()
{
	pthread_t thread1, thread2;

	pthread_create(&thread1, NULL, &wait_f, NULL);
         
         //
         // run notify_f() in a child thread
         //	
         //pthread_create(&thread2, NULL, &notify_f, NULL);

         //
         // run notify_f in the main process
         //
	notify_f(NULL);

	sleep(10);
	exit(0);
}

void* wait_f(void*)
{
	pthread_mutex_lock(&mutex);

	cout<<"1-1 sleeping "<<endl;
	pthread_cond_wait(&cond, &mutex);
	cout<<"1-1 waked up "<<endl;

	pthread_mutex_unlock(&mutex);
}

void* notify_f(void*)
{
	struct timeval now;
	struct timespec timeout;

        gettimeofday(&now, NULL);
        timeout.tv_sec = now.tv_sec + 3;
        timeout.tv_nsec = now.tv_usec * 1000;

        pthread_mutex_lock(&mutex);

        cout<<"2-1 sleeping "<<endl;
        pthread_cond_timedwait(&cond, &mutex, &timeout);
        cout<<"2-1 waked "<<endl;

	cout<<"2-2 notify "<<endl;
	pthread_cond_broadcast(&cond);
	pthread_mutex_unlock(&mutex);

	sleep(3);
}
/***********************************************************/