如何在一个简单的Pthread编程分配数组变量? [英] How do I assign array variable in a simple Pthread programming?

查看:137
本文介绍了如何在一个简单的Pthread编程分配数组变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Pthread的编程的新手。

I'm a newbie in Pthread programming.

我一直在这样code以下非常简单的方式使用的Pthread,和它运作良好,在我的codeBLOCK,因为我已经包含该dll和bin文件。

I've been trying to use Pthread in a very simple way like this code below, and it works well in my CodeBlock as I already included the dll and bin files.

#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);
}

然后我写的另一个简单的程序中添加2阵列(arrayA + arrayB)到arrayC,使用Pthread的。
这里是我的简单code。
一切都很难codeD,无循环和这样在main(),因为我想让它尽可能的简单,我了解如何创建一个单一的Pthread。

And then I write another simple program to add 2 arrays (arrayA + arrayB) into arrayC, using Pthread. Here's my simple code. Everything was hardcoded, no looping and such in the main(), because I want to make it as simple as possible for me to understand how to create a single 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;和这样

I got 12 error messages around these lines: (*x).arrA = arrayA; and such

||In function `int main()':|
\pth_array.c|58|error: ISO C++ forbids assignment of arrays|

下面是我的问题:


  1. 为什么阵列的禁assigment?而如何解决呢?

  2. 在上面的第一个节目,我把了pthread_exit(NULL)两次:在
    main()和在空隙*函数。我想我只需要把它一次。那么,究竟做我必须把它?在main()或void *的功能?

  3. 是否必须返回0之前,把在pthread_join 在main()?

  1. Why the forbidden assigment of arrays? And how to solve it?
  2. In the first program above, I put pthread_exit(NULL) twice: in main() and in the void* function. I suppose I only need to put it once. So where do exactly I have to put it? In main() or in the void* function?
  3. Is it mandatory to put pthread_join in the main() before return 0?

感谢您提前。
你的解释将是对我来说是很大的帮助。

Thank you in advance. Your explanation will be a big help for me.

感谢

P.S:我在下面的章节后另一个类似的问题(约矩阵)。

P.S.: I post another similar question (about matrix) in the section below.

推荐答案

关于像什么:

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

[...]

x->arrA = arrayA
[...]

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

pthread_join is needed because you want to wait for every thread to be finished before exiting the application.

这篇关于如何在一个简单的Pthread编程分配数组变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆