Linux??????????????????
?????pointer_y ???????[ 2017/2/27 13:42:45 ] ?????????????? Linux
???????????
???????????????????????顣
??????????????????????顣
???????????????????????????????????????????????????????????????????????????????????????????????????????????е???????????????????????????????????????????????????????????????????????????????????????????
?????????????л???????????????????????????????????????????????????????????????????????????????????????????????????????????????????CPU???????????????????л??棬??????????????????CPU???
?????????????????£?
?????????????????????
????1.??????????????????????????????????????????????????????????????????????????
????2.???????????????????????????
????3.???????????????????????????????????????????????????????????????????????????????????????????????????д??“1234”????д??12??????????????????????????????????????12??????????????????????????????????????????????????????????????л????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????仹???????????
??????????????
????1.???????????????????
????????????????????????????????????????????????????????????????????PUSH?????????????????????δ?????????POP?????
?????????????
#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
#include<assert.h>
pthread_mutex_t mylock=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t mycond=PTHREAD_COND_INITIALIZER;
typedef struct node
{
int _data;
struct node* _next;
}node??*pnode;
typedef struct Linklist
{
node * phead;
}Linklist??*pLinklist;
pnode creatNode(int data)
{
pnode newnode=(pnode)malloc(sizeof(node));
if(newnode==NULL)
{
perror("malloc");
exit(EXIT_FAILURE);
}
newnode->_data=data;
newnode->_next=NULL;
return newnode;
}
void initList(pLinklist plist)
{
assert(plist);
//pLinklist head=(pLinklist)malloc(sizeof(Linklist));
// plist=head;
plist->phead=NULL;
}
void pushHead(pLinklist list??int data)
{
assert(list);
pnode newnode=creatNode(data);
if(list->phead==NULL)
{
list->phead=newnode;
return;
}
newnode->_next=list->phead;
list->phead=newnode;
}
void popHead(pLinklist list??int* data)
{
assert(list);
if(list->phead==NULL)
{
printf("list empty!
");
return;
}
pnode del=list->phead;
*data=del->_data;
list->phead=del->_next;
del->_next=NULL;
free(del);
}
void destoryList(pLinklist list)
{
assert(list);
if(list->phead!=NULL)
{
pnode cur =list->phead;
while(cur)
{
pnode del=cur;
cur=cur->_next;
free(del);
del=NULL;
}
}
list->phead=NULL;
}
void showList(pLinklist list)
{
assert(list);
pnode cur=list->phead;
while(cur!=NULL)
{
printf("%d "??cur->_data);
cur=cur->_next;
}
printf("
");
}
void* producter_thread(void* arg)
{
pLinklist list=(pLinklist)arg;
while(1)
{
sleep(1);
pthread_mutex_lock(&mylock); //??????????????
pushHead(list??rand()%1000);
pthread_cond_signal(&mycond); //????????????????????????μ????
pthread_mutex_unlock(&mylock); //???????????
printf("producter success %d
"??list->phead->_data);
}
}
void* consumer_thread(void* arg)
{
pLinklist list=(pLinklist)arg;
while(1)
{
sleep(1);
pthread_mutex_lock(&mylock); //????
int data=0;
while(list->phead==NULL)
{
pthread_cond_wait(&mycond??&mylock); //???????????????????????????????
}
popHead(list??&data);
pthread_mutex_unlock(&mylock); //????
printf("consumer success %d
"??data);
}
}
int main()
{
Linklist list;
initList(&list);
pthread_t tid1??tid2;
pthread_create(&tid1??NULL??producter_thread??(void*)&list);//???????
pthread_create(&tid2??NULL??consumer_thread??(void*)&list);
pthread_join(tid1??NULL); //????????????????
pthread_join(tid2??NULL);
destoryList(&list);
return 0;
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11