线程属性-调度
创建多个线程

取消线程

erhuabushuo posted @ 2012年9月10日 15:30 in C , 1299 阅读
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>

void *thread_function(void *arg);

int main()
{
    int res;
    pthread_t a_thread;
    void *thread_result;

    res = pthread_create(&a_thread, NULL, thread_function, NULL);
    if (res != 0)
    {
        perror("Thread creation failed");
        exit(EXIT_FAILURE);
    }
    sleep(3);
    printf("Canceling thread...\n");
    res = pthread_cancel(a_thread);
    if (res != 0)
    {
        perror("Thread cancelation failed");
        exit(EXIT_FAILURE);
    }
    printf("Waiting for thread to finish...\n");
    res = pthread_join(a_thread, &thread_result);
    if (res != 0)
    {
        perror("Thread join failed");
        exit(EXIT_FAILURE);
    }
    exit(EXIT_SUCCESS);
}

void *thread_function(void *arg)
{
    int i, res;
    res = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
    if (res != 0)
    {
        perror("Thread pthread_setcancelstate failed");
        exit(EXIT_FAILURE);
    }
    res = pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL);
    if (res != 0)
    {
        perror("Thread pthread_setcanceltype failed");
        exit(EXIT_FAILURE);
    }
    printf("thread_function is running\n");
    for (i = 0; i < 10; i++)
    {
        printf("Thread is still running (%d) ...\n", i);
        sleep(1);
    }
    pthread_exit(0);
}

thread_function is running
Thread is still running (0) ...
Thread is still running (1) ...
Thread is still running (2) ...
Canceling thread...
Waiting for thread to finish...
 

 

Gabrielle McGowan 说:
2019年2月17日 21:06

The programmer and all options of the jobs are met for the individuals. The sharing of the tool and uk assignment writing job is marked for the prisons. The range is devoid of the calculations for the students the midst of the elements for the future offers.

12cpfinancialcalcula 说:
2019年6月04日 23:08

<a href="12cpfinancialcalculator.yolasite.com0">12cpfinancialcalculator.yolasite.com</a> it was firstly introduced in 1981, made by Hewlett Packard(HP). There were at least eight hardware revisions of the HP-12C since 1981.HP-12c is HP's best and longest selling product,
the proprietary silicon on sapphire processor's technology has been redesigned to integrate all the circuitry into a single chip (first the 1LQ9, then 1RR2)[1] and to refresh the manufacturing process

Advit Sharma 说:
2019年9月23日 23:03

Fitness is everything for a normal person. Eating well supplementation is a part of the life of the person who wants to stay fit. You have your personal diet plan, workout, and other stuff but then you also need energy

bigshows 说:
2019年11月03日 23:39

Pro Outdoor Services was established to provide high-quality custom landscaping solutions with honest pricing, unmatched communication and unparalleled customer service. Whether you are looking to install a whole new landscape or remodel your existing space, you can trust the experts at Pro Outdoor Services to help you create your own unique outdoor oasis. We will work with you to build the outdoor lifestyle of your dreams so that you can enjoy the beautiful weather that Arizona has to offer.

my subscription boxe 说:
2019年11月13日 22:50

Subscription boxes are a type of boxes which are delivered to the regular customers in order to build goodwill of the brand. They are also a part of the product distribution strategy. As a woman, you should subscribe to these boxes to bless yourself with a new and astonishing box of happiness each month. visit <a href="https://www.diigo.com/item/note/75hqe/cpat?k=f065c1fea812f7a533e277600bb6f6e5">mysubscriptionsboxes</a>

bigo live hack 说:
2019年11月16日 05:07

BIGO LIVE for PC is a video broadcasting Bigo Live Hack application to get enjoy continuous video talk.

bigo live ipvanish i 说:
2019年11月27日 21:32

The Hidden Truth About Ipvanish Review

mobdro apk 说:
2020年1月20日 01:35

Mobdro is safe to use. However, you have to make sure you are downloading the app via the official website. ... The Mobdro app is mainly designed for Android Smartphone and tables, but you can also install Mobdro on Fire Stick or Amazon Fire TV.

wwe supercard cheats 说:
2020年5月05日 06:14

On this article, I will direct you all through on the most proficient method to download, introduce WWE Supercard altered form on your android gadget and still play a similar game disconnected.

NRA CET Exam 2020-21 说:
2020年10月09日 03:26

A typical prelims test will be directed by the administration in 2021 to employ experts for various non-gazetted posts in the focal government. The Common Eligibility Test or CET is chosen to be led by the administration rather than prelims tests for every legislative post.

johnmathew506 说:
2021年1月16日 22:39

Great post. Thanks for sharing


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter