For more information, see Linux programming and kernel. I wrote a simple multi-threaded program to calculate an area in the thread and then output the result. However, after running it, the void calculate (void * area) function called in my thread does not seem to be executed at all. Very puzzled. The procedure is as follows:
# Include
# Include
# Include
# Include
Void calculate (void * area ){
Printf ("pthread \ n ");
Float r = 2.0;
Sleep (1 );
* (Float *) area = 3.14 * r;
Printf ("pthread area = % f \ n", * (float *) area );
Pthread_exit (NULL );
}
Int main (){
Int ret;
Void * p;
P = malloc (sizeof (float ));
Pthread_t id;
Ret = pthread_create (& id, NULL, (void *) calculate, p );
If (ret! = 0 ){
Printf ("error \ n ");
Exit (1 );
}
If (ret! = 0) {/* note this is important! */
Pthread_join (id, NULL );
Printf ("thread is finished \ n ");
}
Printf ("thread is finished \ n ");
Printf ("area = % f \ n", * (float *) p );
Free (p );
Exit (0 );
}
The output result of this program is thread is finished.
Areas = 0.000000
Please give me some advice. Thank you. This program has made me very depressed (6) m: B
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service