📄 mt.c
字号:
#include <stdio.h> /* standard I/O routines */#include <pthread.h> /* pthread functions and data structures */#include <strings.h>#include <stdlib.h>#include <unistd.h>void* do_computing(void *data){ int j,sum; j=*((int *)data); for(sum=0;j<=100;j++){ sum=sum+j; } printf("Sum is %d\n",sum); pthread_exit(0);}void* do_leak(void *data){ int i; char *p; for(i=0;i<20;i++){ if(i==16){ p=(char*)malloc(20*sizeof(char)); if(p==NULL) exit(-1); else{ memset(p,0,20); strcpy(p,"Hello Sun Studio"); printf("%s\n",p); } } } pthread_exit(0);}intmain(int argc, char* argv[]){ int num; int thr_id1; /* thread ID for the first new thread */ int thr_id2; /* thread ID for the second new thread */ pthread_t p_thread1; /* first thread's structure */ pthread_t p_thread2; /* second thread's structure */ num=1; /* create a new thread that will execute 'do_computing()'*/ thr_id1 = pthread_create(&p_thread1, NULL, do_computing, (void*)&num); /* create a second thread that will execute 'do_leak()' */ thr_id2 = pthread_create(&p_thread2, NULL, do_leak, NULL); pthread_join(p_thread1,NULL); pthread_join(p_thread2,NULL); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -