Linux C——多线程的基本概念及API函数

一、线程和进程之间的关系,与进程相比线程有哪些优点?

1.线程与进程之间的关系

①线程是进程的一个执行流,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位。

②一个进程由几个线程组成(拥有很多相对独立的执行流的用户程序共享应用程序的大部分数据结构),线程与同属一个进程的其他的线程共享进程所拥有的全部资源。

"进程——资源分配的最小单位,线程——程序执行的最小单位“

④进程有独立的地址空间,一个进程崩溃后,在保护模式下不会对其它进程产生影响,而线程只是一个进程中的不同执行路径。

⑤线程有自己的堆栈和局部变量,但线程没有单独的地址空间,一个线程死掉就等于整个进程死掉,所以多进程的程序要比多线程的程序健壮,但在进程切换时,耗费资源较大,效率要差一些。但对于一些要求同时进行并且又要共享某些变量的并发操作,只能用线程,不能用进程。

 

2.与进程相比有哪些优点

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

②线程间方便的通信机制。

对不同进程来说,它们具有独立的数据空间,要进行数据的传递只能通过进程间通信的方式进行,这种方式不仅费时,而且很不方便。

线程则不然,由于同一进程下的线程之间共享数据空间,所以一个线程的数据可以直接为其它线程所用,这不仅快捷,而且方便

③使多CPU系统更加有效。操作系统会保证当线程数不大于CPU数目时,不同的线程运行于不同的CPU.

④改善程序结构.一个既长又复杂的进程可以考虑分为多个线程,成为几个独立或半独立的运行部分,这样的程序会利于理解和修改。

 

二、线程的创建,等待,退出的API函数详解

1.线程的创建pthread_create

函数原型:int pthread_create(pthread_t *tidp,const pthread_attr_t *attr,(void*)(*start_rtn)(void*),void *arg);

参数:tidp:为指向线程标识符的指针。

      attr:用来设置线程属性。

      start_routine:线程的执行函数。

      arg:传入线程执行函数的参数。

返回值:成功 0,出错返回错误码

#include <stdio.h>
#include <pthread.h>
 
void *myThread1(void)
{
    int i;
    for (i=0; i<3; 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<3; 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);
    
    return 0;
}

2.线程等待退出pthread_join

函数原型: int pthread_join(pthread_t thread, void **retval);

参数:hread: 线程标识符,即线程ID,标识唯一线程。

      retval: 用户定义的指针,用来存储被等待线程的返回值。

返回值: 0代表成功。 失败,返回的则是错误号

#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
 
void *thread(void *str)
{
    int i;
    for (i = 0; i < 10; ++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, (void *)(i));
    
    pthread_join(pth, NULL);
    
    printf("123\n");
    for (i = 0; i < 10; ++i)
    {
        sleep(1);
        printf( "This in the main : %d\n" , i );
    }
    
    return 0;
}

3.线程退出pthread_exit

函数原型:void pthread_exit(void* retval)

返回值:调用线程的返回值,可用其他函数如pthread_join来获取

 

查看线程的返回值

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
 
void *create(void *arg)
{
    printf("new thread is created ... \n");
    return (void *)8;
}
 
int main(int argc,char *argv[])
{
    pthread_t tid;
    int error;
    void *temp;
 
    error = pthread_create(&tid, NULL, create, NULL);
    printf("main thread!\n");
    if( error )
    {
        printf("thread is not created ... \n");
        return -1;
    }
    error = pthread_join(tid, &temp);
    if( error )
    {
        printf("thread is not exit ... \n");
        return -2;
    }
    
    printf("thread is exit code %d \n", (int)temp);
    return 0;
}


三、在创建线程时参数如何传递?

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.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=(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;
}


 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值