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

相关推荐
2301_764441332 小时前
Python构建输入法应用
开发语言·python·算法
咨询QQ276998852 小时前
V-REP小车项目+匹配文档,基于V-REP与MATLAB联合仿真,小车能够完成循迹、避障、走...
开发语言
咩图2 小时前
C#创建AI项目
开发语言·人工智能·c#
豆沙沙包?3 小时前
2025年--Lc293-784. 字母大小写全排列(回溯)--java版
java·开发语言
珑墨3 小时前
【唯一随机数】如何用JavaScript的Set生成唯一的随机数?
开发语言·前端·javascript·ecmascript
周杰伦fans3 小时前
C# - Task 是什么?想象一下你在餐厅点餐
服务器·开发语言·c#
芳草萋萋鹦鹉洲哦3 小时前
【tauri+rust】App会加载白屏,有时显示在左上角显示一小块,如何优化
开发语言·后端·rust
前端世界4 小时前
float 还是 double?用储罐体积计算带你看懂 C 语言浮点数的真实世界坑
java·c语言·开发语言
豐儀麟阁贵4 小时前
8.5在方法中抛出异常
java·开发语言·前端·算法