10、linux多线程程序设计

来源:互联网 发布:mac下模拟仿真软件 编辑:程序博客网 时间:2024/06/07 07:15
 

       多线程

         优点

Ø         和进程相比,它是一种节俭的多任务操作方式(与父进程共享)。在linux系统中,启动一个新的进程必须分配给他独立的地址空间,建立众多的数据表来维护他的代码段,堆栈段,数据段,这是一种昂贵的多任务工作方式。

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

     多线程程序作为一种多任务,并发的工作方式,还有如下的优点

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

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

 

      Linux系统下的多线程遵循POSIX线程接口,称为pthread.编写                linux下的多线程程序,需要使用头文件prhread.h,连接时需要使用库                      libpthread.a.。

       创建线程:

           #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:strat_rtn 的参数

                      编译:

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

                            #gcc filename .c –lpthread –o filename

 

 例题1;thread_creat.c

#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.thread_int.c

#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.thread_string.c

#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.thread_share.c

#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);

    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(“a=%d\n”,a);

   

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

    return 0;

}

 

 

            终止进程

如果进程中任何一个线程调用exit或者_exit,那么整个进程都会终止

            线程的正常退出方式有

Ø         线程从启动例程中返回

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

Ø         线程自己调用pthread_exit函数

 

       注意:进程包含线程。

#include<pthread.h>

Void pthread_exit (void *rval_ptr)

功能:

    终止调用线程

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

 

 

例题5(1)thread_exit.c

#include <stdio.h>

#include <pthread.h>

#include <unistd.h>

 

void *create(void *arg)

{

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

    return (void *)8;

    //exit (0);

 

}

 

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;

}

例题5(2)thread_exit.c

#include <stdio.h>

#include <pthread.h>

#include <unistd.h>

 

void *create(void *arg)

{

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

   // return (void *)8;

    exit (0);

 

}

 

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;

}

注意:

1、        全部线程都停止了

2  、    

3、     

4、     没打印。

 

原创粉丝点击