Multithreaded programming

Keywords: Attribute

Thread creation

int   pthreat_create(pthread_t*tidp,const  pthread_attr*attr,void  *(*start_rtn)(void),void *arg)

id of tidp thread

attr: attribute of thread, generally empty

Start? RTN: function to be executed by the thread

arg: the parameter of start ˊ RTN

Add pthread to compile multithread. gcc filename - lpthread

The termination of a thread. The thread returns from the starting licheng routine. The thread can be terminated by another process. The thread can call pthread -- exit function by itself

void   pthread_exit(void* rval_ptr)

Pointer to the return value of the rval_ptr thread exit

int   pthread_join(pthread_t  tid,void**rval_ptr)

Blocks the calling thread until the specified thread terminates

tid thread id waiting to exit

Pointer to the return value of the rval_ptr thread exit

pthread_t   pthread_self(void)

Get the id of the calling thread

There are two types of thread termination: normal termination and unexpected termination

pthread_cleanup_push--------------pthread_cleanup_pop

The termination actions in the code between the two sections (including calling pthread_exit and abnormal termination, excluding return) will be executed,

void   pthread_cleanup_push(void(*rtn)(void*),void*arg)

rtn clear function arg parameters of clear function

Push the purge function onto the stack,

void    pthread_cleanup_pop  (int  execute)

Pop the purge function out of the stack and decide whether to execute the purge function. If execute is 1, do not execute the purge, and execute the purge for 0

/**********************************************************
*Experiment requirements: create a thread in the program and pass a structure to the thread processing function as
*           Parameters.
*Function Description: create a thread through pthread "create" and pass in a structure parameter, then
*           This parameter is received in the thread and printed out.
**********************************************************/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>

struct menber
{
    int a;
    char *s;
};

/*
 * Thread execution function
 * */
void *create(void *arg)
{
    struct menber *temp;
    temp=(struct menber *)arg;
    printf("menber->a = %d  \n",temp->a);
    printf("menber->s = %s  \n",temp->s);
    return (void *)0;
}

/*
 * Program entry
 * */
int main(int argc,char *argv[])
{
    pthread_t tidp;
    int error;
    struct menber *b;

	/*Allocate memory and assign values to structure pointer b*/
    b=(struct menber *)malloc( sizeof(struct menber) );
    b->a = 4;
    b->s = "zieckey";
    
	/*Create a thread and run the thread execution function*/
    error = pthread_create(&tidp, NULL, create, (void *)b);
    if( error )
    {
        printf("phread is not created...\n");
        return -1;
    }

    sleep(1); //Process sleep for one second, the process will not end until the thread finishes executing

    printf("pthread is created...\n");
    return 0;
}

/**********************************************************
*Experiment requirements: create a thread in the program, the process needs to wait for the thread to finish execution before continuing.
*Function Description: block and wait through pthread ﹣ join until the end of the corresponding thread.
**********************************************************/
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>

/*
* Thread's execution function
* */
void *thread(void *str)
{
    int i;
    for (i = 0; i < 3; ++i)
    {
        sleep(2);
        printf( "This in the thread : %d\n" , i );
    }
    return NULL;
}


/*
* Program entry
* */
int main()
{
    pthread_t pth;
    int i;

	/*Create thread and execute thread execution function*/
    int ret = pthread_create(&pth, NULL, thread, NULL);  
	printf("The main process will be to run,but will be blocked soon\n");	
	/*Block waiting for thread to exit*/
    pthread_join(pth, NULL);

    printf("thread was exit\n");
    for (i = 0; i < 3; ++i)
    {
        sleep(1);
        printf( "This in the main : %d\n" , i );
    }
    return 0;
}

/**********************************************************
*Experiment requirements: create a thread in the program, and use the thread API to clean up the thread.
*Function Description: create a thread and use the pthread? Cleanup? Push and function in it
*           pthread_cleanup_pop,Verify the effect of these two cleanup functions.
**********************************************************/
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

/*
 * Thread cleanup function
 * */
void *clean(void *arg)
{
    printf("cleanup :%s\n",(char *)arg);
    return (void *)0;
}

/*
 * Execution function for thread 1
 * */
void *thr_fn1(void *arg)
{
    printf("thread 1 start  \n");
	/*Push the thread cleanup function into the cleanup stack twice*/
    pthread_cleanup_push( (void*)clean,"thread 1 first handler");
    pthread_cleanup_push( (void*)clean,"thread 1 second hadler");
    printf("thread 1 push complete  \n");

    if(arg)
    {
        return((void *)1); //The thread will finish running here, and the following code will not be run. Because it exits with return, the thread cleanup function will not be executed.
    }
    pthread_cleanup_pop(0);
    pthread_cleanup_pop(0);
    return (void *)1;
}

/*
 * Execution function for thread 2
 * */
void *thr_fn2(void *arg)
{
    printf("thread 2 start  \n");

	/*Push the thread cleanup function into the cleanup stack twice*/
    pthread_cleanup_push( (void*)clean,"thread 2 first handler");
    pthread_cleanup_push( (void*)clean,"thread 2 second handler");
    printf("thread 2 push complete  \n");
    
	if(arg)
    {
        pthread_exit((void *)2);//The thread will finish running here, and the following code will not be run. Because you exit with pthread_exit, the thread cleanup function is executed. The order of execution is to push the stack first and then execute, that is, last in, first out.
    }
    pthread_cleanup_pop(0);
    pthread_cleanup_pop(0);
    pthread_exit((void *)2);
}

/*
 * Program entry
 * */
int main(void)
{
    int err;
    pthread_t tid1,tid2;
    void *tret;

	/*Create thread 1 and execute the thread execution function*/
    err=pthread_create(&tid1,NULL,thr_fn1,(void *)1);
    if(err!=0)
    {
        printf("error .... \n");
        return -1;
    }
	/*Create thread 2 and execute the thread execution function*/
    err=pthread_create(&tid2,NULL,thr_fn2,(void *)1);
	if(err!=0)
    {
        printf("error .... \n");
        return -1;
    }

	/*Block wait for thread 1 to exit and get the return value of thread 1*/
    err=pthread_join(tid1,&tret);
    if(err!=0)
    {
        printf("error .... \n");
        return -1;
    }
    printf("thread 1 exit code %d  \n",(int)tret);
	/*Block wait for thread 2 to exit and get the return value of thread 2*/
    err=pthread_join(tid2,&tret);
    if(err!=0)
    {
        printf("error .... ");
        return -1;
    }
	printf("thread 2 exit code %d  \n",(int)tret);  
    return 1;
}

 

Posted by anser316 on Fri, 03 Jan 2020 13:12:56 -0800