1.运行程序,说明该程序的目的。
#include <unistd.h> #include <pthread.h> #include <stdio.h> #define N 2 void *thread(void *vargp); char **ptr; /* 全局变量 */ int main() { int i; pthread_t tid; char *msgs[N] = { "Hello from foo", "Hello from bar" }; ptr = msgs; for (i = 0; i < N;i++) { pthread_create(&tid, NULL, thread, (void *)i); // pthread_join(tid, NULL); } //tid指向新创建线程ID的变量,作为函数的输出 //NULL表示不同线程的属性为默认 //函数指针, 为线程开始执行的函数名 //函数的唯一无类型(void)指针参数, 如要传多个参数, 可以用结构封装 pthread_exit(NULL); } void *thread(void *vargp) { int myid = (int)vargp; static int cnt = 0; printf("[%d]: %s (cnt=%d)\n", myid, ptr[myid], ++cnt); return NULL; } #include <unistd.h> #include <pth