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

📄 thrd.c

📁 一本非常经典的书籍阿!
💻 C
字号:
/** * A pthread program illustrating how to * create a simple thread and some of the pthread API * This program implements the summation function where * the summation operation is run as a separate thread. * Usage on Solaris/Linux/Mac OS X: * * gcc thrd.c -lpthread * a.out <number> **/#include <pthread.h>#include <stdio.h>int sum; /* this data is shared by the thread(s) */void *runner(void *param); /* the thread */main(int argc, char *argv[]){pthread_t tid; /* the thread identifier */pthread_attr_t attr; /* set of attributes for the thread */if (argc != 2) {	fprintf(stderr,"usage: a.out <integer value>\n");	exit();}if (atoi(argv[1]) < 0) {	fprintf(stderr,"Argument %d must be non-negative\n",atoi(argv[1]));	exit();}/* get the default attributes */pthread_attr_init(&attr);/* create the thread */pthread_create(&tid,&attr,runner,argv[1]);/* now wait for the thread to exit */pthread_join(tid,NULL);printf("sum = %d\n",sum);}/** * The thread will begin control in this function */void *runner(void *param) {int upper = atoi(param);int i;sum = 0;	if (upper > 0) {		for (i = 1; i <= upper; i++)			sum += i;	}	pthread_exit(0);}

⌨️ 快捷键说明

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