当我尝试实现多线程时如何删除这些烦人的警告 [英] how to remove these annoying warnings when I try to implement multithread

查看:64
本文介绍了当我尝试实现多线程时如何删除这些烦人的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码.

Hi, here is the code.

...
int x;  // assume this is the parameter of the function thread
pthread_t t1;
pthread_create(t1,NULL,thread,(void *)x);
...

void *thread(void *arg)
{
int x=(int)arg;
}


由于我需要将值传递给线程,
所以我该如何摆脱以下警告:
警告:从不同大小的整数强制转换为指针...


Since I need to pass value to the thread,
so how can I get rid of the following kind of warnings:
warning: cast to pointer from integer of different size .....

Thanks a bunch!

推荐答案

好吧,如果要构建64位Linux,则指针的大小(void *)大于指针的大小. int,所以您不能打字.您可以使用"long"代替"int".

关于"int"是在32位编译器上是32位还是在64位编译器上是64位,存在一些争论.它似乎取决于编译器.您应该检查一下它上的内容.

通常,

sizeof(int)= 32
sizeof(long)= 64
sizeof(void *)= 64(在64位编译器上,否则为32)
Well, if you''re building 64-bit Linux, the size of a pointer (void *) is bigger than the size of an int so you can''t typecast it. You could use "long" instead of "int".

There is some debate on if "int" is 32-bits on 32 bit compilers and 64-bits on 64 bit compilers. It seems to be compiler dependent. You should check what it is on yours.

Typically,

sizeof(int) = 32
sizeof(long) = 64
sizeof(void *) = 64 (on 64-bit compilers, otherwise 32)


您确定您不对我们的代码样本感到满意吗?我问是因为任何不错的C ++编译器都应该产生编译错误,而不是警告.您的函数应返回void *指针.它不能编译,否则就不能编译.

—SA
Are you sure you are not fulling us with this code sample? I''m asking because any decent C++ compiler should produce a compilation error, not a warning here. Your function should return void * pointer. It cannot compile is it does not.

—SA


这篇关于当我尝试实现多线程时如何删除这些烦人的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆