please i need answer to this problem. Thanks Consider the following code that cr
ID: 3902867 • Letter: P
Question
please i need answer to this problem. Thanks
Consider the following code that creates N threads using the POSIX threading library. The thread function threadFun receives as parameter a thread index (from 0 to N-1).
#define NTHREADS 4 // number of test threads
void* threadFun(void* p) { LINE A // remainder of the function is irrelevant for this question
}
void main() { int i; pthread_t tid[N]; pthread_attr attr;
pthread_attr_init(&attr);
for (i=0; i<NTHREADS; i++)
{ LINE B }
... // remainder of the function is irrelevant for this question
}
What is the right set of statements for LINE A and LINE B that correctly initialize the thread index variable from 0 to N-1,
regardless of the timing of each thread's scheduling?
HINT: watch for memory that can be modified inadvertently by other threads.
a. LINE A: index = *(int*)p; LINE B: pthread_create(&tid[i], &attr, threadFun, &i);
b. LINE A: index = *(int)p; LINE B: pthread_create(&tid[i], &attr, threadFun, &i);
c. LINE A: index = (int)*p; LINE B: pthread_create(&tid[i], &attr, threadFun, (void*)i);
d. LINE A: index = (int)p; LINE B: pthread_create(&tid[i], &attr, threadFun, (void*)i);
Explanation / Answer
If you have any doubts, please give me comment...
Ans: LINE A: index = (int)p; LINE B: pthread_create(&tid[i], &attr, threadFun, (void*)i);
Syntax to create thread:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.