\ // workers terminate | | join workers with pthread_join() | Master ThreadStarting from an initial thread in main(), we'll create several worker threads. Each of these will print its message while the original thread waits for them to complete. As each one exits, the master thread will join it and destroy the system resources it uses. Finally, the original thread will call exit() and the whole process will terminate.
Here's the hello world program (click here for source):
1: /****************************************************************** 2: * simple.c -- multithreaded "hello world" 3: * 4: * Author: Mark Hays 5: */ 6: 7: /* Linux with glibc: 8: * _REENTRANT to grab thread-safe libraries 9: * _POSIX_SOURCE to get POSIX semantics10: */11: #ifdef __linux__12: # define _REENTRANT13: # define _POSIX_SOURCE14: #endif15: 16: /* Hack for LinuxThreads */17: #ifdef __linux__18: # define _P __P19: #endif20: 21: #include 22: 23: #include /* for strerror() */24: 25: #include 26: 27: #define NTHREADS 428: 29: #define errexit(code,str) \30:
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




