This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

1.1.3 and upwards: apparent bug with pthread_cond_wait() and/or signal()


Hi all, I've just been wrestling with some code I've been writing, trying to 
get pthreads condition variables to work under Cygwin on Windows 2000. I've 
tried DLL versions 1.1.3 and the 20020409 snapshot, and neither are working 
for me, so I'm assuming that no versions in between will work either...

When I build an run my test program (attached below) I get the following 
results...

$ ./a.exe
-- test thread has signal()ed
-- test thread about to wait()...
-- main thread wakes!
-- main thread about to signal()
-- main thread waiting for exit...

and the program hangs. Presumably the test thread does not wake. On the other 
hand, when I compile the same test program on SuSE Linux 7.2 (gcc 2.95.3, 
glibc 2.2.2) I get what I consider to be correct results...

michaelb@gilgamesh:~ > ./a.out
-- test thread has signal()ed
-- test thread about to wait()...
-- main thread wakes!
-- main thread about to signal()
-- main thread waiting for exit...
-- test thread wakes!
-- test thread about to exit...
%% PASSED

I've done a lot of staring at my test program, and can't see any problem with 
it (though I'm willing to be proved wrong in this, I can stand the shame!), 
so I'm thinking that this is a Cygwin bug.

Regards
M.Beach

/*
 * foo.cpp
 */

#include <pthread.h>

#include <iostream>

using namespace std;


void *condVarTestThreadEntry(void *arg);

struct CondVarTestData
{
    pthread_mutex_t m;
    pthread_cond_t cv;
    enum { START, NEW_THREAD_RUNNING, NEW_THREAD_ACKNOWLEDGED } state;
};


int main(int argc, char *argv[])
{
    CondVarTestData td;
    pthread_mutex_init(&td.m, 0);
    pthread_cond_init(&td.cv, 0);
    td.state = CondVarTestData::START;
    pthread_t th;
    pthread_create(&th, 0, condVarTestThreadEntry, &td);
    {
	pthread_mutex_lock(&td.m);
	while (td.state != CondVarTestData::NEW_THREAD_RUNNING)
	{
	    pthread_cond_wait(&td.cv, &td.m);
	    clog << "-- main thread wakes!" << endl;
	}
	td.state = CondVarTestData::NEW_THREAD_ACKNOWLEDGED;
	clog << "-- main thread about to signal()" << endl;
	pthread_cond_signal(&td.cv);
	pthread_mutex_unlock(&td.m);
    }
    clog << "-- main thread waiting for exit..." << endl;
    pthread_join(th, 0);
    cout << "%% PASSED" << endl;

    return 0;
}


void *condVarTestThreadEntry(void *arg)
{
    CondVarTestData *td = (CondVarTestData *)arg;
    pthread_mutex_lock(&td->m);
    td->state = CondVarTestData::NEW_THREAD_RUNNING;
    pthread_cond_signal(&td->cv);
    clog << "-- test thread has signal()ed" << endl;
    while (td->state != CondVarTestData::NEW_THREAD_ACKNOWLEDGED)
    {
	clog << "-- test thread about to wait()..." << endl;
        pthread_cond_wait(&td->cv, &td->m);
	clog << "-- test thread wakes!" << endl;
    }
    pthread_mutex_unlock(&td->m);
    clog << "-- test thread about to exit..." << endl;
    return 0;
}
0d03a9@INTHN

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]