hello_arg3.c
来自「Pthread lib库完整说明文档」· C语言 代码 · 共 55 行
C
55 行
/****************************************************************************** FILE: hello_arg3.c* DESCRIPTION:* This "hello world" Pthreads program demonstrates an unsafe (incorrect) * way to pass thread arguments at thread creation. In this case, the * argument variable is changed by the main thread as it creates new threads.* AUTHOR: Blaise Barney* LAST REVISED: 04/05/05******************************************************************************/#include <pthread.h>#include <stdio.h>#include <stdlib.h>#define NUM_THREADS 8char *messages[NUM_THREADS];void *PrintHello(void *threadid){ int *id_ptr, taskid; sleep(3); id_ptr = (int *) threadid; taskid = *id_ptr; printf("Thread %d: %s \n", taskid, messages[taskid]); pthread_exit(NULL);}int main(int argc, char *argv[]){ pthread_t threads[NUM_THREADS]; int rc, t; messages[0] = "English: Hello World!"; messages[1] = "French: Bonjour, le monde!"; messages[2] = "Spanish: Hola al mundo"; messages[3] = "Klingon: Nuq neH!"; messages[4] = "German: Guten Tag, Welt!"; messages[5] = "Russian: Zdravstvytye, mir!"; messages[6] = "Japan: Sekai e konnichiwa!"; messages[7] = "Latin: Orbis, te saluto!"; for(t=0;t<NUM_THREADS;t++) { printf("Creating thread %d\n", t); rc = pthread_create(&threads[t], NULL, PrintHello, (void *) &t); if (rc) { printf("ERROR; return code from pthread_create() is %d\n", rc); exit(-1); } } pthread_exit(NULL);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?