23,110
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
#include<pthread.h>
#include<unistd.h>
#include<errno.h>
#define TIMEGAP 1
void *fun_1()
{
printf("tid = %d\n",pthread_self());
clock_t start,end2;
start=clock(); //线程开始
double duration ;
while(1)
{
duration=(double)(end2-start)/CLOCKS_PER_SEC;
if(duration>1)
{
printf("time arrived!\n");
break;
}
end2=clock(); //结束时间
printf("end2=%d \n",end2);
usleep(1000);
}
}
int main()
{
clock_t start,end1;
double duration;
long i = 10000000L;
pthread_t tid;
int ret=pthread_create(&tid,NULL,fun_1,NULL);
if(!ret)
perror("pthread_create");
start=clock(); //主线程中测量执行时间,
while(i--);
end1=clock();
printf("end1=%d \n",end1);
duration=(double)(end1-start)/CLOCKS_PER_SEC;
printf("%f ",duration);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include<time.h>
#include<pthread.h>
#include<unistd.h>
#include<errno.h>
#define TIMEGAP 1
clock_t start,now;
void *fun_1()
{
printf("tid = %u\n",pthread_self());
double duration ;
while(1)
{ now=clock();
duration=(double)(now-start)/CLOCKS_PER_SEC;
if(duration>1)
{
printf("time arrived!\n");
start=clock();
break;
}
printf("now=%d \n",now);
usleep(1000);
}
}
int main()
{
//clock_t start,end1;
// double duration;
// long i = 10000000L;
pthread_t tid;
int ret=pthread_create(&tid,NULL,fun_1,NULL);
if(!ret)
perror("pthread_create");
start=clock();
// start=clock();
// while(i--);
// end1=clock();
//printf("end1=%d \n",end1);
// duration=(double)(end1-start)/CLOCKS_PER_SEC;
// printf("%f ",duration);
while(1); //
return 0;
}
呵呵,自己搞定了。。
原来自己忘了在main里加while循环,让进程不结束,就可以了。
其实我想实现sleep的功能在这里,
呵呵,结贴了。。
不过#2楼的,提醒了我.......! printf("time arrived!\n");
printf("end2=%d \n",end2);
我其实的想法是向串口发命令--逻辑是:先发一个命令下去(write)->串口回数据(read)->开始解析数据。
我的用意是在:串口回数据(read)->开始解析数据,测量这段时间里总共执行的时间,然后来控制每次该什么时候write