【问题标题】:Error when compiling编译时出错
【发布时间】:2011-05-31 20:52:53
【问题描述】:

我正在尝试编译这段代码:

/* Includes */
#include <unistd.h>     /* Symbolic Constants */
#include <sys/types.h>  /* Primitive System Data Types */ 
#include <errno.h>      /* Errors */
#include <stdio.h>      /* Input/Output */
#include <stdlib.h>     /* General Utilities */
#include <pthread.h>    /* POSIX Threads */
#include <string.h>     /* String handling */
#include <semaphore.h>  /* Semaphore */

/* prototype for thread routine */
void handler ( void *ptr );

/* global vars */
/* semaphores are declared global so they can be accessed 
   in main() and in thread routine,
   here, the semaphore is used as a mutex */
sem_t mutex;
int counter; /* shared variable */

int main()
{
    int i[2];
    pthread_t thread_a;
    pthread_t thread_b;

    i[0] = 0; /* argument to threads */
    i[1] = 1;

    sem_init(&mutex, 0, 1);      /* initialize mutex to 1 - binary semaphore */
                                 /* second param = 0 - semaphore is local */

    /* Note: you can check if thread has been successfully created by checking return value of
       pthread_create */                                 
    pthread_create (&thread_a, NULL, (void *) &handler, (void *) &i[0]);
    pthread_create (&thread_b, NULL, (void *) &handler, (void *) &i[1]);

    pthread_join(thread_a, NULL);
    pthread_join(thread_b, NULL);

    sem_destroy(&mutex); /* destroy semaphore */

    /* exit */  
    exit(0);
} /* main() */

void handler ( void *ptr )
{
    int x; 
    x = *((int *) ptr);
    printf("Thread %d: Waiting to enter critical region...\n", x);
    sem_wait(&mutex);       /* down semaphore */
    /* START CRITICAL REGION */
    printf("Thread %d: Now in critical region...\n", x);
    printf("Thread %d: Counter Value: %d\n", x, counter);
    printf("Thread %d: Incrementing Counter...\n", x);
    counter++;
    printf("Thread %d: New Counter Value: %d\n", x, counter);
    printf("Thread %d: Exiting critical region...\n", x);
    /* END CRITICAL REGION */    
    sem_post(&mutex);       /* up semaphore */

    pthread_exit(0); /* exit thread */
}

但我收到此错误:

sem-ex.c: In function ‘int main()’:
sem-ex.c:35: error: invalid conversion from ‘void*’ to ‘void* (*)(void*)’
sem-ex.c:35: error:   initializing argument 3 of ‘int pthread_create(_opaque_pthread_t**, const pthread_attr_t*, void* (*)(void*), void*)’
sem-ex.c:36: error: invalid conversion from ‘void*’ to ‘void* (*)(void*)’
sem-ex.c:36: error:   initializing argument 3 of ‘int pthread_create(_opaque_pthread_t**, const pthread_attr_t*, void* (*)(void*), void*)’

我错过了什么/做错了什么?谢谢。

【问题讨论】:

    标签: c macos g++ pthreads compiler-errors


    【解决方案1】:

    在将handle 函数的地址传递给pthread_create 之前,您不能将其转换为void *。照原样传递。

    pthread_create (&thread_a, NULL, &handler, (void *) &i[0]);
    pthread_create (&thread_b, NULL, &handler, (void *) &i[1]);
    

    另外,请根据以下 nos 的评论更改您的 handler 函数的签名。

    【讨论】:

    • 除此之外,handler函数还应该返回一个void*,所以应该是void *handler (void *ptr);
    【解决方案2】:

    你有两个问题:

    1. 您的“处理程序”函数签名错误。它必须有返回类型 void*,not void。
    2. 您还应该删除强制转换(无论如何,强制转换是错误的类型,因此您的错误)。

    【讨论】:

      猜你喜欢
      • 2013-12-30
      • 2012-01-28
      • 2013-08-16
      • 2017-07-13
      • 2014-06-25
      • 2012-03-11
      • 2017-04-27
      相关资源
      最近更新 更多