/*
 * Uma thread pode corromper o conteúdo da pilha de outra threaad.
 */

#include<stdio.h>
#include <unistd.h>
#include <pthread.h>

int* v0;

int f(int param_f) {
  int v[2];
  v0 = v;
  sleep(2);
  return 0; 
} 

void* f_thread0(void *a) {
  printf("Thread 0.\n");
  f(0);
  return NULL;
}

void* f_thread1(void *a) {
  printf("Thread 1.\n");
  sleep(1);   /* Com grande probabilidade, thr0 já foi escalonada e 
                está dormindo. */
  v0[3] = 0;  /* Corrompe pilha da thread 0 */ 
  return NULL;
}

int main() { 
  pthread_t thr0, thr1; 
  
  pthread_create(&thr0, NULL, f_thread0, NULL);
  pthread_create(&thr1, NULL, f_thread1, NULL);

  pthread_join(thr0, NULL);
  pthread_join(thr1, NULL);

  return 0;
}