Question

Consider the following C++ code: 7 string animals[5] = {dog, cat, turkey, shark, lion); B 9 void* printword(void* aSample Output 2: Current word: Current word: Current word: Current word: Current word: lionlion Process done! Sample Output 3

c++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE :

#include<iostream>
#include<pthread.h>
#include<string>
using namespace std;


string animals[5] = {"dog", "cat","turkey","shark","lion"};

void *printWord(void *arg ){

        int id = *((int*)arg);
        cout<<"Current word : "<<animals[id] << endl;
        pthread_exit(NULL);

}

int main(){

        pthread_t thread_id[5];
        for(int num = 0; num < 5; num++){

                pthread_create(&thread_id[num],NULL,printWord,(void*)&num);
                pthread_join(thread_id[num],NULL);

        }
        
        cout<<"Process done!\n";
        return 0;

}

Output :

F:\CODES\VS\C\t.exe Current word : dog Current word : cat Current word : turkey Current word : shark Current word : lion Proc

Explanation :

  • In the question, code first creates all the threads and then tries to join them all to the program thread at the end.
  • pthread_join(thread_id[num], NULL): This function will suspend current thread execution until the thread specified (thread_id[num]) execution terminates and then prints the return value from the thread and resumes the current thread.If we want nothing to print we pass NULL.
  • But if we create all the threads at the beginning just like first for loop in the question and try to join all the threads at the end, the current thread is suspended more than one time and is not executed properly. It may also end due to its own join call. This will happen to all the threads since all the threads are created and start executing at all.
  • So the output that is provided is the result due to this logical error.
  • If we try to create a single thread and join it after its creation, the problem could be solved.
  • So, in the single for-loop I have created the thread and then join it to the parent immediately after its creation.
  • This will help to suspend the execution of the other threads if so created after the first one and wait until its execution.
  • Similarly, we can run threads without suspending their execution and waiting until their termination to execute the next thread.
Add a comment
Know the answer?
Add Answer to:
c++ Consider the following C++ code: 7 string animals[5] = {"dog", "cat", "turkey", "shark", "lion"); B...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C Programming - Please Help us! Implementing Load Balancing, the 3 Base Code files are at the bot...

    C Programming - Please Help us! Implementing Load Balancing, the 3 Base Code files are at the bottom: Implementing Load Balancing Summary: In this homework, you will be implementing the main muti-threaded logic for doing batch based server load balancing using mutexes Background In this assignment you will write a batch-based load balancer. Consider a server which handles data proces- sing based on user requests. In general, a server has only a fixed set of hardware resources that it can...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT