#include #include #include #include #include "log.h" //variables for indexing of messages by logging functions int logindex=0; int *logi = &logindex; //thread mutex lock for access to the log pthread_mutex_t tlock = PTHREAD_MUTEX_INITIALIZER; void* doit(void*); char* p = 0; pthread_t tid1, tid2; // function main ------------------------------------------------- int main() { create_log("log.txt"); Msg("going to create the first thread"); pthread_create(&tid1,NULL,doit,NULL); Msg("going to create the second thread"); pthread_create(&tid2,NULL,doit,NULL); Msg("going to wait for the first thread to exit"); pthread_join(tid1,NULL); Msg("the first thread exited"); Msg("going to wait for the second thread to exit"); pthread_join(tid2,NULL); Msg("the second thread exited"); exit(0); }//end main // function doit -------------------------------------------------- void* doit(void* x) { pthread_t me; me = pthread_self(); Msg("I am thread %u (p=%u)",me,p); p = malloc(10); Msg("I am thread %u and I allocated segment %u",me,p); if (me==tid1) // allow thread B to do the sleep(2); // allocation and deallocation if (p) { free(p); Msg("I am thread %u and I deallocated segment %u", pthread_self(),p); p = NULL; } pthread_exit(NULL); return NULL; }//end doit