just testing

This commit is contained in:
2026-02-26 16:53:12 +01:00
commit fca4d24417

55
condvars1.cpp Normal file
View File

@@ -0,0 +1,55 @@
#include <pthread.h>
#include <unistd.h>
#include <iostream>
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;
}