I'm practicing a c code on pthreads and there is some code I'm struggling to understand, I'm hoping someone here can give me some help with code explanation. Any help is appreciated, thanks.
01.#include <pthread.h>
02.#include <iostream>
03.#define SIZE 10
04.using namespace std;
05.int array[SIZE];
06.void *countNegativeNumbers(void *arg){...} //Assuming this function works properly
07.void *calAverageNumber(void *arg){...} //Assuming this function works properly
08.void *printReverseOrder(void *arg){...} //Assuming this function works properly
09.int main(){
10. pthread_t id,t1,t2,t3; //add pthreads and declare it here
11. int creatT; //declare the int variable for creat thread later
12. for(int i=0; i<10;i ){
13. cin>>array[i];}
14. creatT=pthread_create(&t1, NULL, countNegativeNumbers, NULL);
15. if(creatT){
16. cout << "ERROR; return code from pthread_create() is " << creatT << endl;
17. return -1;}
18. sleep(5); // busy wait.
19. // same as line 14 to 18 creat threads for t2,t3 and do if statement and busy wait.
20. creatT = pthread_join(t1, &status);
21. if(creatT){
22. cout<<" ERROR; failed to join "<<creatT<<endl;}
23. // same as line 20 to 22 terminate t2 and t3 here.
24. pthread_exit(0);
25. }
Question:
- For lines 11-12, it uses a for loop to collect users' key in values, do we have any other options to do it? I cant use cin>>array[i]; since i is not declare here right?
- Could anyone give me a detailed explanation for code lines 15 to 17? Do I really need those codes here? I think this is some kind of check if the thread is here or not?
- For lines 20 to 22, pthread_join(thread, pointer) since int main has no pointer why I could not use NULL here?
- line 24 pthread_exit(0); what is the 0 stand for?
Thanks for any helps here!
CodePudding user response:
1 - declare the arraywith definite size at line 5 , u also can use any method for getting input but this here are working fine
2 - if u look at manual of pthread create return value section , u will see that it return zero on success and error number otherwise, here is link for ref https://man7.org/linux/man-pages/man3/pthread_create.3.html
3- has same return value as pthread_create also for ref https://man7.org/linux/man-pages/man3/pthread_join.3.html
4- it terminate the thread and release all the resources it was locked by it https://man7.org/linux/man-pages/man3/pthread_exit.3.html
i also recommend using manual in future it will help u have better understanding of the code
CodePudding user response:
For lines 11-12, it uses a for loop to collect users' key in values, do we have any other options to do it? I cant use cin>>array[i]; since i is not declare here right?
I don't see anything wrong with that. i
is defined in for(int i=0; i<10;i ){
, so it is valid inside the loop.
Could anyone give me a detailed explanation for code lines 15 to 17? Do I really need those codes here? I think this is some kind of check if the thread is here or not?
Those lines are checking the return value of pthread_create()
, to make sure the thread was actually created. See the pthread_create()
manual.
For lines 20 to 22, pthread_join(thread, pointer) since int main has no pointer why I could not use NULL here?
pthread_join()
is used to join with the created thread and get its return value once it exits. The pointer argument could be NULL
if you don't care about the return value.
line 24 pthread_exit(0); what is the 0 stand for?
The return value. In this case, it is terminating the main thread and returning 0
. see pthread_exit()
.