LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1010|回复: 10

创建线程问题

[复制链接]
发表于 2007-6-28 13:45:31 | 显示全部楼层 |阅读模式
void *thr_fn(void *arg)
{
        ...
}

int main(void)
{
        int err;

        err = pthread_create(&ntid, NULL, thr_fn, NULL);//创建线程
        if(err != 0)
                err_quit("can't create thread: %s\n", strerror(err));
        exit(0);
}
~

如果thr_fn里的参数有几个如:
void *thr_fn(int i, char *s)这样的该如何创建线程啊?
发表于 2007-6-28 16:48:46 | 显示全部楼层
你可以把多个参数放到结构里,把结构指针作为参数嘛
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-6-28 18:01:03 | 显示全部楼层
void *thr_fn(int i, char *s)
{
...
}

int main(void)
{
int err;
struct st
{
        int i;
        char *s;
}
struct st *stu;
stu->i = 5;
strcpy(stu->s, "lxy");
err = pthread_create(&ntid, NULL, thr_fn, stu);//创建线程
if(err != 0)
err_quit("can't create thread: %s\n", strerror(err));
exit(0);
}

是这样吗?怎么不行啊?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-6-28 18:11:18 | 显示全部楼层

  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <string.h>

  4. int a = 1;
  5. void *
  6. print(char *s, int i)
  7. {
  8.         printf("print(): s = %s\ni = %d\n", s, i);
  9. }

  10. int main()
  11. {
  12.         int err;
  13.         pthread_t tid1, tid2;
  14.         struct st
  15.         {
  16.                 char s[20];
  17.                 int i;
  18.         };
  19.         struct st aa;
  20.         strcpy(aa.s, "lixiaoya");
  21.         aa.i = 5;

  22.         err = pthread_create(&tid1, NULL, print, &aa);
  23.         if(err != 0)
  24.         {
  25.               printf("print() error\n");
  26.               return;
  27.         }

  28.         return 0;
  29. }
复制代码

test@szxdb:~/lxy/unix> gcc asd.c -lpthread
asd.c: In function `main':
asd.c:25: warning: passing arg 3 of `pthread_create' from incompatible pointer type
test@szxdb:~/lxy/unix> ./a.out
print(): s = lixiaoya
i = 1083538564

i为什么不是5呢?
回复 支持 反对

使用道具 举报

发表于 2007-6-28 18:49:29 | 显示全部楼层

  1. #include <stdio.h>
  2. #include<stdlib.h>
  3. #include <pthread.h>
  4. #include <string.h>

  5. struct st
  6. {
  7.       char s[20];
  8.       int i;
  9. };

  10. void *print(void *a)
  11. {
  12.         struct st* p = a;
  13.         printf("print(): s = %s\ni = %d\n",p->s, p->i);
  14. }

  15. int main()
  16. {
  17.         int err;
  18.         pthread_t tid1;

  19.         struct st aa;
  20.         strcpy(aa.s, "lixiaoya");
  21.         aa.i = 5;

  22.         err = pthread_create(&tid1, NULL, print, (void*)&aa);
  23.         if(err != 0)
  24.         {
  25.               printf("print() error\n");
  26.               exit(1);
  27.         }

  28.         pthread_join(tid1, NULL);
  29.         return 0;
  30. }


复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-6-28 19:01:39 | 显示全部楼层
为什么总是有“warning: passing arg 3 of `pthread_create' from incompatible pointer type”这个警告错误呢?
回复 支持 反对

使用道具 举报

发表于 2007-6-28 19:12:43 | 显示全部楼层
Post by lixiaoya
为什么总是有“warning: passing arg 3 of `pthread_create' from incompatible pointer type”这个警告错误呢?
这个警告是指函数传递参数的类型不匹配, 因为线程函数的参数是void* 这种指针类型.
但是我这个代码应该不会出现这个问题的.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-6-28 19:47:38 | 显示全部楼层
你那个程序在我的机子上编译也有这个警告错误。
回复 支持 反对

使用道具 举报

发表于 2007-6-28 20:40:05 | 显示全部楼层
scutan兄的代码在我这里也没有问题,楼主什么系统啊?要不截个图上来看看?
回复 支持 反对

使用道具 举报

发表于 2007-6-28 20:53:09 | 显示全部楼层
Post by lixiaoya
你那个程序在我的机子上编译也有这个警告错误。
我代码在贴上来之后过了几分钟又修改了一下, 你看看是不是还是弄的以前那个, 开始那个没有注意到void类型, 所以可能会有警告.
SORRY.
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表