in Education by
This question already has an answer here: Pthread_create() incorrect start routine parameter passing (1 answer) Closed 3 years ago. I tried to build a program which should create threads and assign a Print function to each one of them, while the main process should use printf function directly. Firstly, I made it without any synchronization means and expected to get a randomized output. Later I tried to add a mutex to the Print function which was assigned to the threads and expected to get a chronological output but it seems like the mutex had no effect about the output. Should I use a mutex on the printf function in the main process as well? Thanks in advance My code: #include #include #include pthread_t threadID[20]; pthread_mutex_t lock; void* Print(void* _num); int main(void) { int num = 20, indx = 0, k = 0; if (pthread_mutex_init(&lock, NULL)) { perror("err pthread_mutex_init\n"); return errno; } for (; indx < num; ++indx) { if (pthread_create(&threadID[indx], NULL, Print, &indx)) { perror("err pthread_create\n"); return errno; } } for (; k < num; ++k) { printf("%d from main\n", k); } indx = 0; for (; indx < num; ++indx) { if (pthread_join(threadID[indx], NULL)) { perror("err pthread_join\n"); return errno; } } pthread_mutex_destroy(&lock); return 0; } void* Print(void* _indx) { pthread_mutex_lock(&lock); printf("%d from thread\n", *(int*)_indx); pthread_mutex_unlock(&lock); return NULL; } JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
All questions of program bugs notwithstanding, pthreads mutexes provide only mutual exclusion, not any guarantee of scheduling order. This is typical of mutex implementations. Similarly, pthread_create() only creates and starts threads; it does not make any guarantee about scheduling order, such as would justify an assumption that the threads reach the pthread_mutex_lock() call in the same order that they were created. Overall, if you want to order thread activities based on some characteristic of the threads, then you have to manage that yourself. You need to maintain a sense of which thread's turn it is, and provide a mechanism sufficient to make a thread notice when it's turn arrives. In some circumstances, with some care, you can do this by using semaphores instead of mutexes. The more general solution, however, is to use a condition variable together with your mutex, and some shared variable that serves as to indicate who's turn it currently is.

Related questions

0 votes
    This question already has an answer here: Pthread_create() incorrect start routine parameter passing (1 answer) ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 19, 2022 in Education by JackTerrance
0 votes
    This question already has an answer here: Pthread_create() incorrect start routine parameter passing (1 answer) ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    I am investigating the design of a work queue processor where the QueueProcessor retrieves a Command Pattern object ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 19, 2022 in Education by JackTerrance
0 votes
    What is multithreaded programming? (a) It's a process in which two different processes run simultaneously (b ... Multithreading of Java Select the correct answer from above options...
asked Mar 1, 2022 in Education by JackTerrance
0 votes
    What is multithreaded programming? (a) It's a process in which two different processes run simultaneously (b ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
0 votes
    With unity coupling, the mutual inductance will be (a) L1 x L2 (b) L1/L2 (c) √( ... theory proposed by,electromagnetic theory engineering physics,electromagnetic theory nptel...
asked Nov 6, 2021 in Education by JackTerrance
0 votes
    The maximum value of mutual inductance of 2 inductively coupled coils with self inductance LA = 49 mH and LB = 81 ... GATE EC Exam, Network Theory MCQ (Multiple Choice Questions)...
asked Oct 17, 2021 in Education by JackTerrance
0 votes
    I'm building a C++/MFC program in a multilingual environment. I have one main (national) language ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 25, 2022 in Education by JackTerrance
0 votes
    I'm building a C++/MFC program in a multilingual environment. I have one main (national) language ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 24, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Closed 10 years ago. Possible Duplicate: GNU C++ how to check ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    This question already has answers here: Closed 10 years ago. Possible Duplicate: GNU C++ how to check ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    Mutual trust between the sender and the receiver helps in reducing…………………………… errors. Select the correct answer from above options...
asked Dec 24, 2021 in Education by JackTerrance
...