举例一:子线程循环 3 次,接着主线程循环 6 次,接着又回到子线程循环 3 次,接着再回到主线程又循环6 次,如此循环50次,试写出代码。
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
int flag = 0;
int count = 0;
void *function(void *arg)
{
while (1)
{
if (flag == 0)
{
for (int i = 0; i < 3; i++)
{
printf("pthread circ 3 time: %d\n",++count);
if (count == 50)
{
goto sign;
}
}
flag = 1;
}
if(count == 50)
{
sign:
break;
}
}
return NULL;
}
int main()
{
pthread_t pid;
int ret;
void *return_val = NULL;
if ((ret = pthread_create(&pid, NULL, (void *)function, NULL)) != 0)
{
perror("pthread create error!");
exit(EXIT_FAILURE);
}
while (1)
{
sleep(1);
if (flag == 1)
{
for (int i = 0; i < 6; i++)
{
printf("main circ 6 time: %d\n",++count);
if (count == 50)
{
goto sign;
}
}
flag = 0;
}
if (count == 50)
{
sign:
break;
}
}
pthread_join(pid, &return_val);
return 0;
}
举例二:编写一个程序,开启3个线程,这3个线程的ID分别为A、B、C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示;如:ABCABC….依次递推。
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
pthread_mutex_t lock;
pthread_cond_t A;
pthread_cond_t B;
pthread_cond_t C;
int flag = 1;
void *function1(void *arg)
{
for (int i = 0; i < 10; i++)
{
pthread_mutex_lock(&lock);
if (flag != 1)
{
pthread_cond_wait(&A, &lock);
}
//sleep(1);
printf("pthread1 id is: %ld\n", pthread_self());
flag = 2;
pthread_cond_signal(&B);
pthread_mutex_unlock(&lock);
}
return NULL;
}
void *function2(void *arg)
{
for (int i = 0; i < 10; i++)
{
pthread_mutex_lock(&lock);
if(flag != 2)
{
pthread_cond_wait(&B, &lock);
}
//sleep(1);
printf("pthread2 id is: %ld\n", pthread_self());
flag = 3;
pthread_cond_signal(&C);
pthread_mutex_unlock(&lock);
}
return NULL;
}
void *function3(void *arg)
{
for (int i = 0; i < 10; i++)
{
pthread_mutex_lock(&lock);
if (flag != 3)
{
pthread_cond_wait(&C, &lock);
}
//sleep(1);
printf("pthread3 id is: %ld\n", pthread_self());
flag = 1;
pthread_cond_signal(&A);
pthread_mutex_unlock(&lock);
}
return NULL;
}
int main(int argc, char *argv[])
{
pthread_t pid1;
pthread_t pid2;
pthread_t pid3;
int ret;
void *return_val;
pthread_mutex_init(&lock, NULL);
pthread_cond_init(&A, NULL);
pthread_cond_init(&B, NULL);
pthread_cond_init(&C, NULL);
if ((ret = pthread_create(&pid1, NULL, (void *)function1, NULL)) != 0)
{
perror("pthread create error!");
exit(EXIT_FAILURE);
}
pthread_create(&pid2, NULL, (void *)function2, NULL);
pthread_create(&pid3, NULL, (void *)function3, NULL);
if ((ret = pthread_join(pid1, &return_val)) != 0)
{
perror("pthread join error!");
exit(EXIT_FAILURE);
}
pthread_join(pid2, &return_val);
pthread_join(pid3, &return_val);
pthread_mutex_destroy(&lock);
pthread_cond_destroy(&A);
pthread_cond_destroy(&B);
pthread_cond_destroy(&C);
return 0;
}
举例三:
有四个线程1、2、3、4。线程1的功能就是输出1,线程2的功能就是输出2,以此类推.........现在有四个文件ABCD。初始都为空。现要让四个文件呈如下格式:
A:1 2 3 4 1 2....
B:2 3 4 1 2 3....
C:3 4 1 2 3 4....
D:4 1 2 3 4 1....
请设计程序。
将代码中 全局变量 改为 1 和 文件名改为:A.txt ;运行程序得到:A .txt (A:1 2 3 4 1 2....)
将代码中 全局变量 改为 2 和 文件名改为:B.txt ;运行程序得到:B .txt (B:2 3 4 1 2 3....)
以此类推:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
pthread_mutex_t lock;
pthread_cond_t A;
pthread_cond_t B;
pthread_cond_t C;
pthread_cond_t D;
int fd;
int n = 4;
void *thread_func1(void *arg)
{
char k;
for (int i = 0; i < 3; i++)
{
pthread_mutex_lock(&lock);
while(n != 1)
{
pthread_cond_wait(&D,&lock);
}
printf("%d ", n);
fd = open("D.txt", O_CREAT|O_RDWR, S_IRUSR|S_IWUSR);
lseek(fd, 0, SEEK_END);
k = n + '0';
write(fd, &k, sizeof(k));
close(fd);
pthread_cond_signal(&A);
pthread_mutex_unlock(&lock);
n = 2;
}
return NULL;
}
void *thread_func2(void *arg)
{
char k;
for (int i = 0; i < 3; i++)
{
pthread_mutex_lock(&lock);
while(n != 2)
{
pthread_cond_wait(&A,&lock);
}
printf("%d ", n);
fd = open("D.txt", O_CREAT|O_RDWR, S_IRUSR|S_IWUSR);
lseek(fd, 0, SEEK_END);
k = n + '0';
write(fd, &k, sizeof(k));
close(fd);
pthread_cond_signal(&B);
pthread_mutex_unlock(&lock);
n =3;
}
return NULL;
}
void *thread_func3(void *arg)
{
char k;
for (int i = 0; i < 3; i++)
{
pthread_mutex_lock(&lock);
while(n != 3)
{
pthread_cond_wait(&B,&lock);
}
printf("%d ", n);
fd = open("D.txt", O_CREAT|O_RDWR, S_IRUSR|S_IWUSR);
lseek(fd, 0, SEEK_END);
k = n + '0';
write(fd, &k, sizeof(k));
close(fd);
pthread_cond_signal(&C);
pthread_mutex_unlock(&lock);
n = 4;
}
return NULL;
}
void *thread_func4(void *arg)
{
char k;
for (int i = 0; i < 3; i++)
{
pthread_mutex_lock(&lock);
while(n != 4)
{
pthread_cond_wait(&C,&lock);
}
printf("%d ", n);
fd = open("D.txt", O_CREAT|O_RDWR, S_IRUSR|S_IWUSR);
lseek(fd, 0, SEEK_END);
k = n + '0';
write(fd, &k, sizeof(k));
close(fd);
pthread_cond_signal(&D);
pthread_mutex_unlock(&lock);
n = 1;
}
return NULL;
}
int main()
{
pthread_t pid1;
pthread_t pid2;
pthread_t pid3;
pthread_t pid4;
int ret;
int i;
pthread_mutex_init(&lock,NULL);
pthread_cond_init(&A,NULL);
ret = pthread_create(&pid1,NULL,thread_func1,NULL);
if(ret == -1)
{
printf("pthread_create error!\n");
exit(-1);
}
ret = pthread_create(&pid2,NULL,thread_func2,NULL);
ret = pthread_create(&pid3,NULL,thread_func3,NULL);
ret = pthread_create(&pid4,NULL,thread_func4,NULL);
ret = pthread_join(pid1,NULL);
if(ret == -1)
{
printf("pthread_join error!\n");
exit(-1);
}
ret = pthread_join(pid2,NULL);
ret = pthread_join(pid3,NULL);
ret = pthread_join(pid4,NULL);
pthread_mutex_destroy(&lock);
pthread_cond_destroy(&A);
pthread_cond_destroy(&B);
pthread_cond_destroy(&C);
pthread_cond_destroy(&D);
printf("\n");
return 0;
}