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

相关推荐
HY小海1 分钟前
【C++】二叉搜索树
开发语言·数据结构·c++
sali-tec22 分钟前
C# 基于halcon的视觉工作流-章38-单位转换
开发语言·人工智能·数码相机·算法·计算机视觉·c#
“愿你如星辰如月”1 小时前
Linux C缓冲区机制全解析
linux·运维·服务器·c语言·vscode
ss2731 小时前
手写MyBatis第78弹:装饰器模式在MyBatis二级缓存中的应用:从LRU到防击穿的全方案实现
java·开发语言
AI+程序员在路上1 小时前
QT中QStackedWidget控件功能及应用
开发语言·qt
轩情吖1 小时前
Qt常用控件之QTextEdit
开发语言·c++·qt·信号·qtextedit·多行输入框·桌面级开发
无敌最俊朗@1 小时前
Qt Model/View/Delegate 架构深度解析
开发语言·qt·架构
xiaoxiao无脸男1 小时前
three.js
开发语言·前端·javascript
hnlgzb2 小时前
安卓中,kotlin如何写app界面?
android·开发语言·kotlin
自信150413057592 小时前
初学者小白复盘11之——指针(1)
c语言