c - Incomprehensible result of a multithread code -


i start c programming project used multithread. before start project, have written code practice. purpose see how mutex , threads works. not work properly. here code:

#include <stdio.h> #include <pthread.h> #include <stdlib.h> #include <string.h>  pthread_mutex_t mymutex; char mystrings[100][30]; int i=0;  void *printthread1() {     printf("this initial of first thread\n");     (int j=0; j<33; j++) {         pthread_mutex_lock(&mymutex);         strcpy(mystrings[i], "this first thread");         i++;         pthread_mutex_unlock(&mymutex);     }     pthread_exit(null); }  void *printthread2() {     printf("this initial of second thread\n");     (int j=0; j<33; j++) {         pthread_mutex_lock(&mymutex);         strcpy(mystrings[i], "this second thread");         i++;         pthread_mutex_unlock(&mymutex);     }      pthread_exit(null);  }  void *printthread3() {     printf("this initial of third thread\n");     (int j=0; j<33; j++) {         pthread_mutex_lock(&mymutex);         strcpy(mystrings[i], "this third thread");         i++;         pthread_mutex_unlock(&mymutex);     }      pthread_exit(null); }    int main(int argc, char *argv[]) {     pthread_t firstthread, secondthread, thirdthread;     //pthread_attr_t attr;     pthread_mutex_init(&mymutex, null);     //pthread_attr_init(&attr);     //pthread_attr_setdetachstate(&attr, pthread_create_joinable);      int ft;     ft = pthread_create(&firstthread, null, printthread1(), null);     if (ft){         printf("error; return code pthread_create() %d\n", ft);         exit(-1);     }     ft = pthread_create(&secondthread, null, printthread2(), null);     if (ft){         printf("error; return code pthread_create() %d\n", ft);         exit(-1);     }     ft = pthread_create(&thirdthread, null, printthread3(), null);     if (ft){         printf("error; return code pthread_create() %d\n", ft);         exit(-1);     }      pthread_join(firstthread, null);     pthread_join(secondthread, null);     pthread_join(thirdthread, null);      pthread_mutex_destroy(&mymutex);      (int j=0;j<100; j++) {         printf("string[%d] = %s\n",j,mystrings[j]);     }     printf("\n");      pthread_exit(null);     return -1; } 

when execute code, result is:

this initial of first thread program ended exit code: 0 

i can't figure out mistake.

ft = pthread_create(&firstthread, null, printthread1(), null); 

should read

ft = pthread_create(&firstthread, null, printthread1, null); 

and pthread_create calls.

to use pthread_create, need pass address of thread-start routine, in c writing name of function without function-call parentheses.

as have written it, call intended thread-start routine, on main thread, , pass whatever returns pthread_create thread-start routine. never returns, because calls pthread_exit, (since pthread_create hasn't been called yet, there 1 thread) terminates entire program.

unfortunately, have crank warnings way before compiler catch mistake, , it's not super clear problem is:

$ gcc -std=c99 -wall -pedantic -pthread test.c test.c: in function ‘main’: test.c:60:45: warning: iso c forbids passing argument 3    of ‘pthread_create’ between function pointer , ‘void *’ [-wpedantic]      ft = pthread_create(&firstthread, null, printthread1(), null);                                              ^ 

without -pedantic, no complaint.


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -