现在的位置: 首页 > 综合 > 正文

1.1 线程基础、创建、终止

2014年02月22日 ⁄ 综合 ⁄ 共 2496字 ⁄ 字号 评论关闭

目录

一. 函数:

1. 比较两个线程ID:

#include <pthread.h>
int pthread_equal(pthread_t tid1, pthread_t tid2); 
Returns: nonzero if equal, 0 otherwise
2. 获取自身线程ID:
#include <pthread.h>
pthread_t pthread_self(void);
Returns: the thread ID of the calling thread
3. 创建线程:
#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
4. 线程终止:
#include <pthread.h>
void pthread_exit(void *rval_ptr);
5. 将线程设置成分离状态、获取退出码:

#include <pthread.h>
int pthread_join(pthread_t thread, void **rval_ptr); 
Returns: 0 if OK, error number on failure
6. 取消同一进程中的其他线程:
#include <pthread.h>
int pthread_cancel(pthread_t tid); 
Returns: 0 if OK, error number on failure
7. 进程清理函数:
#include <pthread.h>
void pthread_cleanup_push(void (*rtn)(void *),void *arg);
void pthread_cleanup_pop(int execute);

8. 使线程进入分离状态:
#include <pthread.h>
int pthread_detach(pthread_t tid); 
Returns: 0 if OK, error number on failure

二. 重点

1.pthread_t 在linux2.4为无符号整型,但在其他类UNIX有可能是一个数据结构。
2. 线程退出方式:
    A 在启动例程中返回.
    B 被同一进程的其他线程取消。
    C pthread_exit
3.线程清理处理程序
   A.pthread_cleanup_push(),pthread_cleanup_pop(int execute)必须成对出现。
   B.pthread_cleanup_pop 总是删除上次pthread_cleanup_push调用建立的清理处理程序。
   C.线程执行以下动作时将调用清理函数(顺序和注册的时候相反)。
      * 调用pthread_exit时。
      * 响应取消请求时。
      * 用非零execute参数调用pthread_cleanup_pop时。

三.例子

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

void cleanup(void *arg)
{
    printf("cleanup: %s\n",(char *)arg);
}

void *thr_fn1(void *arg)
{
    printf("thr_fn1 create,%lu\n",pthread_self());
    printf("msg:%s\n",(char *)arg);
    return ((void *)1);
}

void *thr_fn2(void *arg)
{
    printf("thr_fn2 create,%lu\n",pthread_self());
    pthread_detach(pthread_self());
    pthread_cleanup_push(cleanup,"1th handler");
    pthread_cleanup_push(cleanup,"2th handler");
    pthread_exit((void *)2);
    pthread_cleanup_pop(0);
    pthread_cleanup_pop(0);
    pthread_exit((void *)3);
}

int main(void)
{
    int ret;
    pthread_t tid1,tid2;
    void *tret;

    ret = pthread_create(&tid1,NULL,thr_fn1,(void *)"new thread1");
    if(ret != 0)
        printf("create thread err:%s\n",strerror(ret));

    ret = pthread_create(&tid2,NULL,thr_fn2,(void *)1);
    if(ret != 0)
        printf("create thread err:%s\n",strerror(ret));

    printf("tid1:%lu, tid2:%lu, equal:%d\n",tid1,tid2,pthread_equal(tid1,tid2));

    ret = pthread_join(tid1,&tret);
    if(ret != 0)
        printf("can't join with thread1:%s\n",strerror(ret));
    printf("thread1 exit code %d\n",(int)tret);

    sleep(1);
    exit(0);
}

运行:
root@ubuntu1:~/11# ./a.out
tid1:3077507952, tid2:3069115248, equal:0
thr_fn1 create,3077507952
msg:new thread1
thread1 exit code 1
thr_fn2 create,3069115248
cleanup: 2th handler
cleanup: 1th handler
root@ubuntu1:~/11#

抱歉!评论已关闭.