Bug 14586 - a problem with thread synchronization
Summary: a problem with thread synchronization
Keywords:
Status: CLOSED WORKSFORME
Alias: None
Product: Red Hat Linux
Classification: Retired
Component: gcc
Version: 6.0
Hardware: i386
OS: Linux
high
medium
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact:
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2000-07-25 03:40 UTC by Need Real Name
Modified: 2008-05-01 15:37 UTC (History)
0 users

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 2002-12-15 00:24:21 UTC
Embargoed:


Attachments (Terms of Use)

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);
}
/***********************************************************/


Note You need to log in before you can comment on or make changes to this bug.