- 论坛徽章:
- 0
|
- #include"apue.h"
- #include<pthread.h>
- struct foo{
- int a,b,c,d;
- };
- struct foo *fp;
- void printfoo(const char *s,const struct foo *fp)
- {
- printf(s);
- printf("structure at 0x%x\n",(unsigned)fp);
- printf("foo.a=%d\n",fp->a);
- printf("foo.b=%d\n",fp->b);
- printf("foo.c=%d\n",fp->c);
- printf("foo.d=%d\n",fp->d);
- }
- void thr_fn1(void *arg)
- {
- struct foo foo={1,2,3,4};
- printfoo("thread 1:\n",&foo);
- pthread_exit((void *)&foo);
- }
- void thr_fn2(void *arg)
- {
- printf("thread 2:ID is %d\n",pthread_self());
- pthread_exit((void *)0);
- }
- int main(void)
- {
- int err;
- pthread_t tid1,tid2;
- err=pthread_create(&tid1,NULL,thr_fn1,NULL);
- if(err!=0)
- err_quit("can't create thread1: %s\n",strerror(err));
- err=pthread_join(tid1,(void *)&fp);
- if(err!=0)
- err_quit("can't join with thread 1: %s\n",strerror(err));
- sleep(1);
- printf("parent starting second thread \n");
- err=pthread_create(&tid2,NULL,thr_fn2,NULL);
- if(err!=0)
- err_quit("can't create thread 2:%s\n",strerror(err));
- sleep(1);
- printfoo("parent :\n",fp);
- }
复制代码 遇到的问题:- test11-3.c: In function ‘printfoo’:
- test11-3.c:9: warning: format not a string literal and no format arguments
- test11-3.c: In function ‘thr_fn2’:
- test11-3.c:25: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘pthread_t’
- test11-3.c: In function ‘main’:
- test11-3.c:32: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
- /usr/include/pthread.h:225: note: expected ‘void * (*)(void *)’ but argument is of type ‘void (*)(void *)’
- test11-3.c:40: warning: passing argument 3 of ‘pthread_create’ from incompatible pointer type
- /usr/include/pthread.h:225: note: expected ‘void * (*)(void *)’ but argument is of type ‘void (*)(void *)’
复制代码 |
|