linux 学习- 编程基础之多线程编程(创建多线程)

#include<pthread.h>

 

 

int pthread_create (*tidp,const pthread_attr_t*attr,void*(*start_rtn)(void),void *arg)

tidp:  线程ID

 

attr: 线程属性,通常为空

 

start_rtn:  要执行的函数

 

arg: start_rtn 的参数

 

编译:   -lpthread

 

#gcc filename -lpthread

 

 

 

 

 

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

 

 

 

执行:  gcc thread_create.c -lpthread -o thread_create

 

 -lpthread   指明库,    如线程库,数学库需要额外指明

##############################

传递整形数

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void *create(void *arg)        //#################void * 指针  转化成整形指针   取出值   如直接传整形出错的
{
    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);   // #####attr 转成void*型指针 返回到函数create

    if(error)
        {
        printf("pthread_create is created is not created ... /n");
        return -1;
        }
    sleep(1);
    printf("pthread_create is created .../n");
    return 0;       
}

运行: gcc thread_int.c -lpthread -o thread_int

结果:   create parameter is  4

#########################################################################

传字符串

#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);  //   同上 转为void *   到函数再次转成char *

    if(error!=0)
    {
        printf("pthread is not created./n");
        return -1;
    }
    sleep(1);
    printf("pthread is created... /n");
    return 0;
}   

运行:  gcc thread_string.c -lpthead -o thread_string

结果: The parameter passed from main function is  zieckey

############################################################################

 

退出: 

 

 

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

void *create(void *arg)
{
    printf("new thread is created ... /n");
    return (void *)8;                                   //######用return   进程继续执行语句,

   //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);  //   ??  考虑能否执行此语句  该句属于进程语句temp=8
    return 0;
}

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值