POSIX thread (1)

Source: Internet
Author: User

When a new thread is created in the process, the new execution thread will own its own stack (because it also has its own local variables), but share the local variables with its creator.

, File descriptor, signal processing function, and current directory status.

#include<pthread.h>int pthread_creat(pthread_t *thread , pthread_attr_t *attr , void *(*start_routine)(void *) , void *arg);

This function definition looks quite complex and is actually quite simple to use. The first parameter is a pointer to pthread_t data. When a thread is created, this Pointer Points

An identifier is written into the variable.

The second parameter is used to set the attributes of a thread. We generally do not need special properties, so we only need to set the parameter to null.

The last two parameters tell us the function to be started by the thread and the parameter passed to the function.

Void * (* start_routine) (void *)

The above line tells us that we must pass a function address. This function takes a pointer to void as the parameter and returns a pointer to void.

# Include <pthread. h>

Void pthread_exit (void * retval );

The thread calls the pthread_exit function to terminate the execution, just as the process calls the exit function at the end. This function terminates the thread that calls it and returns a point

Pointer of an object.

# Include <pthread. h>

Int pthread_join (pyhread_t th, void ** thread_return );

The pthread_join function is a wait function that uses sub-process information in equivalent processes. The first parameter specifies the thread to wait. The thread passes the pthread_creat

The identifier to be returned. The second parameter is a sincere one. It points to another pointer, while the latter points to the return value of the thread.

#include <stdio.h>#include <unistd.h>#include <stdlib.h>#include <string.h>#include <pthread.h>void *thread_function(void *arg);char message[] = "Hello World";int main() {    int res;    pthread_t a_thread;    void *thread_result;    res = pthread_create(&a_thread, NULL, thread_function, (void *)message);    if (res != 0) {        perror("Thread creation 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);    }    printf("Thread joined, it returned %s\n", (char *)thread_result);    printf("Message is now %s\n", message);    exit(EXIT_SUCCESS);}void *thread_function(void *arg) {    printf("thread_function is running. Argument was %s\n", (char *)arg);    sleep(3);    strcpy(message, "Bye!");    pthread_exit("Thank you for the CPU time");}

CC-d_reentrant-I/usr/include/nptl thread1.c-O thread1-L/use/lib/nptl-lpthread

CC-d_reentrant-I/usr/include/nptl thread1.c-O thread1-lpthread

Cancel a thread

# Include <pthread. h>

Int pthread_cancel (pthread_t thread );

This function is used to send a cancellation request to the thread.

The receiver can use pthread_setcancelstate to set its own cancellation status.

# Include <pthread. h>

Int pthread_setcanclestate (INT state, int * oldstate );

The value of the first parameter can be pthread_cancel_enable, which allows the thread to receive the cancel request or pthread_cancel_disable. Its function is to ignore

Cancel the request. The oldstate pointer is used to obtain the previously canceled state. If you are not interested in it, just pass null to it. If the cancellation request is accepted, the thread can enter the second control.

Level. Use pthread_setcanceltype to set the cancellation type.

# Include <pthread. h>

Int pthread_setcanceltype (INT type, int * oldtype );

The type parameter can have two values: pthread_cancel_asynchronous, which enables immediate action after receiving the cancellation request. The other is

Pthread_cancel_deferred, which will wait until the thread executes one of the following functions after receiving the cancel request. The specific function is pthread_join.

Pthread_wait, pthread_cond_wait, pthread _ cond_timedwait, pthread_testcancel, sem_wait, and sigwait.

#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, j;    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);}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.