linux下的线程(一)

线程理论基础

优点:

使用多线程的理由:

(1)和进程相比,它是一种非常“节俭”的多任务操作方式。在Linux系统下,启动一个新的进程必须分配给他独立的地址空间,建立众多的数据表来为维护它的代码段、堆栈段和数据段,这是一种“昂贵”的多任务工作方式。

(2)线程间方便的通信机制。对不同进程来说,它们具有独立的数据空间,要进行数据的传递只能通过进程间的通信方式进行,这种方式不仅费时,而且很不方便。线程则不然,由于同一进程下的线程之间共享数据空间,所以一个线程的数据可以直接为其它线程所用,这不仅快捷,而且方便。

 

多线程:

Linux系统下的多线程遵循POSIX线程接口,称为pthread.

编写Linux下的多线程程序,需要使用头文件pthread.h,连接时需要使用库libpthread.a

 

多线程的程序设计

创建线程

#include <pthread.h>

int  pthread_create(pthread_t * tidp, const pthread_attr_t *attr, void *(*start_rtn)(void), void *arg);

tidp : 线程id

attr : 线程属性(通常为空)

start_rth : 线程要执行的函数

arg : start_rtn 的参数

 

编译:

因为pthread的库不是linux系统的库,所以在进行编译的时候要加上 -lpthread

# gcc filename.c  -lpthread   -o  filename

 

线程创建的例子:

 #include <stdio.h>

#include <pthread.h>

 

void *myThread1(void)

{

    int i;

    for (i=0; i<100; i++)

    {

        printf("This is the 1st pthread,created by zieckey.\n");

        sleep(1);//Let this thread to sleep 1 second,and then continue to run

    }

}

 

void *myThread2(void)

{

    int i;

    for (i=0; i<100; i++)

    {

        printf("This is the 2st pthread,created by zieckey.\n");

        sleep(1);

    }

}

 

int main()

{

    int i=0, ret=0;

    pthread_t id1,id2;

   

    /*创建线程1*/

    ret = pthread_create(&id1, NULL, (void*)myThread1, NULL);/*第三个参数:线程要执行的函数*/

    if (ret)

    {

        printf("Create pthread error!\n");

        return 1;

    }

   

    /*创建线程2*/

    ret = pthread_create(&id2, NULL, (void*)myThread2, NULL);

    if (ret)

    {

        printf("Create pthread error!\n");

        return 1;

    }

   

    pthread_join(id1, NULL);

    pthread_join(id2, NULL);

    /*功能:阻塞调用线程,直到指定的线程终止。

                                                                     tid :等待退出的线程idrval_ptr : 线程退出的返回值的指针*/

    return 0;

}

 

线程传整型变量例子:

#include <stdio.h>

#include <pthread.h>

#include <unistd.h>

 

void *create(void *arg)

{

    int *num;

    num=(int *)arg;/*强制转换为一个整型的指针*/

    printf("create parameter is %d \n",*num);/*从整型指针了把整数取出来*/

    return (void *)0;

}

int main(int argc ,char *argv[])

{

    pthread_t tidp;

    int error;

   

    int test=4;

    int *attr=&test;/*把整型变量的地址赋给指针&test因为pthread_create()第四个参数必须是指针*/

   

    error=pthread_create(&tidp,NULL,create,(void *)attr);

 

    if(error)

        {

        printf("pthread_create is created is not created ... \n");

        return -1;

        }

    sleep(1);

    printf("pthread_create is created ...\n");

    return 0;       

}

 同一进程下的线程之间共享数据空间,所以一个线程的数据可以直接为其它线程所用;例程:pthread_share证明了这一点

 

终止线程

(1)                                                                                                                 如果进程中任何一个线程中调用exit_exit,那么整个进程都会终止。(会导致线程退出)

(2)                                                                                                                 线程的正常退出方式有:

A线程从启动例程中返回(return()

B线程可以被另一个进程终止

C线程自己调用pthread_exit函数

 

线程退出:

#include  <pthread.h>

   void pthread_exit(void *rval_ptr)

功能:终止调用线程

tid : 等待退出的线程id

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

 

线程标识:

#include  <pthread.h>

       Pthread_t  pthread_self (void)

功能:

获取调用线程的thread identifier(线程标识符)

#include <stdio.h>

#include <pthread.h>

#include <unistd.h> /*getpid()*/

 

void *create(void *arg)

{

    printf("New thread .... \n");

    printf("This thread's id is %u  \n", (unsigned int)pthread_self());

    printf("The process pid is %d  \n",getpid());

    return (void *)0;

}

 

int main(int argc,char *argv[])

{

    pthread_t tid;

    int error;

 

    printf("Main thread is starting ... \n");

 

    error = pthread_create(&tid, NULL, create, NULL);

 

    if(error)

    {

        printf("thread is not created ... \n");

        return -1;

    }

    printf("The main process's pid is %d  \n",getpid());

    sleep(1);

    return 0;

}

 

等待线程:

#include  <pthread.h>

int pthread_join(pthread_ttid ,  void **rval_ptr)

功能:阻塞调用线程,直到指定的线程终止。

tid : 等待退出的线程id

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

 

注意两点:

(1) 进程创建线程首先是先运行进程的;

(2)   pthread_join(pth,NULL);  //让进程等待,等到指定id线程全部运行完后,才往下运行

注意:线程中调用getpid()获得的还是进程的id;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值