1.分离线程常用库

把一个线程的属性设置为 detachd 的状态,让系统来回收它的资源,而不再需要在其它线程中对其进行 pthread_join() 操作。

<pthread.h>
pthread_t tid;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_create(&tid, &attr, THREAD_FUNCTION, NULL);

2.如果把一个线程设为分离的线程,一定要在主线程中循环的等待一下新创建的线程执行,否则就会出现,新创建的线程还没执行结束呢,主线程已经执行结束了

3.采用消费者生产者模型,链表为“消费者对象”,一个生产者,两个消费者。

1.头文件定义 “detach.h”

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
#include <semaphore.h>
#include <pthread.h>
typedef struct Nodes
{int num;struct Nodes *next;
} Nodes;

2.函数实现 “detach.c”

#include "detach.h"
void printNode(Nodes *head)
{Nodes *temp = head;if (NULL == head){printf("head is NULL! \n");return;}else{while (temp != NULL){printf("get datas is %d \n", temp->num);temp = temp->next;}}
}

3.主函数实现

#include "detach.h"
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
sem_t sem;Nodes *head = NULL;Nodes *createNodes()
{Nodes *nodes = (Nodes *)malloc(sizeof(Nodes));nodes->next = NULL;return nodes;
}
Nodes *addTail(int i)
{Nodes *tempNode = head;Nodes *newNode = NULL;newNode = createNodes();newNode->num = i;if (NULL == head){head = newNode;return head;}else{while (tempNode->next != NULL){tempNode = tempNode->next;}tempNode->next = newNode;newNode->next = NULL;return newNode;}
}
void deleNode(Nodes *newNode)
{Nodes *temp = NULL;if (NULL == newNode){return;}else if (newNode == head){temp = head->next;free(newNode);head = temp;}else{temp = head->next;free(head);}
}
void printids(const char *s)
{pid_t pid;pthread_t tid;pid = getpid();tid = pthread_self();printf("%s :pid :%d tid :%d\n", s, (unsigned int)pid, (unsigned int)tid);
}
void *thr_product(void *arg)
{int i;int flag;int x;Nodes *newNode = head;printids("start thread_producter!");pthread_mutex_lock(&mutex);for (i = 0; i < 5; i++){newNode = addTail(i);}pthread_cond_signal(&cond);pthread_mutex_unlock(&mutex);
}
void *thr_conmone(void *arg)
{int i = 0;pthread_mutex_lock(&mutex);pthread_cond_wait(&cond, &mutex);printids("thread_consumer_one start!");// sem_post(&sem);// sem_wait(&sem);while (head != NULL){printf("consumer one get node msg:%d\n", head->num);deleNode(head);if (i == 2){pthread_mutex_unlock(&mutex);sem_post(&sem);}sleep(2);i++;}}
void *thr_conmtwo(void *arg)
{sem_wait(&sem);printids("thread_consumer_two start!");while (head != NULL){printf("consumer two get node msg:%d\n", head->num);deleNode(head);}sem_post(&sem);
}
int main(int argc, char **argv)
{int err;pthread_t thr_dh_product;pthread_t thr_dh_conmone;pthread_t thr_dh_conmtwo;pthread_attr_t attr;sem_init(&sem, 0, 0);pthread_attr_init(&attr);pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);err = pthread_create(&thr_dh_product, &attr, thr_product, NULL);if (0 != err){printf("pthread_create failed: %s\n", strerror(err));}else{printf("thr_producter start success!\n");}err = pthread_create(&thr_dh_conmone, &attr, thr_conmone, NULL);if (0 != err){printf("thr_consumer_one failed:%s\n", strerror(err));}else{printf("thr_consumer_one success! \n");}err = pthread_create(&thr_dh_conmtwo, &attr, thr_conmtwo, NULL);if (0 != err){printf("thr_consumer_two failed:%s\n", strerror(err));}else{printf("thr_consumer_two success! \n");}sleep(5);pthread_attr_destroy(&attr);sem_destroy(&sem);return 0;
}

4.关与Makefile的编写

TARGET=test
CC=gcc
INCLUDE=./
LIBS=-lpthread
OBJS=main.o detach.o
${TARGET}:${OBJS}${CC} -g -Wall -o ${TARGET} ${OBJS} ${LIBS}
main.o:main.c${CC} -c main.c
detach.o:detach.c${CC} -c detach.c
.PHONY:clean
clean:rm -f *.o ${TARGET}

5.实验现象

thr_producter start success!
thr_consumer_one success!
start thread_producter! :pid :21274 tid :1363531520
thr_consumer_two success!
thread_consumer_one start! :pid :21274 tid :1355138816
consumer one get node msg:0
consumer one get node msg:1
consumer one get node msg:2
thread_consumer_two start! :pid :21274 tid :1346746112
consumer two get node msg:3
consumer two get node msg:4

Linux detached(分离线程) 消费者和生产者模型相关推荐

  1. java消费者和生产者模型_Java实现简易生产者消费者模型过程解析

    一.概述 一共两个线程,一个线程生产产品,一个线程消费产品,使用同步代码块方法,同步两个线程.当产品没有时,通知生产者生产,生产者生产后,通知消费者消费,并等待消费者消费完. 需要注意的是,有可能出现 ...

  2. 【Linux入门】多线程(线程概念、生产者消费者模型、消息队列、线程池)万字解说

    目录 1️⃣线程概念 什么是线程 线程的优点 线程的缺点 线程异常 线程异常 Linux进程VS线程 2️⃣线程控制 创建线程 获取线程的id 线程终止 等待线程 线程分离 3️⃣线程互斥 进程线程间 ...

  3. 【Linux下】 线程同步 生产者与消费者模型

    文章目录 [Linux下] 线程同步 生产者与消费者模型 线程同步 同步概念与竞态条件 条件变量 条件变量本质 操作条件变量 初始化和销毁条件变量 等待 唤醒 通过条件变量实现的简单线程同步例子 为什 ...

  4. Linux系统编程---17(条件变量及其函数,生产者消费者条件变量模型,生产者与消费者模型(线程安全队列),条件变量优点,信号量及其主要函数,信号量与条件变量的区别,)

    条件变量 条件变量本身不是锁!但它也可以造成线程阻塞.通常与互斥锁配合使用.给多线程提供一个会合的场所. 主要应用函数: pthread_cond_init 函数 pthread_cond_destr ...

  5. 用三个线程实现生产者消费者模型,其中一个线程作为生产者,二个线程作为消费者,生产者随机生产一个时间戳或者字符串,消费者消费这个时间戳,并不能重复消费,并将其打印出来

    题目要求: 用三个线程实现生产者消费者模型,其中一个线程作为生产者,二个线程作为消费者,生产者随机生产一个时间戳或者字符串,消费者消费这个时间戳,并不能重复消费,并将其打印出来.(这是一道百度面试的算 ...

  6. 线程同步之 生产者消费者模型详解

    前言 博主本来没打算讲这个比较前面的知识的(博主socket编程还有两个部分没讲,进程也才写完回收僵尸进程的三种方法,信号捕捉器也才完结),但是今天有朋友来问博主,什么是生产者消费者模型,所以博主就先 ...

  7. linux使用线程实现生产者消费者问题,Linux平台下线程同步,实现“生产者消费者问题”...

    (1)线程同步,实现"生产者消费者问题" 要求:缓冲区大小为20,生产者每次放一个产品,消费者每次取走一个产品:生产者和消费者至少2个. (2)代码如下: #include #in ...

  8. Linux 多线程开发-线程的属性-分离detached和连接joinable

    1.描述 linux的POSIX线程分离状态决定一个线程以什么方式来终止.默认的分离状态是可连接态(joinable),创建线程是参数设置为NULL,就是默认连接态,可以被pthread_join. ...

  9. Linux信号量与互斥锁解决生产者与消费者问题

    先来看什么是生产者消费者问题: 生产者消费者问题(英语:Producer-consumer problem),也称有限缓冲问题(英语:Bounded-buffer problem),是一个多线程同步问 ...

最新文章

  1. Linux curl命令
  2. Java解析excel表格
  3. 八大算法思想(二)------------------递归算法
  4. win7 mysql8.0.11安装教程_mysql8.0.13下载与安装图文教程(示例代码)
  5. python语言的特点强制可读_python程序语言设计第二讲(笔记)
  6. oracle 恢复坏块,Oracle 修复坏块,关掉闪回
  7. Java核心编程实践--视频
  8. Gateway网关系列(二):SpringCloud Gateway入门实战,路由规则
  9. 【ClearCase使用】之图解merge
  10. 暑期开箱评测Wifi Pineapple(大菠萝)
  11. 用python画一个机器猫歌词_手把手 | 用Python语言模型和LSTM做一个Drake饶舌歌词生成器-阿里云开发者社区...
  12. 微信公众号开发工具类
  13. 基于 Next.js实现在线Excel
  14. uniapp里面怎么打开第三方应用
  15. 记录开发错误:ORA-00911: 无效字符
  16. rhel7 pcs pacemaker corosync配置主从高可用
  17. Caffe2 - (十)训练数据集创建
  18. 【XJTUSE计算机图形学】第三章 几何造型技术(1)——参数曲线和曲面
  19. php电影播放系统在线视频点播系统 php毕业设计题目课题选题 php毕业设计项目作品源码(1)功能模块概要
  20. 工具分享:mariadb10.2.7软件,包括linux、windows64位、windows32位,请自行下载(附下载链接)

热门文章

  1. 我的Android进阶之旅------gt;Android自定义View来实现解析lrc歌词并同步滚动、上下拖动、缩放歌词的功能...
  2. 【WEB开发】前端UI开源框架
  3. php阻塞类型,常见PHP堵塞案例
  4. 【PADS_001】【报错“没有可用的未使用开孔”】
  5. CSS弹性布局 Flex属性
  6. Parse error. Expected a command name, got unquoted argument with text ““.
  7. 微信小程序引用iconfont字体资源
  8. Python学习之路33-上下文管理器和else块
  9. 3D建模界扛把子之Maya,技能适用范围详解
  10. 【MFC】计算两个SYSTEMTIME的时间差