⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 thread.c

📁 嵌入式linux开发应用程序中 多个线程的实例
💻 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 <stdio.h>#include <stdlib.h>#include <unistd.h>#include <sys/ioctl.h>#include <pthread.h>/********************************************************************************************************** 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);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -