check if your locks is working your operating systems, POSIX
the below is to test if the lock functions on your operating system is working *nix (Linux,Unix,MacOS,...etc)
//implement the below functions
void lock(...){}
void unlock(...){}
void init_locks(...){}
void *test_sleep_th(void * param){
int id,sleep_for;
id=((test_sleep_t*) param)->id;
sleep_for=((test_sleep_t*) param)->sleep_for;
lock(reader_lock);
printf("thread %d will sleep for %d seconds\n",id,sleep_for);
sleep(sleep_for);
printf("thread %d wakedup\n",id);
unlock(reader_lock);
}
int main() {
test_sleep_t tst[5];
pthread_t *threads;
int i;
threads=malloc(sizeof(pthread_t[5]));
for(i=0;i<5;i++){
tst[i].id=i+1;
tst[i].sleep_for=i+1;
pthread_create(&threads[i],NULL,test_sleep_th,&tst[i]);
}
for (i = 0; i < 5; i++) {
pthread_join(threads[i], NULL);
}
return 0;
}
Comments
Post a Comment