?? child.c
字號(hào):
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
void task1(int *counter);
void task2(int *counter);
void cleanup(int counter1, int counter2);
int g1 = 0;
int g2 = 0;
int pid = 0;
int main(void)
{
pthread_t thrd1 ,thrd2;
int ret;
ret = pthread_create(&thrd1,NULL,(void *)task1,(void *)&g1);
ret = pthread_create(&thrd1,NULL,(void *)task2,(void *)&g2);
pthread_join(thrd2,NULL);
pthread_join(thrd1,NULL);
int status;
waitpid(pid,&status,0);
cleanup(g1,g2);
exit(0);
}
void task1(int *counter)
{ //pid=getpid();
//sleep(1);
while(*counter<5)
{
printf("task1 ciount: %d\n",*counter);
(*counter)++;
// printf("after plus, task1 ciount: %d\n",*counter);
// sleep(2);
// printf("after sleep, task1 ciount: %d\n", *counter);
}
}
void task2(int *counter)
{
while(*counter <5)
{
printf("task2 count: %d\n",*counter);
(*counter)++;
//sleep(1);
}
}
void cleanup(int counter1,int counter2)
{
printf("toal iterations:%d\n",counter1+counter2);
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -