linux 子线程

这里写图片描述

  1. 线程和进程的区别是不同享栈空间,其他的数据都是同享的
  2. 所以在子线程中要转数据到主线程中就要用全局变量
  3. 线程的信息所以栈保存的

这里写图片描述

一,线程的创建和主线程和子线程的通信

1,创建子线程的操作
参数一:子线程id
参数二:事件
参数三:回调函数
参数四:回调函数的参数

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

2,退出子线程的的函数

参数: 传数据到主线程中指针 必须是全局变量
这是因为线程是不同享栈空间的

 void pthread_exit(void *retval);

3,阻塞函数的使用
参数一:子线程id
参数二:子线程传递的数据

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

4,终止线程的操作

int pthread_cancel(pthread_t thread);
 void pthread_testcancel(void);

5,子线程分离的操作函数

 int pthread_detach(pthread_t thread);
线程创建转换数据
/*************************************************************************
    > File Name: pthread_create_join_exit.c
    > Author: songli
    > QQ: 2734030745
    > Mail: 15850774503@163.com
    > CSDN: http://my.csdn.net/Poisx
    > github: https://github.com/chensongpoixs
    > Created Time: Mon 23 Oct 2017 11:54:14 PM CST
 ************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <pthread.h>

typedef struct 
{
    int age;
    char name[64];
}Student;

//子线程中在栈中的数据要定义到全局变量中
Student p;
void *pthread_call(void *args)
{
    //类型转换
    Student  *ptr = (Student *)args;
    printf("age :%d, name:%s\n", ptr->age, ptr->name);

    //子线程转主线程中数据
    p.age = 21;
    strcpy(p.name, "杨艳");
    pthread_exit((void*)&p);
}

int main(int argc, char *argv[])
{
    //线程pid
    pthread_t tpid;

    int ret;

    Student stu;
    stu.age = 23;
    strcpy(stu.name, "陈丽");
    //创建子线程 发送消息到子线程中
    ret = pthread_create(&tpid, NULL, pthread_call, (void *)&stu );
    if (ret != 0)
    {
        printf("create phtread error:%s\n", strerror(ret));
        return -1;
    }


    //接收子线程的数据
    Student *pstr;

    void *ptr;
    //设置阻塞事件
    pthread_join(tpid, &ptr);

    pstr = (Student *)ptr;
    printf("age:%d, name:%s\n", pstr->age, pstr->name);



    return 0;
}

这里写图片描述

子线程的分离的操作
/*************************************************************************
    > File Name: phread_create_petach.c
    > Author: songli
    > QQ: 2734030745
    > Mail: 15850774503@163.com
    > CSDN: http://my.csdn.net/Poisx
    > github: https://github.com/chensongpoixs
    > Created Time: Tue 24 Oct 2017 12:18:17 AM CST
 ************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>

#define PETACH 1
#define NUPETACH 0

void *pthread_call(void *args)
{
    printf("陈丽\n");
    pthread_exit(NULL);
}


int main(int argc, char *argv[])
{
    //子线程
    pthread_t tpid;

    int ret;
    //创建子线程
#if NUPETACH
    ret = pthread_create(&tpid, NULL, pthread_call, NULL);
    //设置子线程和主线程分离
    ret =  pthread_detach(tpid);
#endif

#if PETACH 
    //================      定义petach事件   =========================
    pthread_attr_t atr;
    //参数线程分离
    pthread_attr_init(&atr);
    //设置事件
    pthread_attr_setdetachstate(&atr, PTHREAD_CREATE_DETACHED);
    ret = pthread_create(&tpid, NULL, pthread_call, NULL);
#endif
    if (ret != 0)
    {
        printf("create pthread :%s\n", strerror(ret));
        return -1;
    }

    if (ret != 0)
    {
        printf("create detach :%s\n", strerror(ret));
        return -1;
    }


    //================       现在阻塞事件没有用了       ======
    ret = pthread_join(tpid, NULL);
    if (ret != 0)
    {
        printf("create join :%s\n", strerror(ret));
        return -1;
    }
#if  PETACH
    pthread_attr_destroy(&atr);
#endif 

    return 0;
}

这里写图片描述

二,线程同步的操作

  1. pthread_mutex_init()
  2. pthread_mutex_lock()
  3. pthread_mutex_unlock()
  4. pthread_mutex_destroy()
/*************************************************************************
    > File Name: pthread_create_mutex_lock.c
    > Author: songli
    > QQ: 2734030745
    > Mail: 15850774503@163.com
    > CSDN: http://my.csdn.net/Poisx
    > github: https://github.com/chensongpoixs
    > Created Time: Tue 24 Oct 2017 12:51:15 AM CST
 ************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <pthread.h>

#define LOCK 1
#define UNLOCK 0

#if LOCK
    pthread_mutex_t mutex;
#endif

#define COUNT 1000

int number = 0;

void *pid_A(void *args)
{
    int i, n = 0;
    for (i = 0; i < COUNT; i++)
    {
#if  LOCK 
        //加LOCK操作
        pthread_mutex_lock(&mutex);
#endif
        n = number;
        n++;
        usleep(100);
        number = n;
        printf("A:%d\n", number);
#if  LOCK 
        //nuLOCK操作
        pthread_mutex_unlock(&mutex);
#endif
    }
    pthread_exit(NULL);
}
void *pid_B(void *args)
{
    int i, n = 0;
    for (i = 0; i < COUNT; i++)
    {
#if  LOCK 
        //加LOCK操作
        pthread_mutex_lock(&mutex);
#endif
        n = number;
        n++;
        number = n;
        printf("B:%d\n", number);
#if  LOCK 
        //nLOCK操作
        pthread_mutex_unlock(&mutex);
#endif
    }
    pthread_exit(NULL);
}


int main(int argc, char *argv[])
{
    //子线程id
    pthread_t tpid1, tpid2;
#if LOCK
    //初始化lock
    pthread_mutex_init(&mutex, NULL);
#endif
    int ret;
    //创建子线程
    ret = pthread_create(&tpid1, NULL, pid_A, NULL);
    if (ret != 0)
    {
        printf("create thread :%s\n", strerror(ret));
        return -1;
    }
    ret = pthread_create(&tpid2, NULL, pid_B, NULL);
    if (ret != 0)
    {
        printf("create thread :%s\n", strerror(ret));
        return -1;
    }

    //设置阻塞事件
    pthread_join(tpid1, NULL);
    pthread_join(tpid2, NULL);

#if LOCK
    //销毁lock
    pthread_mutex_destroy(&mutex);
#endif
    return 0;
}

LOCK = 0 的图
这里写图片描述

LOCK = 1 效果图
这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值