level 2
My_德意志
楼主
事情是这样的,我想做一个程序,定时关机,要用到一点多线程,但多线程困扰了我一天,实在解决不了,所以来提问。(这个程序还不完整)
现在的程序是正常的,但没有用到多线程。但是如果要用多线程的话,(用那两行注释,并且注释掉上面两行),local的值就不对了,输出的就像这样:
Local hour is: 1953719636:1550148719:1130117701
我的目的是要像这样:Local hour is: 20:41:11。
猜想应该是参数问题,但感觉注释部分跟没注释部分并没有什么区别。
希望知道问题在哪里,还有怎么改。
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
#include <string.h>
#include <pthread.h>
char *transition(struct tm *local);
void print(struct tm **local);
int main(void)
{
pthread_t t1;
struct tm *local;
char *ss=(char *)malloc(50*sizeof(char));
print(&local);
printf("Local hour is: %d:%d:%d\n",(local)->tm_hour,(local)->tm_min,(local)->tm_sec);
//pthread_create(&t1,NULL,print,&local);
//printf("Local hour is: %d:%d:%d\n",(local)->tm_hour,(local)->tm_min,(local)->tm_sec);
strcpy(ss,transition(local));
printf("%s",ss);
return 0;
}
void print(struct tm **local){
time_t t;
t=time(NULL);
*local=localtime(&t);
//printf("Local hour is: %d:%d:%d",(*local)->tm_hour,(*local)->tm_min,(*local)->tm_sec);
}
char *transition(struct tm *local){
//这个函数的功能是,根据local,算出现在的时间距离所输入时间的秒数,并生成字符串。
//比如现在是22:00.输入23:00,则返回字符串“shutdown -s -t 3600”。
int h,m,s;//hours,mins,secs
long ls=0;//剩余秒数
scanf("%d:%d:%d",&h,&m,&s);//需要判断合法性
ls = (h-local->tm_hour)*3600 + (m-local->tm_min)*60 + (s-local->tm_sec);
char *s1=(char *)malloc(20*sizeof(char)),*s2=(char *)malloc(40*sizeof(char));
int i=0,k=0;
for(;ls!=0;i++){
s1[i]=ls%10+48;
ls/=10;
}
s1[i]='\0';
for(--i;i>=0;i--){
s2[k++]=s1[i];
}
s2[k]='\0';
char s3[50]="shutdown -s -t ";
strcat(s3,s2);
return s3;
}
2015年05月20日 12点05分
1
现在的程序是正常的,但没有用到多线程。但是如果要用多线程的话,(用那两行注释,并且注释掉上面两行),local的值就不对了,输出的就像这样:
Local hour is: 1953719636:1550148719:1130117701
我的目的是要像这样:Local hour is: 20:41:11。
猜想应该是参数问题,但感觉注释部分跟没注释部分并没有什么区别。
希望知道问题在哪里,还有怎么改。
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
#include <string.h>
#include <pthread.h>
char *transition(struct tm *local);
void print(struct tm **local);
int main(void)
{
pthread_t t1;
struct tm *local;
char *ss=(char *)malloc(50*sizeof(char));
print(&local);
printf("Local hour is: %d:%d:%d\n",(local)->tm_hour,(local)->tm_min,(local)->tm_sec);
//pthread_create(&t1,NULL,print,&local);
//printf("Local hour is: %d:%d:%d\n",(local)->tm_hour,(local)->tm_min,(local)->tm_sec);
strcpy(ss,transition(local));
printf("%s",ss);
return 0;
}
void print(struct tm **local){
time_t t;
t=time(NULL);
*local=localtime(&t);
//printf("Local hour is: %d:%d:%d",(*local)->tm_hour,(*local)->tm_min,(*local)->tm_sec);
}
char *transition(struct tm *local){
//这个函数的功能是,根据local,算出现在的时间距离所输入时间的秒数,并生成字符串。
//比如现在是22:00.输入23:00,则返回字符串“shutdown -s -t 3600”。
int h,m,s;//hours,mins,secs
long ls=0;//剩余秒数
scanf("%d:%d:%d",&h,&m,&s);//需要判断合法性
ls = (h-local->tm_hour)*3600 + (m-local->tm_min)*60 + (s-local->tm_sec);
char *s1=(char *)malloc(20*sizeof(char)),*s2=(char *)malloc(40*sizeof(char));
int i=0,k=0;
for(;ls!=0;i++){
s1[i]=ls%10+48;
ls/=10;
}
s1[i]='\0';
for(--i;i>=0;i--){
s2[k++]=s1[i];
}
s2[k]='\0';
char s3[50]="shutdown -s -t ";
strcat(s3,s2);
return s3;
}