Please consider the below code:
cuckoo.c
#include <stdio.h>
#include <assert.h>
#include <pthread.h>
#include <stdlib.h>
void *thread_fn(void *vargp);
int main(int argc, char **argv){
if (argc < 2){
fprintf(stderr, "Meh, error!\n");
return 1;
}
pthread_t span[argc];
for (int i=1; i < argc; i++){
int input = atoi(argv[i]);
printf("input: argv[%d] = %s\n",i, argv[i]);
int rc = pthread_create(&span[i], NULL, thread_fn, &input);
assert (rc == 0);
}
for (int i=1; i < argc; i++){
int *output;
int err = pthread_join(span[i], (void **)&output);
assert (err == 0);
printf("in main: from thread %lu, input = %s, output = %d\n", span[i], argv[i], *output);
free(output);
}
}
void *thread_fn(void *vargp){
int *input = (int *)vargp;
int *output = malloc( sizeof(*output) );
for (int i=0; i <= *input; i++){
*output += i;
}
printf("in thread_fn: %lu, input = %d, output = %d\n", pthread_self(), *input, *output);
pthread_exit(output);
}
When I run this with individual arguments, it behaves well:
$ ./a.out 4
input: argv[1] = 4
in thread_fn: 139691607996160, input = 4, output = 10
in main: from thread 139691607996160, input = 4, output = 10
$ ./a.out 5
input: argv[1] = 5
in thread_fn: 140564160374528, input = 5, output = 15
in main: from thread 140564160374528, input = 5, output = 15
However, if a pass a number of arguments, it falls all over:
$ ./a.out $(seq 1 5)
input: argv[1] = 1
input: argv[2] = 2
in thread_fn: 139922608498432, input = 2, output = 3
input: argv[3] = 3
input: argv[4] = 4
in thread_fn: 139922518308608, input = 4, output = 10
input: argv[5] = 5
in thread_fn: 139922375698176, input = 5, output = 15
in thread_fn: 139922600105728, input = 5, output = 15
in thread_fn: 139922509915904, input = 5, output = 15
in main: from thread 139922608498432, input = 1, output = 3
in main: from thread 139922600105728, input = 2, output = 15
in main: from thread 139922518308608, input = 3, output = 10
in main: from thread 139922375698176, input = 4, output = 15
in main: from thread 139922509915904, input = 5, output = 15
What am I doing wrong here? Is this approach not recommended? I was able to accomplish this instead with a struct as below, and I still cannot make the above snippet function similarly. I would still like to learn and fix the above pasted code.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <pthread.h>
void *pappanava(void *vargp);
struct payload{
int input;
int sum;
};
int main(int argc, char **argv){
if (argc < 2){
fprintf(stderr, "Invalid usage\n");
return 1;
}
//struct payload *this = malloc( sizeof(*this) );
pthread_t span[argc];
for (int i=1; i< argc; i++){
printf("i/p: %s\n", argv[i]);
struct payload *this = malloc( sizeof(*this) );
this->input = atoi(argv[i]);
int rc = pthread_create(&span[i], NULL, pappanava, &this->input);
}
for (int i=1; i< argc; i++){
struct payload *this;
pthread_join(span[i], (void**)&this);
printf("In _main_: thread: %lu, input: %d, sum: %d\n", span[i], this->input, this->sum);
free(this);
}
return 0;
}
void *pappanava(void *vargp){
struct payload *this = ( struct payload *) vargp;
int sum = 0;
for (int i=0; i <= this->input; i++){
sum += i;
}
this->sum = sum;
printf("In fn: thread: %lu, input: %d, sum: %d\n", pthread_self(), this->input, this->sum);
pthread_exit(this);
}