Delete pingpong.c

This commit is contained in:
VinhTA-Computer-Master 2021-09-08 13:04:29 -07:00 committed by GitHub
parent 8e38413977
commit 5d49811823
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 0 additions and 73 deletions

View File

@ -1,73 +0,0 @@
#include <pthread.h>
#include <stdio.h>
void ping(void);
void pong(void);
int loops=0;
int number=1;
int pingpong=0;
pthread_mutex_t mutex;
main()
{
pthread_t pinger, ponger;
pthread_mutex_init(&mutex, NULL);
pthread_create( &pinger, NULL,
(void*)&ping, NULL );
pthread_create( &ponger, NULL,
(void*)&pong, NULL );
pthread_join( pinger, NULL );
pthread_join( ponger, NULL );
printf("%d\n",loops);
}
void ping(void)
{
int i;
for (i=0; i<100; i++)
{
pthread_mutex_lock( &mutex );
if( pingpong )
{
printf("%d - %d - ping\n", number, i);
number ++;
pingpong = 0;
}
else
{
i--;
}
pthread_mutex_unlock( &mutex );
loops++;
}
pthread_exit(0);
}
void pong(void)
{
int i;
for (i=0; i<100; i++)
{
pthread_mutex_lock( &mutex );
if( !pingpong )
{
printf("%d - %d - pong\n", number, i);
number ++;
pingpong = 1;
}
else
{
i--;
}
pthread_mutex_unlock( &mutex );
loops++;
}
pthread_exit(0);
}