linux线程的使用与实例

由于 pthread 头文件不是系统默认的库 所以在编译时要加上 -lpthread 

GCC编译器这样编译:gcc pthread.c -lpthread -o pthread

这样不会产生错误:对‘pthread_create’未定义的引用

ptherad_create():创建线程                          

函数声明

  int pthread_create(pthread_t *thread,const pthread_attr_t *restrict_attr,void*(*start_rtn)(void*),void *restrict arg);

返回值

  若成功则返回0,否则返回出错编号

参数

  第一个参数为指向线程标识符的指针。

  第二个参数用来设置线程属性,一般设为NULL。

  第三个参数是线程要启动执行的函数的地址。

  最后一个参数是要执行函数的参数。

pthread_join():阻塞至线程结束

函数原型为:

  extern int pthread_join (pthread_t  thread, void **thread_return);

参数:

  第一个参数为被等待的线程标识符

  第二个参数为一个用户定义的指针,它可以用来存储被等待线程的返回值。

注意

    这个函数是一个线程阻塞的函数,调用它的函数将一直等待到被等待的线程结束为止,当函数返回时,被等待线程的资源被收回。如果执行成功,将返回0,如果失败则返回一个错误号。

pthread_exit():线程结束并返回括号内的内容

exit(EXIT_SUCCESS):退出并返回信息“成功”

exit(EXIT_FAILURE):退出,并返回信息“失败”

//"pthread.c"
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<stdlib.h>
#include<pthread.h>


//线程thread_function
void *thread_function(void *arg);

int main()
{
        char message[]="hello world";
        pthread_t a_thread;
        int res;
        void *thread_result;

    //创建线程thread_function
        res = pthread_create(&a_thread,NULL,thread_function,message);
//      res = pthread_create(&a_thread,NULL,thread_function,NULL);
        if(res != 0){
                perror("Thread creat fail");
                exit(EXIT_FAILURE);
        }
        printf("wating for thread to finish...\n");
        res = pthread_join(a_thread,&thread_result);
        if(res != 0){
                perror("pthread join error");
                exit(EXIT_FAILURE);
        }
        printf("Thread join return %s\n",(char *)thread_result);
        printf("Message id now %s\n",message);
        exit(EXIT_SUCCESS);
}

void *thread_function(void *arg)
{
        char *message1;
        message1  = (char*)arg;
        printf("thread_function is running .Argument was %s \n",message1);
        sleep(3);
        strcpy(message1,"Bye !");
        pthread_exit("Thank you for the CPU time");

}


~      

在编译时有警告的程序:

#include<stdlib.h>
#include<stdio.h>
#include<pthread.h>
#include<string.h>
#include<unistd.h>

void *function(void *arg);

int main()
{
        pthread_t a_pthread;
        int tmp;
        void *thread_return;
        char *buf="This is the operate of the pthread\n";
        char *message = "hello world";

        tmp = pthread_create(&a_pthread,NULL,&function,(void*)buf);
        if(tmp != 0 )
        {
                perror("pthread_create faile \n");
                exit(EXIT_FAILURE);
        }
        printf("wating for the pthread ...\n");
        tmp = 1;
        tmp = pthread_join(a_pthread,&thread_return);
        if(tmp != 0)
        {
                perror("pthread_join faile ... \n");
                exit(EXIT_FAILURE);
        }

        printf("pthread_join return : %s \n",(char*)thread_return);
        printf("the message now is :%s\n",message);
        exit(EXIT_SUCCESS);
}

void *function(void *arg)
{
        char *message = (char*)arg;
        char *buf="thank you for the cpu time";
        printf("thread_return is runing ,Arunment was %s\n",message);
        sleep(1);
        strcpy(message,"Bye !");
        pthread_exit((void*)buf);

}

猜你喜欢

转载自blog.csdn.net/cxyzyywoaini/article/details/87483433
今日推荐