LINUX 多线程编程

来源:互联网 发布:剑网3女神脸数据 编辑:程序博客网 时间:2024/05/22 15:32
  线程(thread)技术早在60年代中就被提出,但真正应用多线程到操作系统中去,是80年代中期,solaris是这方面的佼佼者。传统的Unix也支持线程的概念,但是在一个进程(process)中只允许有一个线程,这样就意味着多进程。现在,多线程技术已经被许多操作系统所支持,包括Windows/NT, Linux。
*为什么有了进程还有引入线程呢?
*使用多线程到底有哪些好处呢?
使用多线程的理由之一是:
和进程相比,它是一种非常“节俭”的多任务操作方式。在Linux系统下,启动一个新的进程必须分配给它独立的地址空间,建立众多的数据表来维护它的代码段、堆栈段和数据段,这是一种“昂贵”的多任务工作方式。
运行于一个进程中的多个线程,他们之间使用相同的地址空间,而且线程间彼此切换所需要的时间也远远小于进程切换所需要的时间。据统计,一个进程的开销大约是一个线程开销的30倍左右。
使用多线程的理由之二是:
线程间方便的通讯机制。对于不同的进程来说,他们具有独立的数据空间,要进行数据的传递只能通过进程通讯的方式进行,这种方式不仅费时,而且很不方便。线程则不然,由于同一进程的线程间共享数据空间,所以一个线程的数据可以直接为其它线程所用,这不仅快捷,而且方便。
除了以上所说的有点外,多线程做为一种多任务并发的工作方式,有如下有点:
*使用CPU系统更加有效。操作系统会保证当线程数不大于CPU数目的时候,不同线程运行在不同的CPU上。
*改善程序结构。一个即长又复杂的进程可以考虑分为多个线程,称为几个独立或者半独立的运行部分,这样的程序利于理解和修改。
多线程:
Linux系统下的多线程遵循POSIX线程接口,称为pthread。编写Linux下的多线程程序,需要使用头文件pthread.h,连接时需要使用库libpthread.a。
1 创建线程:
#include <pthread.h>
int pthread_creat(pthread_t *tidp, const pthread_attr_t *attr, void *(*start_rtn)(void), void *arg)
tidp: 线程id
attr: 线程属性(通常为空)
start_rtn: 线程要执行的函数
arg: start_rtn的参数
因为pthread的库不是linux系统的库,所以在进行编译的时候要加上:
   -lpthread
#gcc filename –lpthread
例1,线程创建:
#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);
   
    return 0;
}
例2,向线程传递参数
#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;
   
    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;       
}
例3:向线程传递字符串
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

void *create(void *arg)
{
    char *name;
    name=(char *)arg;
    printf("The parameter passed from main function is %s  \n",name);
    return (void *)0;
}

int main(int argc, char *argv[])
{
    char *a="zieckey";
    int error;
    pthread_t tidp;

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

    if(error!=0)
    {
        printf("pthread is not created.\n");
        return -1;
    }
    sleep(1);
    printf("pthread is created... \n");
    return 0;
}   
例4:向线程传递结构体
#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;
}
例5:线程共享变量
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

//static int a=4;

int a = 1;

void *create(void *arg)
{
    printf("new pthread ... \n");
    printf("a=%d  \n",a);
    return (void *)0;
}

int main(int argc,char *argv[])
{
    pthread_t tidp;
    int error;
   
    int a=5;

    printf("a = %d\n",a);
   
    //error=pthread_create(&tidp, NULL, create, NULL);

    if(error!=0)
    {
        printf("new thread is not create ... \n");
        return -1;
    }
   
    sleep(1);
   
    printf("new thread is created ... \n");
    return 0;
}
2 终止线程:
如果进程中任何一个线程中调用exit或_exit那么整个进程都会终止。线程的正常退出方式有:
(1) 线程从启动例程中返回
(2) 线程可以被另一个进程终止
(3) 线程自己调用pthread_exit函数
线程退出
#include<pthread.h>
Void pthread_exit(void *rval_ptr)
功能:终止调用线程
rval_ptr:线程退出返回值的指针。
例6:进程退出
#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;
}

原创粉丝点击