#include <stdio.h> #include <stdlib.h> // linux API / pthread #include <unistd.h> #include <pthread.h> struct ThreadParam { int id; int max; }; // 线程入口函数 void* Thread_Main(void* context) { ThreadParam* p = (ThreadParam*) context; for(int i=0; i<p->max; i++) { printf("thread: %d, count: %d\n", p->id, i); sleep(1); } free(p); printf("thread : %d exit . \n"); return NULL; } // int main() { // 创建线程(同时启动线程) pthread_t h1, h2; // 创建第1个线程 ThreadParam* p1 = (ThreadParam*) malloc(sizeof(ThreadParam)); p1->id = 1; p1->max = 10; if(pthread_create(&h1, NULL, Thread_Main, p1) < 0) { printf("failed to create thread!\n"); return -1; } // 创建第2个线程 ThreadParam* p2 = (ThreadParam*) malloc(sizeof(ThreadParam)); p2->id = 2; p2->max = 20; if(pthread_create(&h2, NULL, Thread_Main, p2) < 0) { printf("failed to create thread!\n"); return -1; } getchar(); return 0; }