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

相关推荐
郑州光合科技余经理33 分钟前
PHP构建:支撑欧美澳市场的同城生活服务平台开发
java·开发语言·数据库·uni-app·php·排序算法·生活
小浣熊熊熊熊熊熊熊丶8 小时前
《Effective Java》第25条:限制源文件为单个顶级类
java·开发语言·effective java
啃火龙果的兔子8 小时前
JDK 安装配置
java·开发语言
星哥说事8 小时前
应用程序监控:Java 与 Web 应用的实践
java·开发语言
等....9 小时前
Miniconda使用
开发语言·python
zfj3219 小时前
go为什么设计成源码依赖,而不是二进制依赖
开发语言·后端·golang
醇氧9 小时前
org.jetbrains.annotations的@Nullable 学习
java·开发语言·学习·intellij-idea
Java&Develop9 小时前
Aes加密 GCM java
java·开发语言·python
weixin_462446239 小时前
使用 Go 实现 SSE 流式推送 + 打字机效果(模拟 Coze Chat)
开发语言·后端·golang
JIngJaneIL9 小时前
基于springboot + vue古城景区管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端