从不同大小的整数转换为指针 -wint-to-pointer-cast

Emi*_*ega 1 c semaphore

您好,我的代码中有下一个问题:

“从不同大小的整数转换为指针-wint-to-pointer-cast”

这个问题在这行代码中

pthread_create(&filos[i], NULL, (void *)filosofos,(void *) i);
Run Code Online (Sandbox Code Playgroud)

特别是在(void *) i

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

#define N 5               //num. de filosofos
#define IZQ (i-1)%N      //vecino izquierdo de i
#define DER (i+1)%N      //vecino derecho de i
#define PENSANDO 0
#define CON_HAMBRE 1
#define COME 2


pthread_t filos[N];       //hilos que representan a los filósofos
sem_t mutex ;             //semáforo para la sección crítica
sem_t s[N];               //semáforos para los filósofos
int estado [N] ;          //estado actual de cada filósosfo


/* 
    el filosofo i va a perder el tiempo... (va a pensar)
 */
void pensar (int i) 
{
  int t ;
  t = rand() % 11;
  printf("Filosofo %d pensando \n", i) ;
  estado[i] = PENSANDO;
  sleep (t) ;
}


/* 
    El filosofo i, va a comer !!!!!!!!
 */
void comer (int i) 
{
  printf("Filósofo %d esta comiendo un caballo \n", i);
  estado[i] = COME;
  sleep (5);
}


/* 
    Verifica que pueda tomar ambos tenedores
*/
void verifica(int i) 
{
  if( estado[i]==CON_HAMBRE && estado[IZQ]!=COME && estado[DER]!=COME ){
    estado[i] = COME;
    printf("Filósofo %d comiendo\n", i) ;
    sem_post(&s[i]);
  }
}


/* 
    El filosofo i intenta tomar los  tenedores
 */
void toma_tndrs(int i)
{

  sem_wait(&mutex);             //entra a la sección crítica, hace uso del semaforo
  estado[i] = CON_HAMBRE;      //dice: tengo mucha hambre!!!!!!!!!!
  verifica(i);                 // verifica que pueda tomar los tenedores
  sem_post(&mutex);            //sale de la sección crítica y el sem. puede permitir la entrada a alguien más
  sem_wait(&s[i]);            //se bloquea si no consiguió los tenedores
}


/* 
    el filosofo i dejará los tenedores
 */
void deja_tndrs(int i)
{

  sem_wait(&mutex);       // de nuevo entra a la sección critica
  estado[i] = PENSANDO;  //deja de comer y se pone a pensar
  verifica(IZQ);        
  verifica(DER);
  sem_post(&mutex);
}


/* 


 */
void * filosofos (int i)
{
  int j ;


  for (; ; )
    {
      pensar(i) ;
      toma_tndrs(i) ;
      comer(i) ;
      deja_tndrs(i) ;
    }
}


main()
{
  int i ;


  for(i = 0; i < 5; i++){
    sem_init (&s[i], 0, 1);


    estado[i] = PENSANDO ;
  }


  sem_init (&mutex, 0, 1);

  //creamos un hilo de ejecucion para cada filosofo, que ejecuta filosofos()
  for (i=0; i<N; i++)
    pthread_create(&filos[i], NULL, (void *)filosofos,(void *) i);

  //cada hilo espera a que terminen los demás y libera los recursos
  for (i=0; i<N; i++){
    pthread_join(filos[i],NULL);
  }

}
Run Code Online (Sandbox Code Playgroud)

Som*_*ude 5

假设它i是导致问题的变量的强制转换,首先将其强制转换为大于int且同时足够容纳指针的整数类型。

一种这样的类型,专为足够大以容纳任何整数或指针而设计,intptr_t例如在此固定宽度整数参考中描述

演员阵容看起来像

(void *) (intptr_t) i
Run Code Online (Sandbox Code Playgroud)

在线程函数中,您执行相反的操作,首先转换为 tointptr_t然后 to int

void * filosofos (void *p)
{
    int i = (int) (intptr_t) p;

    ...

    return NULL;
}
Run Code Online (Sandbox Code Playgroud)

请注意,我将线程函数的签名更改为正确的,它需要一个void *参数,这在 where 的系统上产生了很大的不同sizeof(int) != sizeof(void *),这在您的情况下似乎是正确的。另请注意,我NULL在函数末尾返回 a 。那是因为线程函数被声明(并指定)为返回一个指针。不从声明为这样做的函数返回值会导致未定义的行为


对于那些好奇的人,虽然我通常不建议将整数转换为这样的指针,但任何事情都有例外,这是可以接受这种转换的少数情况之一(如果不是唯一的)。

许多人仍然看不起这样的解决方案,但仍然使用它,因为它比原本需要的整个malloc复制free马戏团更容易。

对值使用动态分配内存的解决方案如下所示:

  • 线程创建

    // Allocate memory for the index
    int *p = malloc(sizeof i);
    if (p == NULL)
    {
        // TOOD: Handle error! (Left as an exercise to the reader)
    }
    
    // Copy the value
    *p = i;
    
    // And create the thread
    pthread_create(&filos[i], NULL, &filosofos, p);  // Note: no cast needed
    
    Run Code Online (Sandbox Code Playgroud)
  • 在线程函数中

    void * filosofos (void *p)
    {
        int i = *(int *) p;
        free(p);
    
        ...
    
        return NULL;
    }
    
    Run Code Online (Sandbox Code Playgroud)

虽然这个解决方案更“正确”,但可读性较差,这意味着它的可维护性较差;它有更多的代码,这意味着更多的错误机会;如果free忘记调用(迟早发生),它有可能发生内存泄漏。