目录
一、线程的概念
线程就是一个正在运行的函数,不同的平台有不同的线程标准,posix 线程是一套标准,而不是实现;
线程标识:pthread_t,不要用%d直接打印,如果想判断线程 id 可以用 pthread_equal 非零表示相等,pthread_self 返回当前线程号;
二、线程的创建
pthread_create(pthread_t*, const pthread_attr_t,void *(func)(void*), void*) 创建线程
创建失败直接返回 errorno ,线程的调度取决于调度器策略
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <string.h>
static void* fun(void *p) {
printf("create thread : %lu\n", (unsigned long)(pthread_self()));
pthread_exit(NULL);
}
int main() {
pthread_t tid;
puts("Begin!");
int err = pthread_create(&tid, NULL, fun, NULL);
if(err) {
fprintf(stderr, "pthread