c - 通过 pthread_join() 加入两个 pthread 无法按预期工作

标签 c linux multithreading pthreads

<分区>

我正在创建两个不同的 pthread通过 pthread_create()并通过调用 pthread_join() 等待他们退出.

当我运行程序时,最后的输出是“等待线程 1 完成...”,因此它实际上在尝试加入第一个线程时停止,然后终止。

当我只创建和运行一个线程并等待其执行完成时使用 pthread_join() ,一切正常,输出符合预期。

我在这里错过了什么?我已经在网上搜索并发现 pthread_join()创建多个线程时表现不正常。但是我实际上如何使用它来等待完成两个 pthread 的执行?是吗?

这是我的代码:

typedef struct
{
  double speed_in_kmh;
  double current_power_in_mega_watts;
} DeLorean;

typedef struct
{
  unsigned int* cap_indices;
  unsigned int array_length;
} IndexContainer;

typedef struct {
  unsigned char* c_string;
  unsigned int value;
} FluxCapacitor;

DeLorean* delorean__;
FluxCapacitor** capacitors__;

pthread_mutex_t lock;

// Thread function.
void* assembleDeLorean(void* indices)
{
    // Get mutex to lock function and make sure
    // that only one thread at a time is using it.
    pthread_mutex_lock(&lock);


    // Declare "indices" parameter as IndexContainer.
    IndexContainer* iC = indices;

    double sum = 0;

    // Iterate through all given indices in iC
    // and add value of iC to sum, if index exists.
    for (int i = 0; i < 121; ++i)
    {
        for (int j = 0; j < iC->array_length; ++j)
        {
            if (i == iC->cap_indices[j])
            {
                sum += capacitors__[i]->value;
                break;
            }
        }
    }

    // Assign computed sum to power of global delorean.
    delorean__->current_power_in_mega_watts = sum;

    // Release mutex.
    pthread_mutex_unlock(&lock);

    // Stop thread.
    pthread_exit(NULL);
}

int main(void)
{
    printf("Main thread \"main()\" was started.\n");

    capacitors__ = createFluxCapacitorArray(121);
    delorean__ = createDeLorean(0, 0);

    IndexContainer* iC_1 = malloc(sizeof(*iC_1));
    iC_1->array_length = 21;
    iC_1->cap_indices = malloc(21 * sizeof(unsigned int));

    IndexContainer* iC_2 = malloc(sizeof(*iC_2));
    iC_2->array_length = 100;
    iC_2->cap_indices = malloc(100 * sizeof(unsigned int));

    // Fill iC_1.
    for (int i = 0; i < 21; ++i)
    {
        iC_1->cap_indices[i] = i;
    }

    // Fill iC_2.
    int k = 0;
    for (int i = 21; i < 121; ++i)
    {
        iC_2->cap_indices[k] = i;
        ++k;
    }

    // Declare threads.
    pthread_t thread1, thread2;
    int rT1, rT2;

    // Initialize mutex protecting "assembleDeLorean" function.
    pthread_mutex_init(&lock, NULL);

    // Create & run first thread.
    printf("Creating and running thread1.\n");
    rT1 = pthread_create(&thread1, NULL, assembleDeLorean, &iC_1);
    if (rT1 != 0)
    {
        printf("Thread 1 could not be created.\n");
        return EXIT_FAILURE;
    }

    printf("Return value of creation of thread1: %d\n", rT1);


    // Create & run second thread.
    printf("Creating and running thread2.\n");
    rT2 = pthread_create(&thread2, NULL, assembleDeLorean, &iC_2);
    if (rT2 != 0)
    {
        printf("Thread 2 could not be created.\n");
        return EXIT_FAILURE;
    }

    printf("Return value of creation of thread2: %d\n", rT2);   

    // Wait for threads to finish.
    printf("Waiting for thread1 to finish...\n");
    if (pthread_join(thread1, NULL))
    {
        printf("An error occured while joining thread1.\n");
        return EXIT_FAILURE;
    }

    printf("Thread1 finished!");


    printf("Waiting for thread2 to finish...\n");
    if (pthread_join(thread2, NULL))
    {
        printf("An error occured while joining thread2.\n");
        return EXIT_FAILURE;
    }
    printf("Thread2 finished!");

    return EXIT_SUCCESS;
}

最佳答案

您向 pthread_create() 函数传递的参数不正确。

rT2 = pthread_create(&thread2, NULL, assembleDeLorean, &iC_2);
rT1 = pthread_create(&thread1, NULL, assembleDeLorean, &iC_1);

应该是

rT2 = pthread_create(&thread2, NULL, assembleDeLorean, iC_2);
rT1 = pthread_create(&thread1, NULL, assembleDeLorean, iC_1);

关于c - 通过 pthread_join() 加入两个 pthread 无法按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50038990/

相关文章:

linux - 无法打开/dev/tty(2,3,4)。没有这样的文件或目录

multithreading - 快速线程安全读取优化 Map[Long, _]

c - realloc,用于 C 中的字符串数组

无法运行 make 创建的可执行文件

c - block 大小是在硬件中固定的还是可以用 C 或 asm 重新编程?

c++ - Cmake - 如何复制带有输入数据的文件以构建输出文件夹

linux - 使用sed修改配置

c++ - 错误 : argument of type "void (opca_hello::)()" does not match "void* (*)(void*)"

c - VS2010 - C - process.h - "joining"线程以 _beginthread 开始

java - 如何终止包装函数的线程? [复制]