2012-06-28 15 views
5

मैं एक धागा और से बनाने के लिए कोशिश कर रहा हूँ कि मैं क्या याद इस यह करने के लिए सही तरीके से किया जाना चाहिए:pthread_create काम नहीं कर रहा है। गुजर तर्क 3 चेतावनी

#include <pthread.h> 
#include <stdio.h> 
#include <stdlib.h> 
#define NUM_THREADS 5 

int SharedVariable =0; 
void SimpleThread(int which) 
{ 
    int num,val; 
    for(num=0; num<20; num++){ 
     if(random() > RAND_MAX/2) 
      usleep(10); 
     val = SharedVariable; 
     printf("*** thread %d sees value %d\n", which, val); 
     SharedVariable = val+1; 
    } 
    val=SharedVariable; 
    printf("Thread %d sees final value %d\n", which, val); 
} 

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, SimpleThread, (void*)t); 
     if (rc){ 
     printf("ERROR; return code from pthread_create() is %d\n", rc); 
     exit(-1); 
     } 
    } 

    /* Last thing that main() should do */ 
    pthread_exit(NULL); 
} 

और त्रुटि है कि मैं हो रही है इस एक है:

test.c: फ़ंक्शन 'मुख्य' में: test.c: 28: चेतावनी: 'pthread_create' को असंगत सूचक प्रकार /usr/include/pthread.hs27 से नोट करें: नोट: अपेक्षित 'शून्य * (* ) (शून्य *) 'लेकिन तर्क प्रकार का है' शून्य (*) (i एनटी) '

मैं सरल थ्रेड फ़ंक्शन को नहीं बदल सकता इसलिए पैरामीटर के प्रकार को बदलना एक विकल्प नहीं है, भले ही मैंने पहले ही कोशिश की है और यह काम नहीं करता है।

मैं क्या गलत कर रहा हूं?

उत्तर

12

SimpleThread

void* SimpleThread(void *args) { 
} 

जब आप अपने धागा करने के लिए पैरामीटर भेजने के रूप में घोषित किया जाना चाहिए, यह सबसे अच्छा है एक struct उनके लिए, कि करने के लिए एक सूचक को परिभाषित पारित structvoid* के रूप में, और वापस करने के लिए कास्ट करने के लिए समारोह के अंदर सही प्रकार।

+0

मैंने ऐसा करने की कोशिश की लेकिन फिर जब भी मैं int का उपयोग करता हूं तो मुझे त्रुटियां मिलती हैं जो – randomizertech

+1

@ fgualda87 एक त्वरित एक-टाइमर के रूप में आप 'int'' void * 'के रूप में पास कर सकते हैं - यह सुनिश्चित करने के लिए कि आपका कोड काम करता है। 'Int जो = (int) args जोड़ें; ', और कोड संकलित और चलाया जाना चाहिए। आपको 'स्ट्रक्चर' में 'int' लपेटने पर विचार करना चाहिए, हालांकि। – dasblinkenlight

+0

बिल्कुल सही काम किया! SimpleThread विधि को बदले बिना ऐसा करने का कोई तरीका है? – randomizertech

3

यहां आपके प्रोग्राम का संकलन और "कामकाजी" संस्करण है, हालांकि मुझे यह नहीं जानना है कि यह वास्तव में क्या कर रहा है। दर्शकों के आलोचकों के लिए, अंत में pthread_join लूप के लिए मैं पहले से माफ़ी मांगता हूं।

#include <stdio.h> 
#include <stdlib.h> 
#include <pthread.h> 

#define NUM_THREADS 5 

struct my_thread_info { 
    long which; 
}; 

int SharedVariable = 0; 

void *SimpleThread(void *data) 
{ 
    int num, val; 
    struct my_thread_info *info = data; 

    for (num = 0; num < 20; num++) { 
     if (random() > RAND_MAX/2) 
      usleep(10); 
     val = SharedVariable; 
     printf("*** thread %ld sees value %d\n", info->which, val); 
     SharedVariable = val + 1; 
    } 

    val = SharedVariable; 
    printf("Thread %ld sees final value %d\n", info->which, val); 

    free(info); 
    return NULL; 
} 

int main(int argc, char *argv[]) 
{ 
    pthread_t threads[NUM_THREADS]; 
    int rc; 
    long t; 
    struct my_thread_info *info; 
    for (t = 0; t < NUM_THREADS; t++) { 
     printf("In main: creating thread %ld\n", t); 

     info = malloc(sizeof(struct my_thread_info)); 
     info->which = t; 

     rc = pthread_create(&threads[t], NULL, SimpleThread, info); 
     if (rc) { 
      printf("ERROR; return code from pthread_create() is %d\n", rc); 
      exit(-1); 
     } 
    } 

    for (t = 0; t < NUM_THREADS; t++) { 
     pthread_join(threads[t], NULL); 
    } 
} 
+0

यह काम करता है? आपके SimpleTread फ़ंक्शन में कोई वापसी नहीं है। – IanNorton

+0

आश्चर्यजनक रूप से, हाँ। हालांकि मैं इसे जोड़ दूंगा। –

+0

नहीं, यह मेरी सरल थ्रेड विधि को बहुत अधिक बदल देता है। हालांकि धन्यवाद! – randomizertech

संबंधित मुद्दे