使用c的标准库函数创建线程

c 复制代码
#include <stdio.h>
#include <threads.h>
#include <time.h>

int thrd_proc(void * varg){
	 
	// 打印10次
    int times = 10;
	struct timespec ts = {1,0}; // 1秒, 0纳秒
	
	while(times--){
		printf("%s\n",(char *)varg);
		
		// 每隔1秒,打印一次
		thrd_sleep(&ts,0);
	}
}

// 使用c的标准库函数创建线程
int main(void)
{
    thrd_t t1, t2;
	
	printf("hello\n");
	thrd_create(&t1, thrd_proc, "thread 1");
	thrd_create(&t2, thrd_proc, "thread 2");
	
	thrd_join(t1,0);
	thrd_join(t2,0);
	
    return 0;
}

gcc mainthread.c 编译报错。

gcc mainthread.c -lpthread 加上 -lpthread 后,即编译正常.

./a.out 运行程序,正常运行

相关推荐
m0_686041611 分钟前
C++中的适配器模式变体
开发语言·c++·算法
清风~徐~来6 分钟前
【视频点播系统】WebSocketpp 介绍及使用
开发语言
爱吃大芒果17 分钟前
Flutter for OpenHarmony 实战:mango_shop 路由系统的配置与页面跳转逻辑
开发语言·javascript·flutter
学***542319 分钟前
如何轻松避免网络负载过大
开发语言·网络·php
RANCE_atttackkk24 分钟前
Springboot+langchain4j的RAG检索增强生成
java·开发语言·spring boot·后端·spring·ai·ai编程
梵刹古音27 分钟前
【C语言】 格式控制符与输入输出函数
c语言·开发语言·嵌入式
Acrelhuang33 分钟前
工商业用电成本高?安科瑞液冷储能一体机一站式解供能难题-安科瑞黄安南
大数据·开发语言·人工智能·物联网·安全
hello 早上好33 分钟前
03_JVM(Java Virtual Machine)的生命周期
java·开发语言·jvm
沐雪架构师34 分钟前
LangChain 1.0 Agent开发实战指南
开发语言·javascript·langchain
tod11335 分钟前
力扣高频 SQL 50 题阶段总结(四)
开发语言·数据库·sql·算法·leetcode