简体   繁体   English

在pthread_create()中,将int类型转换为void *是什么意思?

[英]What does int type convert to void* mean in pthread_create()?

What does such code mean: 这样的代码是什么意思:

int main() {
    typedef int UDTSOCKET;
    UDTSOCKET recver;
    pthread_create(&rsvthread, NULL, recvAndSend, (void *)(unsigned long)recver);
    return 0;
}

void* recvAndSend(void* usocket)
{
    UDTSOCKET recver = (UDTSOCKET)(unsigned long)usocket;
}

Such code is right? 这样的代码对吗? What does (void *)(unsigned long)recver) mean, how can unsigned int convert to void* , and how can void* convert to UDTSOCKET in (void *)(unsigned long)recver)是什么意思, unsigned int如何转换为void* ,以及void*如何转换为UDTSOCKET

UDTSOCKET recver = (UDTSOCKET)(unsigned long)usocket;

I think it should be 我认为应该

pthread_create(&rsvthread, NULL, recvAndSend, (void *)((unsigned long*)&recver));

and

void* recvAndSend(void* usocket)
{
    UDTSOCKET recver = (UDTSOCKET)(unsigned long)(*usocket);
}

Someone can explain it? 有人可以解释吗?

The first approach does pass by value, the second does pass by reference. 第一种方法确实按值传递,第二种方法确实按引用传递。

Note : The way how the data is passed to the thread function has an essential impact on how the variable can be used after the call to pthread_create() had returned. 注意 :将数据传递到线程函数的方式对返回pthread_create()调用后如何使用变量具有重要影响。

When passing by value the variable's value is copied to the stack during the call to pthread_create() , which has the effect, that the original variable could be used again immediately after the calls return. 按值传递时,变量的值调用pthread_create() 过程中被复制到堆栈 ,这具有如下效果:可以在调用返回后立即再次使用原始变量。

If passed by reference the value pointed to by reference isn't copied immediately, but at a later point in time, typically after pthread_create() had returned. 如果通过引用传递,引用指向的值不会立即复制,而是在稍后的时间复制,通常是 pthread_create()返回之后。 This dues to the asynchronous nature of the way the thread's function (as passed to pthread_create() ) is started, so the variable which's address had been passed in could not be reused immediately. 这归因于线程功能(传递给pthread_create() )的启动方式的异步特性,因此不能立即重用已传递地址的变量。 If it shall be reused, access to it needs to by synchronised. 如果要重用,则需要同步访问。


The first approach makes use of the fact that on some platforms the size of an integer is less or equal the size of a pointer. 第一种方法利用了以下事实:在某些平台上,整数的大小小于或等于指针的大小。

This might work or not. 这可能行不通。

If going this way, make sure to use an integer type which is guaranteed to be of the same size as pointer. 如果采用这种方式,请确保使用整数类型,该类型必须与指针的大小相同。 That is intptr_t for signed and uintptr_t for unsigned integers. intptr_t的签署和uintptr_t无符号整数。

To pass in the data: 传递数据:

#include <stdint.h> /* for intptr_t */

...

pthread_create(&rsvthread, NULL, recvAndSend, (void *)((intptr_t) recver));

To pull out the data: 提取数据:

void * recvAndSend(void * pvsocket)
{
  UDTSOCKET recver = (UDTSOCKET) ((intptr_t) pvsocket);
  ...

The second approach is the portable way to go, assuming we are doing C, do the following 第二种方法是可移植的方法,假设我们正在执行C,请执行以下操作

to pass in the data: 传递数据:

pthread_create(&rsvthread, NULL, recvAndSend, &recver);

to pull out the data: 提取数据:

void * recvAndSend(void * pvsocket)
{
  UDTSOCKET recver = *((UDTSOCKET *) pvsocket); /* First cast to pointer to UDTSOCKET, then dereference this pointer to read out what it ispointing to. */
  ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM