简体   繁体   English

在 pthread_create 代码中出现此错误?

[英]getting this error in pthread_create code?

I am creating my Operating system project in which this is my code, I am using a Linux operating system and I when I am compiling my code, the pthread_create() function is showing an error.我正在创建我的操作系统项目,这是我的代码,我使用的是 Linux 操作系统,当我编译我的代码时,pthread_create() function 显示错误。 The error is related to void return type.该错误与 void 返回类型有关。

#include <pthread.h>

#include <semaphore.h>

#include <stdio.h>

#include <unistd.h>

#include <stdlib.h>

#
define LEFT(PhilNum + 4) % 5# define RIGHT(PhilNum + 1) % 5

enum s {
    EATING,
    HUNGRY,
    THINKING
};
struct Philosopher {
    char * name;
    int id;

};

struct Philosopher P[5] = {
    {
        "Professor A",
        0
    },
    {
        "Professor B",
        1
    },
    {
        "Professor C",
        2
    },
    {
        "Professor D",
        3
    },
    {
        "Professor E",
        4
    }
};
int ProcessCurrantState[5];

sem_t MUTEX;
sem_t Chop[5];

void CheckAvailability(int PhilNum) {
    if (ProcessCurrantState[LEFT] != EATING && ProcessCurrantState[RIGHT] != EATING) {
        // ProcessCurrantState that eating
        ProcessCurrantState[PhilNum] = EATING;

        sleep(2);
        printf("--------------------------------------------------------\n");
        printf("| Philosopher %s ,id : %d | Picking up Chopsticks %d and %d  .| \n", P[PhilNum].name,
            PhilNum + 1, LEFT + 1, PhilNum + 1);
        printf("--------------------------------------------------------\n");
        printf("--------------------------------------------------------\n");
        printf("Philosopher %s , id : %d | is Eating .\n", P[PhilNum].name, PhilNum + 1);
        printf("--------------------------------------------------------\n");
        sem_post( & Chop[PhilNum]);
    }
}

// take up chopsticks
void PickUpChopSticks(int PhilNum) {

    sem_wait( & MUTEX);

    // ProcessCurrantState that hungry
    ProcessCurrantState[PhilNum] = HUNGRY;

    printf("--------------------------------------------------------\n");
    printf(" | Philosopher %s , id : %d | is Hungry . |\n", P[PhilNum].name, PhilNum + 1);
    printf("--------------------------------------------------------\n");
    // eat if neighbours are not eating
    CheckAvailability(PhilNum);

    sem_post( & MUTEX);

    // if unable to eat wait to be signalled
    sem_wait( & Chop[PhilNum]);

    sleep(1);
}

// put down chopsticks
void PutChopsticksDown(int PhilNum) {

    sem_wait( & MUTEX);

    // ProcessCurrantState that thinking
    ProcessCurrantState[PhilNum] = THINKING;
    printf("---------------------------------------------------------\n");
    printf("| Philosopher %s , id : %d | puting down Chopsticks %d and %d .|\n", P[PhilNum].name,
        PhilNum + 1, LEFT + 1, PhilNum + 1);
    printf("--------------------------------------------------------\n");
    printf("| Philosopher %s , id : %d | is thinking . | \n", P[PhilNum].name, PhilNum + 1);
    printf("--------------------------------------------------------\n");

    CheckAvailability(LEFT);
    CheckAvailability(RIGHT);

    sem_post( & MUTEX);
}

void * philospher(void * num) {

    while (1) {

        int i = (int * ) num;

        sleep(1);

        PickUpChopSticks(i);

        sleep(0);

        PutChopsticksDown(i);
    }
}

int main() {

    int i;
    pthread_t Thread[5];

    // initialize the semaphores
    sem_init( & MUTEX, 0, 1);

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

    // create philosopher processes

    for (i = 0; i < 5; i++) {
        pthread_create( & Thread[i], NULL, philospher, (void * ) P[i].id);
        printf("--------------------------------------------------------\n");
        printf("| Philosopher %s , id : %d | is thinking\n . |", P[i].name, i + 1);
        printf("--------------------------------------------------------\n");
    }

    for (i = 0; i < 5; i++)

        pthread_join(Thread[i], NULL);
}

and I am getting this error while running我在运行时收到此错误

main.cpp: In function ‘void* philospher(void*)’:
main.cpp:97:21: error: invalid conversion from ‘int*’ to ‘int’ [-fpermissive]
     int  i = (int *)num;

In pthread_create it casts int to void* .pthread_create中,它将int转换为void* The reverse conversion you need is void* to int :您需要的反向转换是void*int

int  i = (int)num;

In C++ code you may like to use std::thread instead of pthread_create , so that you can pass thread arguments without casting.在 C++ 代码中,您可能喜欢使用std::thread而不是pthread_create ,这样您就可以传递线程 arguments 而无需强制转换。 Eg:例如:

void philospher(int num);
// ...
std::thread Thread[5];
// ...
Thread[i] = std::thread(philospher, P[i].id);
//
for(auto& t : Thread)
    t.join();

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

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