1
0
mirror of https://github.com/ioacademy-jikim/debugging synced 2025-06-08 08:26:14 +00:00
2015-12-13 22:34:58 +09:00

51 lines
855 B
C

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
/* Delete memory that has a held lock and see what happens. */
typedef struct { int stuff[2000];
pthread_mutex_t lock; int morestuff[2000]; } XX;
void bar ( void );
void foo ( void );
int main ( void )
{
XX* xx = malloc(sizeof(XX));
assert(xx);
pthread_mutex_init( &xx->lock, NULL );
pthread_mutex_lock( &xx->lock );
free(xx);
bar();
foo();
bar();
return 0;
}
/* Try the same, on the stack */
void bar ( void )
{
pthread_mutex_t mx = PTHREAD_MUTEX_INITIALIZER;
// pthread_mutex_init( &mx, NULL );
pthread_mutex_lock( &mx );
/* now just abandon mx */
}
/* and again ... */
void foo ( void )
{
pthread_mutex_t mx;
pthread_mutex_init( &mx, NULL );
pthread_mutex_lock( &mx );
/* now just abandon mx */
}