I'm a newbie in Pthread programming.
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);
}
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];
}
}
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|
Here's my questions:
- Why the forbidden assigment of arrays? And how to solve it?
- 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?
- 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.
Thanks
P.S.: I post another similar question (about matrix) in the section below.
What about something like :
pthread_join is needed because you want to wait for every thread to be finished before exiting the application.
Well,