commit fca4d24417ad747871fcbb4d2f085e31d956a558 Author: Detlef Jenett Date: Thu Feb 26 16:53:12 2026 +0100 just testing diff --git a/condvars1.cpp b/condvars1.cpp new file mode 100644 index 0000000..a056d7b --- /dev/null +++ b/condvars1.cpp @@ -0,0 +1,55 @@ + +#include +#include +#include + +void* threadFunction1( void* ); +void* threadFunction2( void* ); + +int state = 0; +pthread_cond_t condvar; +pthread_mutex_t mutex; + + +int main() +{ + pthread_t t1, t2; + pthread_mutex_init( &mutex, 0 ); + pthread_cond_init( &condvar, 0 ); + pthread_create( &t1, 0, threadFunction1, 0 ); + pthread_create( &t2, 0, threadFunction2, 0 ); + + pthread_join( t1, 0 ); + pthread_join( t2, 0 ); + pthread_mutex_destroy( &mutex ); + pthread_cond_destroy( &condvar ); + return 0; +} + + + +void* threadFunction1( void* ) +{ + pthread_mutex_lock( &mutex ); + std::cout << "ding" << std::endl; + state = 1; + pthread_cond_signal( &condvar ); + pthread_mutex_unlock( &mutex ); + + return 0; +} + +void* threadFunction2( void* ) +{ + pthread_mutex_lock( &mutex ); + while( state != 1 ) + { + pthread_cond_wait( &condvar, &mutex ); + } + sleep( 1 ); + std::cout << "dong" << std::endl; + pthread_mutex_unlock( &mutex ); + + return 0; +} +