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

相关推荐
CQU_JIAKE12 分钟前
3.21【A】
开发语言·php
今儿敲了吗23 分钟前
python基础学习笔记第九章——模块、包
开发语言·python
xyq202429 分钟前
TypeScript 命名空间
开发语言
2301_8101609532 分钟前
C++与物联网开发
开发语言·c++·算法
sxlishaobin34 分钟前
Java I/O 模型详解:BIO、NIO、AIO
java·开发语言·nio
cm65432036 分钟前
基于C++的操作系统开发
开发语言·c++·算法
ArturiaZ38 分钟前
【day57】
开发语言·c++·算法
wjs202440 分钟前
XML 技术
开发语言
幽灵诶41 分钟前
指针与其运用
c语言
沪漂阿龙43 分钟前
Python 面向对象编程完全指南:从新手到高手的进阶之路
开发语言·python·microsoft