linux多线程开发pthread以及遇到的问题总结(1)

#include <pthread.h>

int pthread_create(pthread_t *thread,const pthread_attr_t *attr,void *(*start_routine)(void*),(void*)arg);

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

int pthread_detach(pthread_t thread);

例程一:多个线程使用一个线程函数

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
#include<pthread.h>


#define THREAD_NUM 5
void* thread_running(void* arg)
{
    int *res = (int*)arg;
    while(1)
    {
        printf("thread%d is running\n",*res);
        sleep(1);
    }

    return NULL;
}
int main(void)
{
    pthread_t p[THREAD_NUM];
    int param[THREAD_NUM];
    for(int i=0;i<THREAD_NUM;i++)
    {   
        param[i] = i;
        pthread_create(&p[i],NULL,thread_running,(void*)&param[i]);
    }
    for(int i=0;i<THREAD_NUM;i++)
    {
        pthread_detach(p[i]);
    }
    while(1)
    {
        printf("main thread is running\n");
        sleep(1);
    }

    return 0;
}

执行结果:
在这里插入图片描述

注意:
1.pthread_create传入参数3要转成(void*)同时加上取地址符号&,因为传入的参数是一个void指针,需要的是一个地址而不是一个值,否则会将数值当作指针地址去访问从而出现段错误!
2.对于一次性打开多个线程,如果不希望传入参数是线程共享变量,pthread_create传入参数3的地址不能是同一个变量的,否则多个线程都会去访问同一个地址。
3.pthread_detach()和pthread_join()函数的区别是,pthread_join()会阻塞等待线程运行结束后再回收线程资源,而pthread_detach()会将main函数和线程分离开来,线程结束时,自己会把线程资源释放掉
4.注意线程函数是void* func(void*)类型的,返回值是void*而不是void类型,严谨的写法:
没有返回值:return NULL;
有返回值:return res;(还要注意返回值要是堆区或者全局的数据!!!)
5.执行完线程程序后要进行休眠,以让出执行权限。

例程2:线程间的管道通信

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<string.h>
#include<pthread.h>
#include<errno.h>
#include<malloc.h>

#define BUFSIZE 32
int fd[2];
void* hellopipe(void* arg)
{
    char* temp =(char*)malloc(sizeof(char)*BUFSIZE);
    char* buf = "ping\n";
    write(fd[1],(void*)buf,BUFSIZE);
    usleep(1);
    read(fd[0],(void*)temp,BUFSIZE);
    return temp;
}

void* revpipe(void* arg)
{
    char* temp =(char*)malloc(sizeof(char)*BUFSIZE);
    char* buf = "pong\n";
    read(fd[0],(void*)temp,BUFSIZE);
    usleep(1);
    write(fd[1],(void*)buf,BUFSIZE);
    return temp;
}


int main()
{
    pthread_t pthread1;
    pthread_t pthread2;
    pipe(fd);
    printf("init main\n");

    pthread_create(&pthread1,NULL,hellopipe,NULL);//注意参数1要取地址
    pthread_create(&pthread2,NULL,revpipe,NULL);
    void* buf1;
    void* buf2;

    pthread_join(pthread2,&buf1);
    pthread_join(pthread1,&buf2);
    
    printf("thread2 rev pipe message: %s\n",(char*)buf1);
    printf("thread1 rev pipe message: %s\n",(char*)buf2);
    return 0 ;
}

执行结果:
在这里插入图片描述

pipe函数

#include <unistd.h>
int pipe(int pipefd[2])

pipe函数能够是实现进程和线程间的通信。要初始化一个int fd[2],fd[0]是read pipe ,fd[1]是write pipe

注意:
1.进程间使用时要注意调用write()管道时要调用sleep()或usleep()让出使用权,否则,另一个线程在读管道的值时,将会被阻塞。
2.注意pthread_join的参数2是一个二维指针,main函数中的void*指针当作参数接收线程返回值时要注意取地址

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值