栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

Linux C try/catch

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Linux C try/catch

创作人QQ:851301776,邮箱:[lfr890207@163.com](mailto:lfr890207@163.com),欢迎大家一起技术交流,本博客主要是自己学习的心得体会,只为每天进步一点点!

个人座右铭: 1.没有横空出世,只要厚积一定发。 2.你可以学历不高,你可以不上学,但你不能不学习。

 一、try/catch原理

​        try/catch,是一种代码中跳转的思想。主要分为四个点:try、catch/throw、finaliy。核心是:setjmp和longjmp,与我们的栈是一样的都是先进后出,后进先出。

​        与goto的主要区别是:goto只可以在同一个栈上跳转,setjmp/longjmp可以在不同的栈上跳转(不同的函数和不同的线程中跳转)。

​    另外try/catch是线程安全的。

​    此处理函数的栈是覆盖式的

二、setjmp和longjmp介绍 1.函数原型

​        (1)int setjmp(jmp_buf env);

​        (2)void longjmp(jmp_buf env, int val);

 2.说明

​        (1)setjmp:捕获异常

​       (2)longjmp:抛出异常

3.代码演示

```
jmp_buf env;
int count = 0;


void sub_func(int idx) {
    printf("sub_func --> idx:%dn", idx);
    longjmp(env, idx);
}

int main(int argc, char *argv[]) {

    int idx = 0;

    count = setjmp(env);
    if (count == 0) {
        printf("count:%dn", count);
        sub_func(++idx);
    } else if (count == 1) {
        printf("count:%dn", count);
        sub_func(++idx);
    } else if (count == 2) {
        printf("count:%dn", count);
        sub_func(++idx);
    } else if (count == 3) {
        printf("count:%dn", count);
        sub_func(++idx);
    } else {
        printf("other countn");
    }

    return 0;
}
```

4.代码说明

​    刚开始,setjmp设置异常,此时count的值是0,idx的值也是0,sub_func(++idx); 函数抛出异常,并指定定setjmp的返回值是(idx)1,执行longjmp(env, idx);后,代码跳到count = setjmp(env);再次执行。

三、线程私有空间 1.说明

​    线程的私有空间由主进程创建,子线程会继承私有空间。某个子线程写入,其他子线程可以获取其他子线程写入的数据。

​    

2.主要函数

​    1.pthread_key_create(pthread_key_t *key, NULL);  创建线程私有空间

​    2.pthread_key_delete(pthread_key_t key);删除线程私有空间

​    3.pthread_setspecific(pthread_key_t key, void *);向私有空间写入

​    4.pthread_getspecific((pthread_key_t key); 从私有空间读取数据

3.代码演示

```
#define PTHREAD_COUNT        2

pthread_key_t key;
typedef void* (*thread_cb)(void*);

struct pair {
    int x;
    int y;
};

void *thread1_proc(void *arg) {

    struct pair p = {1, 2};
    int i = 0;
    
    while (++i < 100) {
        p.x ++;
        p.y ++;
        pthread_setspecific(key, &p);
        
        struct pair *res = (struct pair *)pthread_getspecific(key);
        printf("%s %s %s x: %d, y: %dn", __FILE__, __LINE__, __func__,res->x, res->y);
    }
}

void *thread2_proc(void *arg) {

    int i = 0;

    while (++i < 100) {
        pthread_setspecific(key, &i);

        int *res = (int *)pthread_getspecific(key);
        printf("res: %dn", *res);
    }
    
}

void *thread3_proc(void *arg) {

    
    int i = 0;
    
    while (++i < 100) {
        
        struct pair *res = (struct pair *)pthread_getspecific(key);
        printf("x: %d, y: %dn", res->x, res->y);
    }
}


int main(int argc, char *argv[]) {

    pthread_key_create(&key, NULL);

    pthread_t thread_id[PTHREAD_COUNT] = {0};
    thread_cb thread_proc[PTHREAD_COUNT] = {
        thread1_proc,
        thread2_proc
    }; 
    
    int i = 0;
    for (i = 0;i < PTHREAD_COUNT;i ++) {
        pthread_create(&thread_id[i], NULL, thread_proc[i], NULL);
    }
    
    for (i = 0;i < PTHREAD_COUNT;i ++) {
        pthread_join(thread_id[i], NULL);
    }

    pthread_key_delete(key);
}
```

 四、多线程try/catch    1.核心点

​            (1)和栈一样,先进后出,后进先出

​            (2)抛出的异常是那个文件,那个行号,那个函数名

​           (3)线程安全的try/catch,setjmp/longjmp

​           (4)线程的私有空间

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/589052.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号