- #include <pthread.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- void *start_routine(void *arg);
- int main(int argc, char **argv)
- {
- pthread_t tid;
- int code;
- int i;
- for (i = 0; i < 5; i++)
- {
- code = pthread_create(&tid, NULL, start_routine, (void *) i);
- if (code != 0)
- {
- fprintf(stderr, "Create new thread failed: %s\\n", strerror(code));
- exit(1);
- }
- fprintf(stdout, "New thread created.\\n");
- }
- pthread_exit((void *) 0);
- }
- void *start_routine(void *arg)
- {
- int id = (int) arg;
- fprintf(stdout, "%s:%d:%s() running ...\\n", __FILE__, __LINE__, __func__);
- fprintf(stdout, "id = %d\\n", id);
- return ((void *) 0);
- }
- // vim:tabstop=8
- //该片段来自于http://www.codesnippet.cn/detail/111120137046.html
来源: http://www.codesnippet.cn/detail/111120137046.html