c assert函数

条件为false,调用abort函数退出进程,在线程中执行也会导致进程退出

//
// Created by gxf on 2020/3/8.
//
#include "linklist.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>

void printHello();

int main() {
    int a = 1;
    int b = 0;
    pthread_t thread;

    pthread_create(&thread, NULL, printHello, NULL);

    pthread_join(thread, NULL);

    printf("before abort
");
    abort();
    assert(b < a);
    printf("hello assert
");


    return 0;
}

void printHello() {
    int k = 1000;
    int i = 0;
    while (1) {
        if (i == 10) {
            assert(k < i);
        }
        i ++;
        printf("hello
");
        sleep(1);
    }
}

  

原文地址:https://www.cnblogs.com/luckygxf/p/12441409.html