如何将整数转换为void指针?

Din*_*esh 21 c casting void-pointers

在使用C语言中的Threads时,我正面临警告

"警告:从不同大小的整数转换为指针"

代码如下

#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
#include<pthread.h>
void *print(void *id)
{
 int a=10;
 printf("My thread id is %ld\n",pthread_self());
 printf("Thread %d is executing\n",id);
 return (void *) 42;
}

int main()
{
 pthread_t th[5];
 int t;
 int i;
 int status;
 void *ret;
 for(i=0;i<5;i++)
 {
   status=pthread_create(&th[i],NULL,print,(void *)i); //Getting warning at this line
   if(status)
   {
    printf("Error creating threads\n");
    exit(0);
   }
   pthread_join(th[i],&ret);
   printf("--->%d\n",(int *)ret);
 }
 pthread_exit(NULL);
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以解释如何将一个整数传递给接收(void*)作为参数的函数吗?

Die*_*Epp 36

这是将整数传递给新pthread的好方法,如果这是你需要的.你只需要压制警告,这样就可以了:

#include <stdint.h>

void *threadfunc(void *param)
{
    int id = (intptr_t) param;
    ...
}

int i, r;
r = pthread_create(&thread, NULL, threadfunc, (void *) (intptr_t) i);
Run Code Online (Sandbox Code Playgroud)

讨论

这可能会冒犯你的感情,但它很短,没有竞争条件(如果你使用的话,就像你一样&i).编写几十行额外代码只是为了得到一堆编号的线程没有任何意义.

数据竞赛

这是一个带有数据竞争的糟糕版本:

#include <pthread.h>
#include <stdio.h>

#define N 10

void *thread_func(void *arg)
{
    int *ptr = arg;
    // Has *ptr changed by the time we get here?  Maybe!
    printf("Arg = %d\n", *ptr);
    return NULL;
}

int main()
{
    int i;
    pthread_t threads[N];
    for (i = 0; i < N; i++) {
        // NO NO NO NO this is bad!
        pthread_create(&threads[i], NULL, thread_func, &i);
    }
    for (i = 0; i < N; i++) {
        pthread_join(threads[i], NULL);
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

现在,当我用线程消毒剂运行它时会发生什么?

(另外,看看它如何打印"5"两次...)

==================
WARNING: ThreadSanitizer: data race (pid=20494)
  Read of size 4 at 0x7ffc95a834ec by thread T1:
    #0 thread_func /home/depp/test.c:9 (a.out+0x000000000a8c)
    #1 <null> <null> (libtsan.so.0+0x000000023519)

  Previous write of size 4 at 0x7ffc95a834ec by main thread:
    #0 main /home/depp/test.c:17 (a.out+0x000000000b3a)

  Location is stack of main thread.

  Thread T1 (tid=20496, running) created by main thread at:
    #0 pthread_create <null> (libtsan.so.0+0x0000000273d4)
    #1 main /home/depp/test.c:18 (a.out+0x000000000b1c)

SUMMARY: ThreadSanitizer: data race /home/depp/test.c:9 thread_func
==================
Arg = 1
Arg = 2
Arg = 3
Arg = 4
Arg = 5
Arg = 6
Arg = 7
Arg = 8
Arg = 9
Arg = 5
ThreadSanitizer: reported 1 warnings

  • @jackdoe:编写"你将来可能需要的"代码是浪费人的生命. (8认同)
  • @Xax:首先,`(void*)`是多余的,只需使用`&i`.此外,编译器不会就竞争条件向您发出警告,您必须自己解决这些问题(或使用特殊工具).出现竞争条件的原因是你必须等到每个线程都读完`i`才能改变`i`或让它离开范围.这是一个可能出错的例子:https://gist.github.com/depp/241d6f839b799042c409 (3认同)
  • @DietrichEpp你可以在`pthread_create()`中使用`(void*)&i`解释什么是竞争条件,我正在使用它并且编译器没有显示警告,我在想但是无法弄明白,我也是将传递给线程的值转换为这样的`int*b =(int*)a`请看[this](https://idone.com/MHzoln) (2认同)