使用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 运行程序,正常运行

相关推荐
leisoo80973 小时前
100GBA股股票数据怎么存ClickHouseRedisMySQLJSON完整对比
大数据·linux·服务器·开发语言·python
不会代码的小猴3 小时前
21. 泛型编程上
开发语言·c++·笔记·算法
捷瑞电子工坊4 小时前
嵌入式秋招面经:5大核心模块高频考题与实战避坑总结
c语言·stm32·嵌入式工程师·rtos·通信协议·嵌入式面试
一只旭宝4 小时前
细讲C加加【9】C++ std::function与std::bind详解|仿函数、绑定器、类成员绑定、占位符、成员偏移指针
开发语言·c++·算法
天空'之城5 小时前
C 语言工业级通用组件手写 30:安全内存拷贝组件
c语言·内存拷贝·工业级组件·安全内存操作
coder!mq6 小时前
说几个常见的语法糖?
java·开发语言·算法
gugucoding6 小时前
38. 【Java】Stream API(下):高级操作与性能
java·开发语言
Lhan.zzZ6 小时前
在 Visual Studio 2022 中打造可扩展的动态链接库模块:从零搭建到原理解析
开发语言·c++·visual studio
LuminousCPP7 小时前
数据结构-双向循环链表
c语言·数据结构·笔记·链表
心平气和量大福大7 小时前
android-实例-蒲公英-更新与安装-5-签名与生成APK
android·java·开发语言