Reputation: 1
I'm meeting a problem with this code which is a basic code from my books to help understand how the threads works. It's is supposed to create NTHREADS which should execute the neg
function and then return the opposite of the argument received
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <errno.h>
#include <string.h>
#define NTHREADS 2
void *neg (void * param);
int main(int argc, char * argv[]){
pthread_t threads[NTHREADS];
int arg [NTHREADS];
int err;
long i;
for(i = 0; i< NTHREADS; i++){
arg[i] = i;
err= pthread_create(&(threads[i]), NULL, &neg, (void *) &(arg[i]));
if(err =! 0){
error(err,"pthread_create");
}
}
int j;
for (j = 0; j < NTHREADS; j++){
int *r;
err= pthread_join(threads[j], (void **) &r);
printf("Resultat [%d] = %d \n", j, *r);
free(r);
if(err!=0){
error(err,"pthread_join");
}
}
return(EXIT_SUCCESS);
}
void *neg (void * param){
int *l;
l= (int *) param;
int *r= (int *) malloc(sizeof(int));
*r = -*l;
return ((void *) r);
}
When compiling I receive this message:
nico@nico-G56JR:~/Desktop$ gcc -pthread Threads.c
nico@nico-G56JR:~/Desktop$ ./a.out
./a.out: c: Unknown error 4196644
I can't find the mistake, can someone help me?
Thanks in advance
Upvotes: 0
Views: 658
Reputation: 1841
This
if(err =! 0){
should be
if(err != 0){
It is a bit of bad luck really, because err =! 0 actually compiles, assigning err a not zero value, which then falls into the error case passing something into the error function that is not really meaningful for your code.
Upvotes: 3