多线程的编程

线程的创建

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

tidp  线程的id

attr:线程的属性,一般为空

start_rtn:线程要执行的函数

arg:start_rtn的参数

编译多线程时要加上  pthread这个库    gcc   filename  -lpthread

线程的终止,线程从启动licheng例程中返回,线程可以被另一个进程终止,线程可以自己调用pthread——exit函数

void   pthread_exit(void* rval_ptr)

rval_ptr  线程退出返回值得指针

int   pthread_join(pthread_t  tid,void**rval_ptr)

阻塞调用线程,直到指定的线程终止

tid  等待退出的线程id

rval_ptr    线程退出的返回值的指针

pthread_t   pthread_self(void)

获取调用线程的id

线程的终止有两种情况,一种是正常的终止,一种是不可预期的终止

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

在这两段之间的的代码中的终止动作(包括调用pthread_exit和异常终止,不包括return)都将执行,

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

rtn  清除函数         arg   清除函数的参数

将清除函数压入栈,

void    pthread_cleanup_pop  (int  execute)

将清除函数弹出栈并决定是否要执行清除函数      execute为1不执行清除,为0执行清除

/**********************************************************
*实验要求:   在程序中创建一个线程,并向该线程处理函数传递一个结构体作为
*           参数。
*功能描述:   通过pthread_create创建一个线程并传入一个结构体参数,再
*           在线程中接收到这个参数并把参数内容打印出来。
**********************************************************/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>

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

/*
 * 线程执行函数
 * */
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;
}

/*
 * 程序入口
 * */
int main(int argc,char *argv[])
{
    pthread_t tidp;
    int error;
    struct menber *b;

	/*为结构体指针b分配内存并赋值*/
    b=(struct menber *)malloc( sizeof(struct menber) );
    b->a = 4;
    b->s = "zieckey";
    
	/*创建线程并运行线程执行函数*/
    error = pthread_create(&tidp, NULL, create, (void *)b);
    if( error )
    {
        printf("phread is not created...\n");
        return -1;
    }

    sleep(1); //进程睡眠一秒使线程执行完后进程才会结束

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

/**********************************************************
*实验要求:   在程序中创建一个线程,进程需等待该线程执行结束后才能继续执行。
*功能描述:   通过pthread_join阻塞等待,直至相应线程结束。
**********************************************************/
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>

/*
* 线程的执行函数
* */
void *thread(void *str)
{
    int i;
    for (i = 0; i < 3; ++i)
    {
        sleep(2);
        printf( "This in the thread : %d\n" , i );
    }
    return NULL;
}


/*
* 程序入口
* */
int main()
{
    pthread_t pth;
    int i;

	/*创建线程并执行线程执行函数*/
    int ret = pthread_create(&pth, NULL, thread, NULL);  
	printf("The main process will be to run,but will be blocked soon\n");	
	/*阻塞等待线程退出*/
    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;
}

/**********************************************************
*实验要求:   在程序中创建一个线程,使用线程API对该线程进行清理工作。
*功能描述:   创建线程,并在其中使用函数pthread_cleanup_push和函数
*           pthread_cleanup_pop,验证这两个清理函数的效果。
**********************************************************/
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

/*
 * 线程清理函数
 * */
void *clean(void *arg)
{
    printf("cleanup :%s\n",(char *)arg);
    return (void *)0;
}

/*
 * 线程1的执行函数
 * */
void *thr_fn1(void *arg)
{
    printf("thread 1 start  \n");
	/*将线程清理函数压入清除栈两次*/
    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); //线程运行到这里会结束,后面的代码不会被运行。由于是用return退出,所以不会执行线程清理函数。
    }
    pthread_cleanup_pop(0);
    pthread_cleanup_pop(0);
    return (void *)1;
}

/*
 * 线程2的执行函数
 * */
void *thr_fn2(void *arg)
{
    printf("thread 2 start  \n");

	/*将线程清理函数压入清除栈两次*/
    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);//线程运行到这里会结束,后面的代码不会被运行。由于是用pthread_exit退出,所以会执行线程清理函数。执行的顺序是先压进栈的后执行,即后进先出。
    }
    pthread_cleanup_pop(0);
    pthread_cleanup_pop(0);
    pthread_exit((void *)2);
}

/*
 * 程序入口
 * */
int main(void)
{
    int err;
    pthread_t tid1,tid2;
    void *tret;

	/*创建线程1并执行线程执行函数*/
    err=pthread_create(&tid1,NULL,thr_fn1,(void *)1);
    if(err!=0)
    {
        printf("error .... \n");
        return -1;
    }
	/*创建线程2并执行线程执行函数*/
    err=pthread_create(&tid2,NULL,thr_fn2,(void *)1);
	if(err!=0)
    {
        printf("error .... \n");
        return -1;
    }

	/*阻塞等待线程1退出,并获取线程1的返回值*/
    err=pthread_join(tid1,&tret);
    if(err!=0)
    {
        printf("error .... \n");
        return -1;
    }
    printf("thread 1 exit code %d  \n",(int)tret);
	/*阻塞等待线程2退出,并获取线程2的返回值*/
    err=pthread_join(tid2,&tret);
    if(err!=0)
    {
        printf("error .... ");
        return -1;
    }
	printf("thread 2 exit code %d  \n",(int)tret);  
    return 1;
}

猜你喜欢

转载自blog.csdn.net/qq_39372207/article/details/82460297