Bug 14586 - a problem with thread synchronization
a problem with thread synchronization
Status: CLOSED WORKSFORME
Product: Red Hat Linux
Classification: Retired
Component: gcc (Show other bugs)
6.0
i386 Linux
high Severity medium
: ---
: ---
Assigned To: Jakub Jelinek
:
Depends On:
Blocks:
  Show dependency treegraph
 
Reported: 2000-07-24 23:40 EDT by Need Real Name
Modified: 2008-05-01 11:37 EDT (History)
0 users

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Environment:
Last Closed: 2002-12-14 19:24:21 EST
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
CRM:
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---


Attachments (Terms of Use)

  None (edit)
Description Need Real Name 2000-07-24 23:40:27 EDT
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.