gpt4 book ai didi

c - gettimeofday 的奇怪行为

转载 作者:行者123 更新时间:2023-11-30 15:49:50 26 4
gpt4 key购买 nike

我想获取一个线程进入临界区和另一个线程获得进入 ARM CortexA8 上同一临界区的权限之间耗时。为此,我一直在使用 C 语言中的 gettimeofday() 函数。

   void *Thread1_Routine ();     //Create the Semaphore
void *Thread2_Routine (); //Wait for Thread1_Routine's Semaphore
void configured_malloc_behavior();
void Calc_Dif_Time (struct timeval *time1,struct timeval *time2);

//Definition to represent the time
typedef struct te_tim96
{
int64_t sec;
int32_t usec;
}te_tim96;
//Variables to save the time
struct timeval t1,t2;
//Variable to control the order enter Critical zone
char lock=0;
int count=0;


//Variable to make the create the 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 the 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 is lower than 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 the gpio63 as Output
do Stuff()

//Create the threads
pthread_create(&threadadd1,&attr1,&Thread1_Routine, NULL);
pthread_create(&threadadd2,&attr2,&Thread2_Routine, NULL);


//Wait to end the Threads ()
pthread_join(threadadd1, NULL);
pthread_join(threadadd2, NULL);
return 0;
}


//Thread Producer
void *Thread1_Routine (void)
{
//Variable to write in gpio/value
char value=1;
while (count<MAXCOUNT)
{
sleep (3);
pthread_mutex_lock(&mutex);
lock=lock+1; //Increment variable lock to indicate that the Thread Producer was done.
gettimeofday(&t1, NULL);
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}

//Thread Consumer
void *Thread2_Routine (void)
{
//Variable to write in gpio/value
char value=0;
while (count<MAXCOUNT)
{
//Wait for the semaphore is free!!!!!
while (lock=0);
pthread_mutex_lock(&mutex);
lock=lock-1; //Decrement variable lock to indicate that the Thread Producer was done.
gettimeofday(&t2, NULL);
Calc_Dif_Time(&t1, &t2); //Function to calculate the latency and plot it
pthread_mutex_unlock(&mutex);
count++; //To incremate the count of how many time goes the thread are 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 to miliseconds
Elapsedtime=(Tmeasure2.sec-Tmeasure1.sec)*1000;
//Part in usec to miliseconds
Elapsedtime+=(Tmeasure2.usec-Tmeasure1.usec)*0.001;

//Work with the rest of the division to convert usec to miliseconds
printf("Time to create the Semaphore[%lld.%6ld] Time to take the Semaphore[%lld.%6ld] Elapsed Time [%f ms]\n", Tmeasure1.sec, Tmeasure1.usec, Tmeasure2.sec, Tmeasure2.usec, Elapsedtime);
Elapsedtime=0; //Reset Elapsedtime to the next measure
}

程序没有错误,但我的问题是,当我执行它时,控制台显示以下结果:

./R_T_Measure4

Time to create the Semaphore[0. 0] Time to take the Semaphore[4878.153276] Elapsed Time [4878153.276000 ms]

Time to create the Semaphore[0. 0] Time to take the Semaphore[4878.153886] Elapsed Time [4878153.886000 ms]

这个结果显示了我如何可能认为 t1 变量没有正确指向或者它被重新启动。但我不知道我在这种情况下忽略了,因为 t2 效果很好。

如有任何帮助,我们将不胜感激

-问候

最佳答案

您的 while 循环不会等待锁释放。当您执行 while (lock=0); 时,总会返回 0,并且您将立即结束循环,并且它也会弄乱您的锁定,因为您正在设置您的 lock 变量。您应该使用 while (lock == 0);

关于c - gettimeofday 的奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16086301/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com