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

相关推荐
走过,莫回头9 分钟前
在OpenMP中,#pragma omp的使用
开发语言·openmp
Warren9818 分钟前
Java Collections工具类
java·开发语言·笔记·python·学习·oracle·硬件工程
安庆平.Я21 分钟前
STM32——寄存器映射
c语言·stm32·单片机·嵌入式硬件·电脑
工程师0071 小时前
C#多线程,同步与异步详解
开发语言·c#·多线程·同步·异步编程
xzkyd outpaper1 小时前
Kotlin中Flow
android·开发语言·kotlin
“αβ”1 小时前
线程安全的单例模式
linux·服务器·开发语言·c++·单例模式·操作系统·vim
五行缺弦2 小时前
Java 笔记 serialVersionUID
java·开发语言·笔记
打码农的篮球2 小时前
C++模板
开发语言·c++
小乖兽技术3 小时前
在 .NET 中使用 Base64 时容易踩的坑总结
开发语言·c#·.net
WJ.Polar3 小时前
Python与Mysql
开发语言·数据库·python·mysql