Ich suche nach Pthread auf C. So bin ich neu. Ich versuche, Syntax und Rollen von Zeigern in Pthread-Code zu lernen. Kann mir jemand sagen, was sind meine Fehler nach Code? Ich kann nicht klar verstehen, was ich getan habe.Pthread-Bibliothek grundlegendes Beispiel funktioniert nicht richtig
Wenn ich versuche, den Rückgabewert pthread_create()
zu überprüfen, bekomme ich falsche/zufällige Werte.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
int *f_1,*f_2,*f_3,*f_4;
void p1(void *a);
void p2(void *a);
void p3(void *a);
void p4(void *a);
int main(void){
pthread_t thread_1, thread_2, thread_3, thread_4;
int *x=1,*y=2,*z=3,*w=4;
f_1=pthread_create(&thread_1, NULL, p1,(void *)x);
f_2=pthread_create(&thread_2, NULL, p2,(void *) y);
f_3=pthread_create(&thread_3, NULL, p1,(void *) z);
f_4=pthread_create(&thread_4, NULL, p1,(void *) w);
pthread_join(thread_1,NULL);
pthread_join(thread_2,NULL);
pthread_join(thread_3,NULL);
pthread_join(thread_4,NULL);
printf("Hi! From %d. thread!",f_1);
printf("Hi! From %d. thread!",f_2);
printf("Hi! From %d. thread!",f_3);
printf("Hi! From %d. thread!",f_4);
return 0;
}
void p1(void *a){
f_1=(int *)a;
}
void p2(void *a){
f_2=(int *)a;
}
void p3(void *a){
f_3=(int *)a;
}
void p4(void *a){
f_4=(int *)a;
}
'Ich kann nicht verstehen, klar, was ich getan habe. Frage: Wie hast du es dann gemacht? –
Kannst du zumindest erklären, was du * versucht * zu tun hast und welche * spezifischen * Probleme du mit diesem Code hast? –
Von einigen Websites, aber es wurde nicht kompiliert. – QatarNotAlone