本文介绍了有人可以解释为什么输出只打印4次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在编写一个代码来创建posix线程print函数是打印p,q用于4个循环,但它不是为第五个循环打印而是调用循环。有人可以解释原因。 #include< stdio.h> #include< pthread.h> #define max 5 struct data { int a; int b; }; 结构数据s [max]; void * print(void * t) { int p, q; struct data * thread; thread =(struct data *)t; p = thread-> a; q = thread-> b; printf("%d%d \ n",p,q); } int main() { pthread_t th [max]; char m [100]; int rc,x = 45,y = 56,i; for(i = 0; i< max; i ++) { scanf("%s",m); s [i] .a = x; s [i] .b = y; //printf("%d",s[i].a); rc = pthread_create(& th [i],NULL,print,(void *) & s [i]); if(rc) printf(" error:%d",rc); } } 解决方案 如果你使用=< ,它包括5I was writing a code to create posix threads print function is printing p,q for 4 loops but it is not printing for fifth loop but the loop is called. Can someone explain the reason.#include<stdio.h>#include<pthread.h>#define max 5struct data{int a;int b;};struct data s[max];void *print(void *t){int p,q;struct data *thread;thread=(struct data *)t;p=thread->a;q=thread->b;printf("%d %d\n",p,q);}int main(){pthread_t th[max];char m[100];int rc,x=45,y=56,i;for(i=0;i<max;i++){scanf("%s",m);s[i].a=x;s[i].b=y;//printf("%d",s[i].a);rc=pthread_create(&th[i],NULL,print,(void *)&s[i]);if(rc)printf("error:%d",rc);}} 解决方案 这篇关于有人可以解释为什么输出只打印4次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-26 07:47
查看更多