⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 thrdcancel.c

📁 基于网络编程的例子
💻 C
字号:
/* * thrdcancel.c - Illustrate thread cancellation */#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <pthread.h>void task1(int *counter);void task2(int *counter);void cleanup(int counter1, int counter2);int g1 = 0;int g2 = 0;int main(int argc, char *argv[]){    pthread_t thrd1, thrd2;    int ret;    /* Create the first thread */    ret = pthread_create(&thrd1, NULL, (void *)task1, (void *)&g1);    if(ret) {        perror("pthread_create: task1");        exit(EXIT_FAILURE);    }    /* Create the second thread */    ret = pthread_create(&thrd2, NULL, (void *)task2, (void *)&g2);    if(ret) {        perror("pthread_create: task2");        exit(EXIT_FAILURE);    }    pthread_join(thrd2, NULL);    pthread_cancel(thrd1); /* Cancel the first thread */    pthread_join(thrd1, NULL);        cleanup(g1, g2);    exit(EXIT_SUCCESS);}void task1(int *counter){    /* Disable thread cancellation */    pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);    while(*counter < 5) {        printf("task1 count: %d\n", *counter);        (*counter)++;        sleep(1);    }    /* Enable thread cancellation */    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);}void task2(int *counter){    while(*counter < 5) {        printf("task2 count: %d\n", *counter);        (*counter)++;    }}void cleanup(int counter1, int counter2){    printf("total iterations: %d\n", counter1 + counter2);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -