【问题标题】:Passing an int to a pthread将 int 传递给 pthread
【发布时间】:2021-06-01 16:09:28
【问题描述】:

无法将 pthread 的最后一个参数作为实际 int 传递。我希望能够访问放在 pthread_create 的最后一个参数中的 1。任何帮助将不胜感激!

#include <pthread.h>
#include <unistd.h>
#include <malloc.h>
#define MAX 5
int arr[MAX];

int p0 = 0;
int p1 = 0;
int p2 = 0;
int p3 = 0;
int p4 = 0;

void *process(void *arg);


int main(int argc, char *argv[]) {

        if(argc != 7) {
                printf("must be 6 ints");
                return -1;
        }

        int quantum = atoi(argv[1]);

        p0 = atoi(argv[2]);
        p1 = atoi(argv[3]);
        p2 = atoi(argv[4]);
        p3 = atoi(argv[5]);
        p4 = atoi(argv[6]);

下面这个是我指的pthread。



        int *pointer0 = malloc(sizeof(*pointer0));
        *pointer0 = p0;
        /* intialize thread 1 */
        pthread_t tid0;
        pthread_attr_t attr0;
        pthread_attr_init(&attr0);
        pthread_create(&tid0, &attr0, process, 1);
        pthread_join(tid0, NULL);


        //if the time remaining of process is not zero, run process[i] again
        return 0;
}

void *process(void *arg) {

        //char **pointer = (char**) arg;
        //int burst = **pointer;
        int burst = 0;
        //int i = atoi(arg);    
        printf("%ls", (int *)arg);
        pthread_exit(NULL);

}

【问题讨论】:

  • void *arg 是一个指针,如果你想将它强制转换为 int,那么你应该使用 (long),尽管将 long 作为指针传递可能会发出警告。
  • 不太确定该怎么做。你是说这不能做吗?
  • 可以做到,Miguel Carvalho 写了一个很好的解释如何做到这一点。

标签: c pthreads


【解决方案1】:

如果您阅读pthread_create() 函数的手册页,您会注意到您在最后一个参数中传递了int 而不是void*

int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                          void *(*start_routine) (void *), void *arg);

为了解决这个问题,创建一个变量来存储int 值并使用它的地址将它传递给pthread_create() 函数。然后,在process() 函数中,将参数转换为int

int main () {
    // (...)
    int arg = 1;
    pthread_create(&tid0, &attr0, process, &arg);
    // (...)
    return 0;
}

void *process (void *arg) {
    int arg = *((int *) arg);
    // (...)
    return NULL;
}

现在,请注意我刚刚在main() 函数中声明的变量arg 将始终可用,只要程序最后只退出main() 函数。如果在另一个函数内部调用pthread_create() 函数,强烈建议您动态分配内存以确保即使退出该函数也始终可用。在退出process()函数之前不要忘记释放内存。

int foo () {
    // (...)
    int *arg = malloc( sizeof(int) );
    if ( !arg ) {
        fprintf(stderr, "error: allocating memory");
        return 0;
    }
    *arg = 1;
    pthread_create(&tid0, &attr0, process, arg);
    // (...)
    return 0;
}

void *process (void *arg) {
    int value = *((int *) arg);
    // (...)
    free(arg);
    return NULL;
}

如果您想通过pthread_create() 函数传递更多变量,请声明struct 并重复相同的过程。

typedef struct {
    char name[50];
    char surname[50];
    int age;
} person_t;

int foo () {
    // (...)
    person_t *arg = malloc( sizeof(person_t) );
    if ( !arg ) {
        fprintf(stderr, "error: allocating memory");
        return 0;
    }
    sprintf( arg->name , "Miguel" );
    sprintf( arg->name , "Carvalho" );
    arg->age = 22;
    pthread_create(&tid0, &attr0, process, arg);
    // (...)
    return 0;
}

void *process (void *arg) {
    person_t person = *((person_t *) arg);
    printf( "%s; %s; %d\n", person.name, person.surname, person.age);
    // (...)
    free(arg);
    return NULL;
}

【讨论】:

    猜你喜欢
    • 2011-02-09
    • 2011-12-11
    • 1970-01-01
    • 1970-01-01
    • 2020-11-19
    • 2013-07-13
    • 2018-05-01
    • 2013-12-10
    • 1970-01-01
    相关资源
    最近更新 更多