www.pudn.com > 5.4-thread.rar > thread.c
/* ****************************************Copyright (c)************************************************** ** Guangzhou Zhiyuan Electronic Co.,LTD. ** graduate school ** http://www.zyinside.com ** **------------------------------------- File Info ------------------------------------------------------ ** File name: thread.c ** Last modified Date: 2005-12-30 ** Last Version: 1.0 ** Descriptions: Demo multi-thread program. **------------------------------------------------------------------------------------------------------ ** Created by: Chenxibing ** Created date: 2005-12-30 ** Version: 1.0 ** Descriptions: Preliminary version. ** **------------------------------------------------------------------------------------------------------ ** Modified by: ** Modified date: ** Version: ** Descriptions: ** ******************************************************************************************************** */ #include#include #include #include #include /* ******************************************************************************************************* ** Function name: main() ** Descriptions : Demo for thread. ** Input : NONE ** Output : NONE ** Created by : Chenxibing ** Created Date : 2005-12-30 **----------------------------------------------------------------------------------------------------- ** Modified by : ** Modified Date: **----------------------------------------------------------------------------------------------------- ******************************************************************************************************* */ int task1(int *cnt) { while(*cnt < 5) { sleep(1); (*cnt)++; printf("task1 cnt = %d.\n", *cnt); } return (*cnt); } int task2(int *cnt) { while(*cnt < 5) { sleep(2); (*cnt)++; printf("task2 cnt = %d.\n", *cnt); } return (*cnt); } int main(int argc, char **argv) { int result; int t1 = 0; int t2 = 0; int rt1, rt2; pthread_t thread1, thread2; /* create the first thread. */ result = pthread_create(&thread1, PTHREAD_CREATE_JOINABLE, (void *)task1, (void *)&t1); if(result) { perror("pthread_create: task1.\n"); exit(EXIT_FAILURE); } /* create the second thread. */ result = pthread_create(&thread2, PTHREAD_CREATE_JOINABLE, (void *)task2, (void *)&t2); if(result) { perror("pthread_create: task2.\n"); exit(EXIT_FAILURE); } pthread_join(thread1, (void *)&rt1); pthread_join(thread2, (void *)&rt2); printf("total %d times.\n", t1+t2); printf("return value of task1: %d.\n", rt1); printf("return value of task2: %d.\n", rt2); exit(EXIT_SUCCESS); }