#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#define LEFT 30000000
#define RIGHT 30000200
#define THRNUM 4
static int num=0;
static pthread_mutex_t mut_num=PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond_num=PTHREAD_COND_INITIALIZER;
struct thr_arg_st
{
int n;
};
static void *thr_prime(void *p)
{
int i,j,mark;
while(1)
{
pthread_mutex_lock(&mut_num);
while(num == 0)
{
pthread_cond_wait(&cond_num,&mut_num);
/*
pthread_mutex_unlock(&mut_num);
sched_yield();//出让调度器给其他进程
pthread_mutex_lock(&mut_num);
*/
}
if(num == -1)
{
pthread_mutex_unlock(&mut_num);
break;//临界区跳转一定先注意解锁
}
i=num;
num=0;
pthread_cond_broadcast(&cond_num);
pthread_mutex_unlock(&mut_num);
mark=1;
for(j=2;j<i/2;j++)
{
if(i%j==0)
{
mark=0;
break;
}
}
if(mark)
printf("%d %dis a primer\n",(int)p,i);
}
pthread_exit(NULL);//返回值用于free
}
int main()
{
int i,err;
pthread_t tid[THRNUM];
for(i=0;i<THRNUM;i++)
{
err=pthread_create(tid+i,NULL,thr_prime,(void *)i);
// free(p);//值已经娶走了,可以拆了--尽量在同一个函数中或者同一个模块中干掉
if(err)
{
fprintf(stderr,"pthread_create():%s\n",strerror(err));
exit(1);
}
}
for(i=LEFT;i<=RIGHT;i++)
{
pthread_mutex_lock(&mut_num);
while(num !=0)
{
pthread_cond_wait(&cond_num,&mut_num);
/*
pthread_mutex_unlock(&mut_num);//先解锁然后立即加锁有可能还是被自己抢到,所以用sched_yield()
sched_yield();//以sched_开头的都是调度器相关的,出让调度器给其他线程
pthread_mutex_lock(&mut_num);
*/
}
num=i;
pthread_cond_signal(&cond_num);//叫醒任意一个就好!!!
pthread_mutex_unlock(&mut_num);
}
pthread_mutex_lock(&mut_num);
while(num !=0)//确保最后一个任务被取走
{
pthread_mutex_unlock(&mut_num);
sched_yield();
pthread_mutex_lock(&mut_num);
}
num=-1;//用来线程退出的
pthread_cond_broadcast(&cond_num);
pthread_mutex_unlock(&mut_num);
for(i=0;i<THRNUM;i++)
{
pthread_join(tid[i],NULL);//
}
pthread_mutex_destroy(&mut_num);
pthread_cond_destroy(&cond_num);
exit(0);
}
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#define LEFT 30000000
#define RIGHT 30000200
#define THRNUM 4
static int num=0;
static pthread_mutex_t mut_num=PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond_num=PTHREAD_COND_INITIALIZER;
struct thr_arg_st
{
int n;
};
static void *thr_prime(void *p)
{
int i,j,mark;
while(1)
{
pthread_mutex_lock(&mut_num);
while(num == 0)
{
pthread_cond_wait(&cond_num,&mut_num);
/*
pthread_mutex_unlock(&mut_num);
sched_yield();//出让调度器给其他进程
pthread_mutex_lock(&mut_num);
*/
}
if(num == -1)
{
pthread_mutex_unlock(&mut_num);
break;//临界区跳转一定先注意解锁
}
i=num;
num=0;
pthread_cond_broadcast(&cond_num);
pthread_mutex_unlock(&mut_num);
mark=1;
for(j=2;j<i/2;j++)
{
if(i%j==0)
{
mark=0;
break;
}
}
if(mark)
printf("%d %dis a primer\n",(int)p,i);
}
pthread_exit(NULL);//返回值用于free
}
int main()
{
int i,err;
pthread_t tid[THRNUM];
for(i=0;i<THRNUM;i++)
{
err=pthread_create(tid+i,NULL,thr_prime,(void *)i);
// free(p);//值已经娶走了,可以拆了--尽量在同一个函数中或者同一个模块中干掉
if(err)
{
fprintf(stderr,"pthread_create():%s\n",strerror(err));
exit(1);
}
}
for(i=LEFT;i<=RIGHT;i++)
{
pthread_mutex_lock(&mut_num);
while(num !=0)
{
pthread_cond_wait(&cond_num,&mut_num);
/*
pthread_mutex_unlock(&mut_num);//先解锁然后立即加锁有可能还是被自己抢到,所以用sched_yield()
sched_yield();//以sched_开头的都是调度器相关的,出让调度器给其他线程
pthread_mutex_lock(&mut_num);
*/
}
num=i;
pthread_cond_signal(&cond_num);//叫醒任意一个就好!!!
pthread_mutex_unlock(&mut_num);
}
pthread_mutex_lock(&mut_num);
while(num !=0)//确保最后一个任务被取走
{
pthread_mutex_unlock(&mut_num);
sched_yield();
pthread_mutex_lock(&mut_num);
}
num=-1;//用来线程退出的
pthread_cond_broadcast(&cond_num);
pthread_mutex_unlock(&mut_num);
for(i=0;i<THRNUM;i++)
{
pthread_join(tid[i],NULL);//
}
pthread_mutex_destroy(&mut_num);
pthread_cond_destroy(&cond_num);
exit(0);
}