C语言为数组加锁,c语言数据结构应用-数组队列(无锁队列)在多线程中的使用...

本文介绍了如何在C语言中使用无锁队列实现多线程的同步,并通过示例展示了使用线程锁、信号量和条件变量三种方式的性能对比,分析了它们在速度和CPU占用上的差异。
摘要由CSDN通过智能技术生成

一、背景

上篇文章《c语言数据结构实现-数组队列/环形队列》讲述了数组队列的原理与实现,本文编写一个双线程进行速度测试

二、相关知识

多线程编程接口:

1) 创建线程 pthread_create 函数

SYNOPSIS

#include int pthread_create(pthread_t *thread, const pthread_attr_t *attr,

void *(*start_routine) (void *), void *arg);

Compile and link with -pthread.

DESCRIPTION:

The pthread_create() function starts a new thread in the calling process.  The new thread starts execution by invoking start_routine(); arg is passed as the sole argument of start_routine().

RETURN VALUE:

On success, pthread_create() returns 0; on error, it returns an error number, and the contents of *thread are undefined.

2) 设置线程属性 pthread_attr_setdetachstate

SYNOPSIS

#include int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);

Compile and link with -pthread.

DESCRIPTION

The pthread_attr_setdetachstate() function sets the detach state attribute of the thread attributes object referred to by attr to the value specified in detachstate. The detach state attribute determines whether a thread created using the thread attributes object attr will be created in a joinable or a detached state.

RETURN VALUE

On success, these functions return 0; on error, they return a nonzero error

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值