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