linux多线程编程

一 线程

  线程就是“轻量级”的进程,线程与创建它的进程共享代码段和数据段,线程拥有自己独立的栈。

                                                                    ------此处建议参考《计算机操作系统》(第三版)汤小丹

                                                                                                   《从实践中学嵌入式linux应用程序开发》 华清远见嵌入式学院

二 函数学习

三 实例

  
在线程被创建后,就开始运行相关的线程函数,在该函数运行完之后,该线程就退出,这是退出线程的一种方法。另一种退出方法就是调用pthread_exit(),这是线程的主动行为。

/*thread.c*/
#include <stdio.h>  
#include <pthread.h>  
#include <stdlib.h>

#define THREAD_NUMBER 3 /*线程数*/
#define REPEAT_NUMBER 5 /*每个线程中的小任务数*/
#define DELAY_TIME_LEVELS 10.0 /*小任务之间的最大时间间隔*/

void *thrd_func(void *arg)
{
    //线程函数例程
    int thrd_num=(int)arg;
    int delay_time=0;
    int count=0;
    
    printf("thread %d is starting\n",thrd_num);
    
    for(count=0;count<REPEAT_NUMBER;count++)
    {
    	/*RAND_MAX 是 <stdlib.h> 中伪随机数生成函数 rand 所能返回的最大数值*/
        delay_time=(int)(rand()* DELAY_TIME_LEVELS/(RAND_MAX))+1;
        sleep(delay_time);
        
        printf("thread %d: job %d,delay=%d\n",thrd_num,count,delay_time);	
    	   	
    }	
    
    printf("Thread %d finished\n",thrd_num);
    /*******************
    函数原型:void pthread_exit(void *retval)
    函数参数: retval线程结束时的返回值,可由其他函数如pthread_join()获取
    ******************/
    pthread_exit(NULL);	
}



int main()
{
    pthread_t thread[THREAD_NUMBER];
    int no=0,res;
    void *thrd_ret;
    
    /**********************************************
    time()函数
    time_t time(time_t *seconds) 返回自纪元 Epoch(1970-01-01 00:00:00 UTC)起经过的时间,
    以秒为单位。如果 seconds 不为空,则返回值也存储在变量 seconds 中。
    **********************************************/

    srand(time(NULL));
    
    for(no=0;no<THREAD_NUMBER;no++)
    {
    	/*创建多线程*/
    	/******************************
      	函数原型:int pthread_create(pthread_t *thread,
    	                   const pthread_attr_t *attr,
    	                   void *(*start_routine)(void *),
    	                   void *arg)
    	函数参数:thread 线程标示符,即新建线程的id
    	          attr   线程属性,如果没有特殊要求,通常去NULL
    	          start_routine 线程函数的起始地址,
    	                        是一个以指向void的指针作为参数和返回值的函数指针
    	          arg    传递给start_routine的参数
    	返回值: 成功 0
    	         失败 返回错误码
    	备注:编译时必须链接pthread            
    	**********************************/
    	res=pthread_create(&thread[no],NULL,thrd_func,(void*)no);
     	if(res!=0)
     	{
            //创建线程失败
     	    printf("thread %d join failed\n",no);
     	    exit(res);
     	}
    	
    }
    
    printf("creat threads success\n waiting for threads to finish...\n");
    
    for(no=0;no<THREAD_NUMBER;no++)
    {
        //等待线程结束
        /***************
        函数原型:int pthread_join(pthread_t thread,void **retval)
        函数参数: thread 等待线程的标示符
                   retval 用户定义的指针,用来存储被等待线程结束时的返回值(不为NULL时)
        返回值: 成功 0
                 出错 返回错误码
        ***************/
        res=pthread_join(thread[no],&thrd_ret);	
    	if(!res)
    	{
    	    printf("thread %d joined\n",no);	
    	}
    	else
    	{
    	    printf("thread %d joined fail\n",no);	
    		
    	}
    	
    	
    	
    }
    
    return 0;		
}

运行结果:


从运行结果可以看出,每一个线程的运行和结束时无序,独立与并行的。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值