📄 thcreat.c
字号:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
void pthread1(int *counter);
void pthread2(int *counter);
void total(int counter1, int counter2);
int g1 = 0;
int g2 = 0;
int main(void)
{
pthread_t thid1, thid2;
pthread_create(&thid1, NULL, (void*)pthread1, (void*)&g1);
pthread_create(&thid2, NULL, (void*)pthread2, (void*)&g2);
pthread_join(&thid1, NULL);
pthread_join(&thid2, NULL);
total(g1, g2);
}
void pthread1(int *counter)
{
while(*counter < 5){
(*counter)++;
printf("pthread1 counter %d\n", *counter);
sleep(1);
}
}
void pthread2(int *counter)
{
while(*counter < 5){
(*counter)++;
printf("pthread2 counter %d\n", *counter);
}
}
void total(int counter1, int counter2)
{
printf("the total is %d\n", counter1+counter2);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -