[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
1. 概述:
该demo主要完成了linux下线程创建, 以及资源回收等操作,相关接口介绍可以参考<<UNIX环境高级编程>>
2. 测试:
/* demo_pthread.c 线程编程demo : 线程创建, 以及资源回收等 pthread并非Linux系统的默认库, 而是POSIX线程库
在Linux中将其作为一个库来使用, 因此加上 -lpthread(或-pthread)以显式链接该库 */ #include <stdio.h> #
include <string.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h>
#include <sys/prctl.h> #define MAX_BUF 64 static void * pthread_fun(void *arg){
/* 线程重命名 */ prctl(PR_SET_NAME, "pthread_fun"); char buf[MAX_BUF]; #if 0 /*
回收线程资源 将非分离的线程设置为分离线程 即通知线程库, 在指定的线程终止时回收线程占用的内存等资源 */ pthread_detach(
pthread_self()); #endif memset(buf, 0x0, sizeof(buf)); memcpy(buf, (char *)arg,
strlen(arg)); /* 线程开始运行 */ printf("pthread start!\n"); printf("pthread id =
%lu\n", pthread_self()); printf("pthread buf = %s\n", buf); sleep(2); /* 线程结束运行
*/ printf("pthread end!\n"); pthread_exit((void *) 0); } int main(int argc, char
**argv){ void * ret; pthread_t pid; char buf[MAX_BUF]; /* 主线程开始运行 */ printf(
"main start!\n"); memset(buf, 0x0, sizeof(buf)); printf("请输入消息\t"); scanf("%s",
buf); /* 创建线程 */ if ((pthread_create(&pid, NULL, pthread_fun, (void*)buf)) != 0)
{ /*执行错误时, 并不修改系统全局变量errno*/ printf("pthread_create err\n"); return -1; } /*
等待线程结束 : 当前线程会处于阻塞状态, 直到被调用的线程结束后, 当前线程才会继续执行 回收线程资源 : 如果被调用的线程是非分离的,
并且没有对该线程使用pthread_join()的话, 该线程结束后并不会释放其内存空间 */ if (pthread_join(pid, &ret) != 0
){ printf("pthread_join err\n"); return -1; } printf("pthread ret = %ld\n", (
long)ret); /* 主线程结束运行 */ printf("main end!\n"); return 0; } #Makefile CC := gcc
INCLUDE= -I /home/demo/include/ LIB = -lpthread all: $(CC) demo_pthread.c $(
INCLUDE) $(LIB) -o demo_pthread -Wall -Werror clean: rm demo_pthread