मैं एक धागा और से बनाने के लिए कोशिश कर रहा हूँ कि मैं क्या याद इस यह करने के लिए सही तरीके से किया जाना चाहिए: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 एनटी) '
मैं सरल थ्रेड फ़ंक्शन को नहीं बदल सकता इसलिए पैरामीटर के प्रकार को बदलना एक विकल्प नहीं है, भले ही मैंने पहले ही कोशिश की है और यह काम नहीं करता है।
मैं क्या गलत कर रहा हूं?
मैंने ऐसा करने की कोशिश की लेकिन फिर जब भी मैं int का उपयोग करता हूं तो मुझे त्रुटियां मिलती हैं जो – randomizertech
@ fgualda87 एक त्वरित एक-टाइमर के रूप में आप 'int'' void * 'के रूप में पास कर सकते हैं - यह सुनिश्चित करने के लिए कि आपका कोड काम करता है। 'Int जो = (int) args जोड़ें; ', और कोड संकलित और चलाया जाना चाहिए। आपको 'स्ट्रक्चर' में 'int' लपेटने पर विचार करना चाहिए, हालांकि। – dasblinkenlight
बिल्कुल सही काम किया! SimpleThread विधि को बदले बिना ऐसा करने का कोई तरीका है? – randomizertech