c - Weird behavior of gettimeofday -


i obtain elapsed time between 1 thread enters critical zone , other takes permision enter in same critical zone on arm cortexa8. have been using function gettimeofday() in c.

   void *thread1_routine ();     //create semaphore void *thread2_routine (); //wait thread1_routine's semaphore void configured_malloc_behavior(); void calc_dif_time (struct timeval *time1,struct timeval *time2);  //definition represent time typedef struct te_tim96 {     int64_t sec;     int32_t usec; }te_tim96; //variables save time struct timeval t1,t2; //variable control order enter critical zone char lock=0; int count=0;   //variable make create mutex pthread_mutex_t mutex;  int main (void) {     //variables define threads     pthread_t threadadd1, threadadd2;     pthread_attr_t attr1, attr2;     struct sched_param p1, p2;      //configured malloc behavior     configured_malloc_behavior();      //init thread     pthread_mutex_init(&mutex, null);      //define threads     pthread_attr_init(&attr1);     pthread_attr_init(&attr2);     //thread1     pthread_attr_setschedpolicy(&attr1, sched_fifo);     p1.sched_priority= 98; //this lower thread2     pthread_attr_setschedparam(&attr1, &p1);     //thread2     pthread_attr_setschedpolicy(&attr2, sched_fifo);     p2.sched_priority= 99;     pthread_attr_setschedparam(&attr2, &p2); //end define threads   //init gpio63 output     stuff()    //create threads                                                 pthread_create(&threadadd1,&attr1,&thread1_routine, null);     pthread_create(&threadadd2,&attr2,&thread2_routine, null);   //wait end threads ()     pthread_join(threadadd1, null);     pthread_join(threadadd2, null);     return 0; }   //thread producer void *thread1_routine (void) { //variable write in gpio/value char value=1;     while (count<maxcount)     {         sleep (3);         pthread_mutex_lock(&mutex);         lock=lock+1;    //increment variable lock indicate thread producer done.         gettimeofday(&t1, null);         pthread_mutex_unlock(&mutex);     }     pthread_exit(null); }  //thread consumer void *thread2_routine (void) { //variable write in gpio/value char value=0;     while (count<maxcount)     {     //wait semaphore free!!!!!         while (lock=0);         pthread_mutex_lock(&mutex);         lock=lock-1;    //decrement variable lock indicate thread producer done.         gettimeofday(&t2, null);         calc_dif_time(&t1, &t2); //function calculate latency , plot         pthread_mutex_unlock(&mutex);         count++; //to incremate count of how many time goes thread made     }     pthread_exit(null); }  void calc_dif_time (struct timeval *time1,struct timeval *time2) { struct te_tim96 tmeasure1, tmeasure2; double elapsedtime; //tmeasurey=ty tmeasure1.sec=(*time1).tv_sec; tmeasure1.usec=(*time1).tv_usec; tmeasure2.sec=(*time2).tv_sec; tmeasure2.usec=(*time2).tv_usec;  //calculate     //part in sec miliseconds     elapsedtime=(tmeasure2.sec-tmeasure1.sec)*1000;     //part in usec miliseconds     elapsedtime+=(tmeasure2.usec-tmeasure1.usec)*0.001;      //work rest of division convert usec miliseconds     printf("time create semaphore[%lld.%6ld] time take semaphore[%lld.%6ld] elapsed time [%f ms]\n", tmeasure1.sec, tmeasure1.usec, tmeasure2.sec, tmeasure2.usec, elapsedtime);     elapsedtime=0; //reset elapsedtime next measure     } 

the program without error, problem, when execute it, console show following result:

./r_t_measure4

time create semaphore[0. 0] time take semaphore[4878.153276] elapsed time [4878153.276000 ms]

time create semaphore[0. 0] time take semaphore[4878.153886] elapsed time [4878153.886000 ms]

this result shows how might t1 variable not pointed or restarted. don't know overlooking in case because t2 works well.

any grateful

-regards

your while loop isn't waiting lock free. when while (lock=0);, return 0, , end loop immediately, , messing locking, because setting lock variable. should use while (lock == 0);


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -