zoukankan      html  css  js  c++  java
  • Linux系统编程——多线程实现多任务

    概述

    每一个进程都拥有自己的数据段、代码段和堆栈段。这就造成进程在进行创建、切换、撤销操作时,须要较大的系统开销。

    为了降低系统开销,从进程中演化出了线程。为了让进程完毕一定的工作。进程必须至少包括一个线程。线程存在于进程中,共享进程的资源。

    很多其它详情。请看《进程和线程的差别与联系


    就像每一个进程都有一个进程号一样。每一个线程也有一个线程号。

    进程号在整个系统中是唯一的,但线程号不同,线程号仅仅在它所属的进程环境中有效。进程号用 pid_t 数据类型表示。是一个非负整数。线程号则用 pthread_t 数据类型来表示,Linux 使用无符号长整数表示。有的系统在实现 pthread_t 的时候,用一个结构体来表示,所以在可移植的操作系统实现不能把它做为整数处理。


    线程的经常使用函数

    1)获取线程号

    所需头文件:

    #include <pthread.h>


    pthread_t pthread_self(void);

    功能:

    获取线程号。

    參数:

    返回值:

    调用线程的线程 ID 。


    2)线程号的比較

    所需头文件:

    #include <pthread.h>


    int pthread_equal(pthread_t t1, pthread_t t2);

    功能:

    推断线程号 t1 和 t2 是否相等。

    为了方便移植。尽量使用函数来比較线程 ID。

    參数:

    t1。t2:待推断的线程号。

    返回值:

    相等:  非 0

    不相等:0


    演示样例代码:

    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    
    int main(int argc, char *argv[])
    {
    	pthread_t thread_id;
    
    	thread_id = pthread_self(); // 返回调用线程的线程ID
    	printf("Thread ID = %lu 
    ",thread_id);
    
    	if( 0 != pthread_equal( thread_id, pthread_self() ) ){
    		printf("Equal!
    ");
    	}else{
    		printf("Not equal!
    ");
    	}
    	
    	return 0;
    }

    线程函数的程序在 pthread 库中。故链接时要加上參数 -lpthread


    执行结果例如以下:



    3)线程的创建

    所需头文件:

    #include <pthread.h>


    int pthread_create( pthread_t *thread,

    const pthread_attr_t *attr,

    void *(*start_routine)(void *),

    void *arg );

    功能:

    创建一个线程。

    參数:

    thread:线程标识符地址。

    attr:线程属性结构体地址。通常设置为 NULL。

    start_routine:线程函数的入口地址。

    arg:传给线程函数的參数。

    返回值:

    成功:0

    失败:非 0


    pthread_create() 创建的线程从指定的回调函数開始执行,该函数执行完后,该线程也就退出了。线程依赖进程存在的。共享进程的资源,假设创建线程的进程结束了。线程也就结束了。


    演示样例一:

    #include <stdio.h>
    #include <unistd.h>
    #include <pthread.h>
    
    int var  = 8;
    
    void *thread_1(void *arg)
    {
    	while(1)
    	{
    		printf("this is my new thread1: var++
    ");
    		var++;
    		sleep(1);
    	}
    	return NULL;
    }
    
    void *thread_2(void * arg)
    {
    	while(1){
    		printf("this is my new thread2: var = %d
    ", var);
    		sleep(1);
    	}
    	
    	return NULL;
    }
    
    int main(int argc, char *argv[])
    {
    	pthread_t tid1,tid2;
    	
    	//创建两个线程
    	pthread_create(&tid1, NULL, thread_1, NULL);  
    	pthread_create(&tid2, NULL, thread_2, NULL);
    	
    	while(1){
    		printf("the main thread: var = %d
    ", var);
    		sleep(1);
    	}
    	
    	return 0;
    }

    执行结果例如以下:



    演示样例二:

    #include <stdio.h>
    #include <unistd.h>
    #include <pthread.h>
    
    // 回调函数
    void *thread_fun(void * arg)
    {
    	sleep(1);
    	int num = *( (int *)arg );
    	printf("int the new thread: num = %d
    ", num);
    	
    	return NULL;
    }
    
    int main(int argc, char *argv[])
    {
    	pthread_t tid;
    	int test = 100;
    	
    	// 创建线程, 把 &test 传给回调函数 thread_fun()
    	pthread_create(&tid, NULL, thread_fun, (void *)&test);  
    
    	while(1);
    	
    	return 0;
    }

    执行结果例如以下:



    4)回收线程资源

    所需头文件:

    #include <pthread.h>


    int pthread_join(pthread_t thread, void **retval);

    功能:

    等待线程结束(此函数会堵塞),并回收线程资源,类似进程的 wait() 函数。假设线程已经结束。那么该函数会马上返回。

    參数:

    thread:被等待的线程号。
    retval:用来存储线程退出状态的指针的地址。

    返回值:

    成功:0

    失败:非 0


    演示样例代码例如以下:

    #include <stdio.h>
    #include <unistd.h>
    #include <pthread.h>
    
    void *thead(void *arg)
    {
    	static int num = 123; //静态变量
    	
    	printf("after 2 seceonds, thread will return
    ");
    	sleep(2);
    	
    	return #
    }
    
    int main(int argc, char *argv[])
    {
    	pthread_t tid;
    	int ret = 0;
    	void *value = NULL;
    	
    	// 创建线程
    	ret = pthread_create(&tid, NULL, thead, NULL);
    	if(ret != 0){ //创建失败
    		perror("pthread_create");
    	}
    	
    	// 等待线程号为 tid 的线程。假设此线程结束就回收其资源
    	// &value保存线程退出的返回值
    	pthread_join(tid, &value); 
    	
    	printf("value = %d
    ", *( (int *)value ) );
    	
    	return 0;
    }

    执行结果例如以下:



    创建一个线程后应回收其资源,但使用 pthread_join() 函数会使调用者堵塞,Linux 还提供了非堵塞函数 pthread_detach() 来回收线程的资源。


    所需头文件:

    #include <pthread.h>


    int pthread_detach(pthread_t thread);

    功能:

    使调用线程与当前进程分离。分离后不代表此线程不依赖与当前进程,线程分离的目的是将线程资源的回收工作交由系统自己主动来完毕。也就是说当被分离的线程结束之后,系统会自己主动回收它的资源。所以,此函数不会堵塞。

    參数:

    thread:线程号。


    返回值:

    成功:0

    失败:非 0


    注意,调用 pthread_detach() 后再调用 pthread_join() , pthread_join() 会立刻返回,调用失败。


    演示样例代码例如以下:

    #include <stdio.h>
    #include <unistd.h>
    #include <pthread.h>
    
    void *thead(void *arg)
    {
    	int i;
    	for(i=0; i<5; i++)
    	{
    		printf("I am runing
    ");
    		sleep(1);
    	}
    	
    	return NULL;
    }
    
    int main(int argc, char *argv[])
    {
    	int ret  = 0;
    	pthread_t tid;
    	
    	ret = pthread_create(&tid, NULL, thead, NULL);
    	if(ret!=0){
    		perror("pthread_create");
    	}
    	
    	pthread_detach(tid); // 线程分离。不堵塞
    	
    	// 立刻返回。调用失败
    	int flag = pthread_join(tid, NULL);
    	if(flag != 0){
    		printf("join not working
    ");
    	}
    	
    	printf("after join
    ");
    	sleep(3);
    	printf("I am leaving
    ");
    	
    	return 0;
    }

    执行结果例如以下:



    5)线程退出

    在进程中我们能够调用 exit() 函数或 _exit() 函数来结束进程。在一个线程中我们能够通过 pthread_exit() 在不终止整个进程的情况下停止它的控制流。


    所需头文件:

    #include <pthread.h>


    void pthread_exit(void *retval);

    功能:

    退出调用线程。一个进程中的多个线程是共享该进程的数据段。因此,通常线程退出后所占用的资源并不会释放。

    參数:

    retval:存储线程退出状态的指针。

    返回值:


    演示样例代码例如以下:

    #include <stdio.h>
    #include <unistd.h>
    #include <pthread.h>
    
    void *thread(void *arg)
    {
    	static int num = 123; //静态变量
    	int i = 0;
    	while(1)
    	{
    		printf("I am runing
    ");
    		sleep(1);
    		i++;
    		if(i==3)
    		{
    			pthread_exit( (void *)&num );
    			// return &num;
    		}
    	}
    	
    	return NULL;
    }
    
    int main(int argc, char *argv[])
    {
    	int ret  = 0;
    	pthread_t tid;
    	void *value  = NULL;
    	
    	ret = pthread_create(&tid, NULL, thread, NULL);  
    	if(ret!=0){
    		perror("pthread_create");
    	}
    	
    	pthread_join(tid, &value);
    	
    	printf("value = %d
    ", *(int *)value );
    	
    	return 0;
    }

    执行结果例如以下:



    本教程演示样例代码下载请点此链接。

  • 相关阅读:
    Atlassian In Action-Jira之核心配置(二)
    mybatis的插入与批量插入的返回ID的原理
    Atlassian In Action-Jira之指导思想(一)
    Atlassian In Action
    如何进行软件技术管理?
    [simple-orm-mybaits]基于Mybatis的ORM封装介绍
    Java利用Mybatis进行数据权限控制
    给大家推荐一个Java学习和面试的GitHub项目
    立志
    一勤天下无难事
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/7081424.html
Copyright © 2011-2022 走看看