gpt4 book ai didi

c - 如何在简单的 Pthread 编程中分配数组变量?

转载 作者:行者123 更新时间:2023-12-04 05:08:38 24 4
gpt4 key购买 nike

我是 Pthread 编程的新手。

我一直在尝试以非常简单的方式使用 Pthread,就像下面的代码一样,它在我的 CodeBlock 中运行良好,因为我已经包含了 dll 和 bin 文件。

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

void *printNumber(void *x);

int main(){
pthread_t threadA, threadB, threadC, threadD;
pthread_create(&threadA, NULL, printNumber, (void *)"Sponge");
pthread_create(&threadB, NULL, printNumber, (void *)"Star");
pthread_create(&threadC, NULL, printNumber, (void *)"Squid");
pthread_create(&threadD, NULL, printNumber, (void *)"Crab");
pthread_exit(NULL);
return 0;
}

void *printNumber(void *x){
char* id = (char*)x;
int i;
for(i=0;i<100;i++){
printf("Thread %s: printing integer value %i\n", id, i);
}
pthread_exit(NULL);
}

然后我编写了另一个简单的程序,使用 Pthread 将 2 个数组(arrayA + arrayB)添加到 arrayC 中。
这是我的简单代码。
一切都是硬编码的,在 main() 中没有循环等,因为我想让它尽可能简单,让我了解如何创建单个 Pthread。
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>

#define SIZE 16
#define UPPER_RAND 100
#define NUM_THREADS 4

// HEADER PROCEDURES
void randomGenerator(int arr[]);
void printArray(int arr[]);
void *addArrayPthread(void *x);

typedef struct {
int startIdx;
int arrC[SIZE], arrA[SIZE], arrB[SIZE];
} someType;

int main(){
printf("A Simple Program To Add Arrays Using PThread\n");
int arrayA[SIZE];
int arrayB[SIZE];
int arrayC[SIZE];

randomGenerator(arrayA);
printArray(arrayA);

randomGenerator(arrayB);
printArray(arrayB);

someType *w,*x,*y,*z;
w = (someType*) malloc(sizeof(someType));
x = (someType*) malloc(sizeof(someType));
y = (someType*) malloc(sizeof(someType));
z = (someType*) malloc(sizeof(someType));

(*w).startIdx = 0;
(*w).arrA = arrayA;
(*w).arrB = arrayB;
(*w).arrC = arrayC;

(*x).startIdx = 4;
(*x).arrA = arrayA;
(*x).arrB = arrayB;
(*x).arrC = arrayC;

(*y).startIdx = 8;
(*y).arrA = arrayA;
(*y).arrB = arrayB;
(*y).arrC = arrayC;

(*z).startIdx = 12;
(*z).arrA = arrayA;
(*z).arrB = arrayB;
(*z).arrC = arrayC;


pthread_t threadA, threadB, threadC, threadD;
pthread_create(&threadA, NULL, addArrayPthread, (void *)w);
pthread_create(&threadB, NULL, addArrayPthread, (void *)x);
pthread_create(&threadC, NULL, addArrayPthread, (void *)y);
pthread_create(&threadD, NULL, addArrayPthread, (void *)z);

pthread_join(threadA, NULL);
pthread_join(threadB, NULL);
pthread_join(threadC, NULL);
pthread_join(threadD, NULL);

return 0;
}


//=====================================================================================//

void randomGenerator(int arr[]){
printf("Generating random value for the array...\n");
int i;
for (i=0;i<SIZE;i++){
arr[i] = (rand() % UPPER_RAND);
}
}

void printArray(int arr[]){
printf("Display the array value...\n");
int i;
printf("[");
for (i=0;i<SIZE;i++){
printf("%i, ",arr[i]);
}
printf("]\n");
}

void *addArrayPthread(void *x){
someType *p = (someType *) x;
printf("Adding to arrays, starting from index #%i\n",(*p).startIdx);
int blockSize = SIZE/NUM_THREAD;
int end = (*p).startIdx + blockSize;
int i;
for (i=(*p).startIdx;i<end;i++){
(*p).arrC[i] = (*p).arrA[i] + (*p).arrB[i];
}
}

我在这些行周围收到 12 条错误消息:
(*x).arrA = arrayA;而这样的
||In function `int main()':|
\pth_array.c|58|error: ISO C++ forbids assignment of arrays|

这是我的问题:
  • 为什么禁止分配数组?以及如何解决?
  • 在上面的第一个程序中,我输入了 pthread_exit(NULL) 两次:在
    main() 和 void* 函数中。我想我只需要放一次。那么我到底要把它放在哪里呢?在 main() 或 void* 函数中?
  • 是否必须输入 pthread_join 在返回 0 之前的 main() 中?

  • 提前谢谢你。
    你的解释对我很有帮助。

    谢谢

    PS:我在下面的部分中发布了另一个类似的问题(关于矩阵)。

    最佳答案

    怎么样:

    typedef struct {
    int startIdx;
    int *arrC, *arrA, *arrB;
    } someType;

    [...]

    x->arrA = arrayA
    [...]

    需要 pthread_join 是因为您希望在退出应用程序之前等待每个线程完成。

    关于c - 如何在简单的 Pthread 编程中分配数组变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15176603/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com