История изменений
Исправление CrazyAlex25, (текущая версия) :
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <string.h>
#define ERROR_CREATE_THREAD -11
#define ERROR_JOIN_THREAD -12
#define BAD_MESSAGE -13
#define SUCCESS 0
typedef struct someArgs_tag {
int *a;
int sum;
} someArgs_t;
void* helloWorld(void *args) {
someArgs_t *arg = (someArgs_t*) args;
int len;
arg->sum=0;
for(int i=0;i<10;i++)
{
printf("helloWorldFunc %i\n",arg->a[i]);
arg->sum+=arg->a[i];
}
return SUCCESS;
}
#define NUM_THREADS 2
int main() {
pthread_t threads[NUM_THREADS];
int status;
int i;
int status_addr;
someArgs_t args[NUM_THREADS];
int a[2][10];
srand(time(NULL));
for (int i = 0; i<2; i++)
{
for (int j = 0; j<10; j++)
{
a[i][j] = rand() % 100 + 1;
printf("%i\n",a[i][j]);
}
}
for (i = 0; i < NUM_THREADS; i++) {
//args->a[i]=malloc(10*sizeof(int));
//memcpy(args->a[i],a[i],10);
args[i].a=&a[i];
}
for (i = 0; i < NUM_THREADS; i++) {
status = pthread_create(&threads[i], NULL, helloWorld, (void*) &args[i]);
if (status != 0) {
printf("main error: can't create thread, status = %d\n", status);
exit(ERROR_CREATE_THREAD);
}
}
printf("Main Message\n");
for (i = 0; i < NUM_THREADS; i++) {
status = pthread_join(threads[i], (void**)&status_addr);
if (status != SUCCESS) {
printf("main error: can't join thread, status = %d\n", status);
exit(ERROR_JOIN_THREAD);
}
printf("joined with address %d\n", status_addr);
}
int total=0;
for (i = 0; i < NUM_THREADS; i++) {
//printf("thread %d arg.out = %d\n", i, args[i].out);
printf("sum %i", args[i].sum);
total+=args[i].sum;
}
printf("Total sum %i", total);
return 0;
}
15 минут чтения https://learnc.info/c/pthreads_create_and_join.html
Исходная версия CrazyAlex25, :
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <string.h>
#define ERROR_CREATE_THREAD -11
#define ERROR_JOIN_THREAD -12
#define BAD_MESSAGE -13
#define SUCCESS 0
typedef struct someArgs_tag {
int *a;
int sum;
} someArgs_t;
void* helloWorld(void *args) {
someArgs_t *arg = (someArgs_t*) args;
int len;
arg->sum=0;
for(int i=0;i<10;i++)
{
printf("helloWorldFunc %i\n",arg->a[i]);
arg->sum+=arg->a[i];
}
return SUCCESS;
}
#define NUM_THREADS 2
int main() {
pthread_t threads[NUM_THREADS];
int status;
int i;
int status_addr;
someArgs_t args[NUM_THREADS];
int a[2][10];
srand(time(NULL));
for (int i = 0; i<2; i++)
{
for (int j = 0; j<10; j++)
{
a[i][j] = rand() % 100 + 1;
printf("%i\n",a[i][j]);
}
}
for (i = 0; i < NUM_THREADS; i++) {
//args->a[i]=malloc(10*sizeof(int));
//memcpy(args->a[i],a[i],10);
args[i].a=&a[i];
}
for (i = 0; i < NUM_THREADS; i++) {
status = pthread_create(&threads[i], NULL, helloWorld, (void*) &args[i]);
if (status != 0) {
printf("main error: can't create thread, status = %d\n", status);
exit(ERROR_CREATE_THREAD);
}
}
printf("Main Message\n");
for (i = 0; i < NUM_THREADS; i++) {
status = pthread_join(threads[i], (void**)&status_addr);
if (status != SUCCESS) {
printf("main error: can't join thread, status = %d\n", status);
exit(ERROR_JOIN_THREAD);
}
printf("joined with address %d\n", status_addr);
}
int total=0;
for (i = 0; i < NUM_THREADS; i++) {
//printf("thread %d arg.out = %d\n", i, args[i].out);
printf("sum %i", args[i].sum);
total+=args[i].sum;
}
printf("Total sum %i", total);
return 0;
}