Nosferatou90 Δημοσ. 22 Νοεμβρίου 2010 Δημοσ. 22 Νοεμβρίου 2010 Καλησπέρα,θα ήθελα να ρωτήσω εαν ξέρει κάποιος και μπορεί να με βοηθήσει στο παρακάτω: Έχω ένα server π.χ: #include <pthread.h> #include <stdio.h> #include <stdlib.h> #define NUM_THREADS 5 struct data{ int id; char name[40]; float value; }temp; int my_func() { printf("Epilexate Dhmiourgia Neou Logariasmou\n"); getchar(); do { printf("Eisagete to onoma sas\n"); gets(temp.name); // Eisagwgh tou onomatepwnymou tou xrhsth. }while(strlen(temp.name)>40); // Elegxoume oti to alfarithmhtiko einai mikrotero apo 40 xarakthres. . . . } ///////arxh ths main///////////////// int main(int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; int rc; long t; for(t=0;t<NUM_THREADS;t++){ printf("In main: creating thread %ld\n", t); rc = pthread_create(&threads[t], NULL,//edw ti prepei na valw gia na dhmiourgh8ei ena kainourgio thread gia thn sunarthsh mou myfunc();); if (rc){ printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); } } pthread_exit(NULL); } πώς γίνεται τα δεδομένα μου να τα αποθηκευσω στη struct data του server μέσω της συνάρτησης myfunc() (δηλαδή τι ορίσματα πρέπει να βάλω μέσα στη myfunc() )?? Ελπίζω να γίνομαι κατανοητός..
pinball_elf Δημοσ. 25 Νοεμβρίου 2010 Δημοσ. 25 Νοεμβρίου 2010 Δεν ξέρω αν είναι ακριβώς αυτό που θέλεις: > #define _REENTRANT #include <pthread.h> #include <stdio.h> #include <stdlib.h> #define NUM_THREADS 5 typedef struct data_type { int id; char name[40]; float value; } data_type; void * my_func(void * data_in) { data_type * data = (data_type *) data_in; printf("Epilexate Dhmiourgia Neou Logariasmou\n"); printf("Eisagete to onoma sas\n"); fgets(data->name, 40, stdin); /* ... */ } /* arxh ths main */ int main(int argc, char *argv[]) { pthread_t threads[NUM_THREADS]; data_type all_data[NUM_THREADS]; int rc; int i; for(i=0; i<NUM_THREADS; i++) { printf("In main: creating thread %d\n", i); rc = pthread_create(&threads[i], NULL, my_func, (void*)&(all_data[i])); if (rc) { printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); } else { pthread_join(threads[i], NULL); } } exit(0); }
Προτεινόμενες αναρτήσεις
Αρχειοθετημένο
Αυτό το θέμα έχει αρχειοθετηθεί και είναι κλειστό για περαιτέρω απαντήσεις.