Linux多线程 C/C++系统接口函数

本文介绍了POSIX线程(pthread)的使用,包括线程的创建与退出、线程ID获取、互斥量、读写锁以及信号量的详细操作。这些是多线程编程中的基础概念,对于实现线程间的同步和通信至关重要。通过pthread库,开发者可以有效地管理线程并确保资源的安全访问。
摘要由CSDN通过智能技术生成
  1. 取得线程id

    pthread_t pthread_self(void); //返回线程id
  1. 线程的创建和退出

	// 创建线程
	int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
	                          void *(*start_routine) (void *), void *arg)//退出线程                  
	void pthread_exit(void *retval)// 回收线程 阻塞等待,不能与detach一起用 
	int pthread_join(pthread_t thread, void **retval)// 杀死线程
	int pthread_cancel(pthread_t thread); 
	// 线程分离,不需要自己回收线程了,不能与join一起用
	int pthread_detach(pthread_t thread);
	//判断两个线程id是否相等
	int pthread_equal(pthread_t t1, pthread_t t2); 
  1. 互斥量Mutex(锁)

    /* Initialize a mutex. 总是返回0 */
    int pthread_mutex_init (pthread_mutex_t *__mutex,
                     const pthread_mutexattr_t *__mutexattr)
    /* Destroy a mutex.  */
    int pthread_mutex_destroy (pthread_mutex_t *__mutex)

    /* Try locking a mutex.  如果锁了立即返回*/ 
    int pthread_mutex_trylock (pthread_mutex_t *__mutex)

    /* Lock a mutex.  */
    int pthread_mutex_lock (pthread_mutex_t *__mutex)

    /* Unlock a mutex.  */
    int pthread_mutex_unlock (pthread_mutex_t *__mutex)
    /* 
     * 以上函数成功时返回0 ,否则返回错误号
     */

  1. 读写锁(读共享,写独占,写优先级高,读多写少时可用)

    // 初始化
    int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock,
               const pthread_rwlockattr_t *restrict attr);
    // 便捷初始化
    pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
    // 销毁读写锁,回收资源
    int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
    // 加读锁
    int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
    // 加写锁
    int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
    // 开锁
    int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
    /* 
     * 以上函数皆是成功时返回0 ,否则返回错误号
     */
    
  2. 信号量semaphore

    /*信号量初始化 
     *__sem: 定义的信号量,传出
     * __pshared: 0 代表线程信号量
     *           非0 代表进程信号量
     * __value: 定义信号量的个数
     */
    int sem_init (sem_t *__sem, int __pshared, unsigned int __value)// 释放信号sem_t对象
    int sem_destroy (sem_t *__sem);
    
    // 申请信号量 申请成功 value--
    int sem_wait (sem_t *__sem);
    
    // 释放信号量 value++
    int sem_post (sem_t *__sem):
    
    // 得到当前的sem的value,并放入SVAL中
    int sem_getvalue (sem_t *__restrict __sem, int *__restrict __sval)/* 
     * 以上函数成功时返回0 ,否则返回-1, errno也会被设置
     */
    
    以下函数待更新:
    sem_t *sem_open (const char *__name, int __oflag, ...)/* Close descriptor for named semaphore SEM.  */
    int sem_close (sem_t *__sem) __THROW;
    /* Remove named semaphore NAME.  */
    int sem_unlink (const char *__name) __THROW;
    
    /* Similar to `sem_wait' but wait only until ABSTIME.
     *This function is a cancellation point and therefore not marked *with __THROW.  
     */
    int sem_timedwait (sem_t *__restrict __sem,
               const struct timespec *__restrict __abstime);
    /* Test whether SEM is posted.  */
    int sem_trywait (sem_t *__sem) __THROWNL;
    
    
  3. 线程局部变量(待更新)

    thread_local

  4. 查看当前pthread版本库 (NPTL)

    getconf GNU_LIBPTHREAD_VERSION
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值