【问题标题】:How do I pass an integer into the pthread_create function from argv? (C)如何将整数从 argv 传递给 pthread_create 函数? (C)
【发布时间】:2015-02-07 03:12:35
【问题描述】:

对于这个程序,我通过命令行传入数字,然后有一个多线程程序接受每个参数,计算其因子,然后打印它们。我知道 c++,但我对 c 很粗糙,似乎无法为这个程序正确地进行强制转换。特别是当我将参数传递给 thread_create 并将其转换为整数时。我下面的代码可以编译,但转换后的值始终为 0。如何将 char 值转换为 void* 然后转换为整数?

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

    #define MAX_ARRAY (17)

    void *thread_func(void *);

    int factors[MAX_ARRAY];

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

            pthread_t thread_handle[argc];
            int i;
            int g;

            // Create Children Threads
            for ( i = 0; i < argc; i++ ) {
                    pthread_create(&thread_handle[i], NULL, thread_func, &argv[i + 1]);
            }

            // Rejoin Threads
            for ( i = 0; i < argc; i++ ) {
                    pthread_join(thread_handle[i], NULL);

                    // Print Factors Here
                    printf("%d: ", atoi(argv[i]));
                    for ( g = 0; g < MAX_ARRAY; g++ ) {
                            printf("%d, ", factors[g]);
                    }

                    printf("\n");

                    for ( g = 0; g < MAX_ARRAY; g++ ) {
                            factors[g] = 0;
                    }
            }
            return 0;
    }

    void *thread_func(void *data) {
            int n = atoi(data); 
            int x;
            int v;

            printf("Number to factor is: %d\n", n);

            for ( x = 1; x <= n; ++x ) {
                    if (n%x == 0)
                            factors[v++] = x;
            }
            return NULL;
    }

【问题讨论】:

  • 我没有在您的代码中看到任何强制转换,也没有任何理由认为您应该需要它们。
  • 在 thread_func 中我必须取消引用传入的值,该值是一个 void 指针。我必须将它从 void* 转换为 integer* 或我可以取消引用的东西。
  • @user3208991:不,指针指向命令行中的字符串。您只需将其传递给atoi(就像您正在做的那样)将字符串解释为十进制数。
  • 你的代码中的问题是无关的。 v 未初始化使用,factors 是全局的,所有线程都可以访问,无需任何同步。我不确定你想在那里做什么......
  • 我试图让每个线程访问同一个数组并将其因子插入其中。一旦一个线程这样做并加入,它就会从数组中打印它的因子,然后当下一个线程运行时,它会覆盖前一个线程的数字并做同样的事情。

标签: c casting pthreads argv


【解决方案1】:

问题是每个线程使用相同的数组作为因子,没有任何同步。但是,如果每个线程在运行之前都必须为数组获得一个锁,那么它们实际上会全部按顺序运行,这会破坏线程的目的。

顺便提一下,argv[0] 是程序名,你应该跳过它。

您应该为每个线程设置不同的因子数组,以便它们可以独立工作而不受干扰。您还应该在主线程中进行所有显示,以控制打印的顺序。

由于可能最好按顺序显示因子,因此应首先创建所有线程,然后将所有线程连接起来,最后显示结果。

这里和那里也有一些小错误,比如一个错误或未初始化的变量。

这是一个更正的版本:

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

#define MAX_ARRAY   17

typedef struct {
    int factors[MAX_ARRAY];
    int n;
} thread_data;

void * thread_func (void *);

int main (int argc, char *argv[]) {
    int n = argc - 1;
    pthread_t thread_handle[n];
    thread_data thread_data_table[n];
    int i;

    // Create Children Threads
    for (i = 0; i < n; i++ ) {
        thread_data_table[i].n = atoi (argv[i + 1]);
        pthread_create(&thread_handle[i], NULL, thread_func,
                       &thread_data_table[i]);
    }

    // Join Threads
    for (i = 0; i < n; i++ ) {
        pthread_join(thread_handle[i], NULL);
    }

    // Print Factors
    for (i = 0; i < n; i++) {
        int j;

        printf("%d: ", thread_data_table[i].n);
        for (j = 0; j < MAX_ARRAY; j++ ) {
            int x = thread_data_table[i].factors[j];
            if (x == 0) {
                break;
            }
            printf("%d, ", x);
        }
        printf("\n");        
    }
    return 0;
}

void * thread_func (void *data)
{
    thread_data *p = (thread_data*)data;
    int i;
    int count = 0;

    for (i = 1; i <= p->n; ++i ) {
        if (p->n % i == 0) {
            if (count == MAX_ARRAY) {
                break;
            }
            p->factors[count++] = i;
        }
    }
    if (count < MAX_ARRAY) {
        p->factors[count] = 0;
    }

    return NULL;
}

【讨论】:

  • 谢谢!我为我凌乱的代码道歉。我误解了线程之间的实际运行方式。是否可以通过在每个线程上分配一个数组(加入后释放它)来实现相同的结果?
猜你喜欢
  • 2014-05-28
  • 1970-01-01
  • 2022-08-03
  • 2023-03-31
  • 2021-05-22
  • 2011-12-14
  • 2020-09-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多