pthread常用函数说明

1.1  pthread_create()

1.1.1 函数原型

#include <pthread.h>

int pthread_create(pthread_t *restrict tidp,
               const pthread_attr_t *restrict attr,
               void *(*start_rtn)(void),
               void *restrict arg);

Returns: 0 if OK, error number on failure

1.1.2 函数功能

linux下用C开发多线程程序,多线程遵循POSIX线程接口,称为pthread该函数的功能是创建一个线程。

1.1.3 传入参数

pthread_t *restrict tidp,由tidp指向的内存单元被设置为新创建线程的线程IDrestrictc99标准引入的,由 restrict 修饰的指针是最初唯一对指针所指向的对象进行存取的方法,即它告诉编译器,所有修改该指针所指向内存中内容的操作都必须通过该指针来修改,而不能通过其它途径(其它变量或指针)来修改,更好的优化代码

const pthread_attr_t *restrict attr,第二个参数用来设置线程属性。

void *(*start_rtn)(void),第三个参数是一个函数指针,即线程运行函数的起始地址。新创建的线程从start_rtn函数的地址开始运行,该函数只有一个万能指针参数arg,如果需要向start_rtn函数传递的参数不止一个,那么需要把这些参数放到一个结构中,然后把这个结构的地址作为arg的参数传入。

void *restrict arg),最后一个参数是运行函数的参数。

1.1.4 返回值

若成功则返回0,否则返回出错编号

1.2 pthread_join()

1.2.1 函数原型

#include <pthread.h>

int pthread_join(pthread_t thread, 

              void **retval); 

Returns: 0 if OK, error number on failure

1.2.2 函数功能

一般情况下,进程中各个线程的运行都是相互独立的,线程的终止并不会通知,也不会影响其他线程,终止的线程所占用的资源也并不会随着线程的终止而得到释放。正如进程之间可以用wait()系统调用来同步终止并释放资源一样,线程之间也有类似机制,那就是pthread_join()函数。

这个函数是一个线程阻塞的函数,调用它的线程将一直等待到被等待的线程结束为止,当函数返回时,被等待线程的资源被收回。一个线程的结束有两种途径,一种是象我们上面的例子一样,函数结束了,调用它的线程也就结束了;另一种方式是通过函数pthread_exit来实现pthread_exitpthread_join的区别在于:使用 pthread_exit() 函数,则仅仅是主线程消亡,进程却不会结束,进程内的其他线程也不会终止,知道所有线程结束,进程才会结束。

1.2.3 传入参数

pthread_t thread, 为被等待的线程标识符,

void **retval为一个用户定义的指针,它可以用来存储被等待线程的返回值。

1.2.4 返回值

若成功则返回0,否则返回出错编号


3.1  具体实例


/*
 * thread_creat.c
 *
 *  Created on: 2012-3-28
 *      Author: dell
 */


#include <pthread.h>
#include <stdio.h>

/*要传递给线程函数的参数*/
struct char_print_parms
{
    char character;    /*要打印的字符*/
    int count;    /*打印字符的次数*/
};

void *char_print (void *parameters)
{
    struct char_print_parms *p = (struct char_print_parms*) parameters;
    int i;

    for(i = 0; i < p->count; i++)
        fputc(p->character, stderr);
    return NULL;
}


int main()
{
    pthread_t pthread1_id;
    pthread_t pthread2_id;

    struct char_print_parms thread1_args;
    struct char_print_parms thread2_args;

    thread1_args.character = 'x';
    thread1_args.count = 30000;
    pthread_create(&pthread1_id, NULL, &char_print, &thread1_args);

    thread1_args.character = 'o';
    thread1_args.count = 20000;
    pthread_create(&pthread2_id, NULL, &char_print, &thread2_args);

    //sleep(1);
    printf("hello world\n");

    pthread_join(pthread1_id,NULL);
    pthread_join(pthread2_id,NULL);
   // pthread_exit(NULL);

    return 0;
}



  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言中,线程相关的常用函数包括: 1. pthread_create ```c int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void *), void *arg); ``` 该函数用于创建一个新的线程,参数说明如下: - thread:指向线程标识符的指针,创建成功后会将线程的标识符返回给调用者。 - attr:指定线程的属性,通常使用默认属性即可,传入NULL。 - start_routine:指向线程函数的指针,线程创建后会从该函数开始执行。 - arg:传递给线程函数的参数。 2. pthread_join ```c int pthread_join(pthread_t thread, void **retval); ``` 该函数用于等待一个线程结束,参数说明如下: - thread:等待的线程标识符。 - retval:指向线程返回值存储位置的指针,可以为NULL,表示不关心线程返回值。 3. pthread_detach ```c int pthread_detach(pthread_t thread); ``` 该函数用于将线程设置为分离状态,使得线程结束时能够自动释放资源,参数说明如下: - thread:需要分离的线程标识符。 4. pthread_exit ```c void pthread_exit(void *retval); ``` 该函数用于线程退出,参数说明如下: - retval:线程的返回值。 5. pthread_cancel ```c int pthread_cancel(pthread_t thread); ``` 该函数用于取消一个线程,参数说明如下: - thread:需要取消的线程标识符。 6. pthread_mutex_init ```c int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr); ``` 该函数用于初始化一个互斥锁,参数说明如下: - mutex:指向互斥锁的指针。 - attr:指定互斥锁的属性,通常使用默认属性即可,传入NULL。 7. pthread_mutex_destroy ```c int pthread_mutex_destroy(pthread_mutex_t *mutex); ``` 该函数用于销毁一个互斥锁,参数说明如下: - mutex:指向互斥锁的指针。 8. pthread_mutex_lock ```c int pthread_mutex_lock(pthread_mutex_t *mutex); ``` 该函数用于加锁一个互斥锁,参数说明如下: - mutex:指向互斥锁的指针。 9. pthread_mutex_unlock ```c int pthread_mutex_unlock(pthread_mutex_t *mutex); ``` 该函数用于解锁一个互斥锁,参数说明如下: - mutex:指向互斥锁的指针。 10. pthread_cond_init ```c int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr); ``` 该函数用于初始化一个条件变量,参数说明如下: - cond:指向条件变量的指针。 - attr:指定条件变量的属性,通常使用默认属性即可,传入NULL。 11. pthread_cond_destroy ```c int pthread_cond_destroy(pthread_cond_t *cond); ``` 该函数用于销毁一个条件变量,参数说明如下: - cond:指向条件变量的指针。 12. pthread_cond_wait ```c int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex); ``` 该函数用于等待一个条件变量,参数说明如下: - cond:指向条件变量的指针。 - mutex:指向互斥锁的指针。 13. pthread_cond_signal ```c int pthread_cond_signal(pthread_cond_t *cond); ``` 该函数用于唤醒一个等待条件变量的线程,参数说明如下: - cond:指向条件变量的指针。 14. pthread_cond_broadcast ```c int pthread_cond_broadcast(pthread_cond_t *cond); ``` 该函数用于广播一个条件变量,唤醒所有等待条件变量的线程,参数说明如下: - cond:指向条件变量的指针。 这些函数在不同的操作系统和编译器中可能有所不同,需要根据具体的使用情况来选择和调用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值