实验2 多线程编程

1 编写一个最基本的多线程编程。

#include

#include

#include

#include

#include

#include

void *run(void  *arg)

{

sleep(1);

printf(“helloworld!

}

int main()

{

pthread_t  ntid;

int err;

err=pthread_create(&ntid,NULL,run,NULL);

err=pthread_create(&ntid,NULL,run,NULL);

sleep(1);

return 0;

}

修改该程序,要求变成8 个子线程,并且通过ps–elf ps 命令进行观察

I am thread  %u\r\n”,pthread_self());

return ((void  *)0);//注意必须这样写

2 编写关于互斥量的程序

#include

#include

#include

#include

#include

pthread_mutex_t  mqlock=PTHREAD_MUTEX_INITIALIZER;

int vari=0;

void pthread1(void*  arg)

{

int i=0;

for (i=0;i<3;i++)

{

pthread_mutex_lock(&mqlock);

vari=vari+10;


printf("thread 1 vari=%d\r\n",vari);

sleep(3);

pthread_mutex_unlock(&mqlock);

}

}

void pthread2(void*  arg)

{

int i=0;

for (i=0;i<3;i++)

{

pthread_mutex_lock(&mqlock);

vari=vari-10;

printf("thread 2 vari=%d\r\n",vari);

pthread_mutex_unlock(&mqlock);

}

}

int main()

{

int ret;

pthread_t  id1,id2;

ret=pthread_create(&id1,NULL,(void  *)pthread1, NULL);

if(ret!=0)  perror("pthread cread1");

ret=pthread_create(&id2,NULL,(void *)pthread2,  NULL);

if(ret!=0) perror("pthread cread2");

pthread_join(id1,NULL);

pthread_join(id2,NULL);

printf("result  is vari=%d\r\n",vari);

}

将该程序改写为5 个子线程修改临界变量(延时1 秒)的程序

3 关于trylock 的程序

#include

#include

#include

#include

#include


pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

int lock_var;

time_t end_time;

void pthread1(void  *arg);

void pthread2(void  *arg);

int main(int  argc, char *argv[])

{

pthread_t  id1,id2;

pthread_t  mon_th_id;

int ret;

end_time =  time(NULL)+10;

/*互斥锁初始化*/

pthread_mutex_init(&mutex,NULL);

/*创建两个线程*/

ret=pthread_create(&id1,NULL,(void *)pthread1,  NULL);

if(ret!=0)

perror("pthread cread1");

ret=pthread_create(&id2,NULL,(void *)pthread2,  NULL);

if(ret!=0)

perror("pthread  cread2");

pthread_join(id1,NULL);

pthread_join(id2,NULL);

exit(0);

}

void pthread1(void  *arg)

{

int i;

while(time(NULL)  < end_time)

{

/*互斥锁上锁*/

pthread_mutex_lock(&mutex);

printf("pthread1:pthread1  lock the variable\n");

for(i=0;i<2;i++){

sleep(1);

lock_var++;

}

/*互斥锁接锁*/

pthread_mutex_unlock(&mutex);

printf("pthread1:pthread1  unlock the variable\n");


sleep(1);

}

}

void pthread2(void  *arg)

{

int nolock=0;

int ret;

while(time(NULL)  < end_time)

{

/*测试互斥锁*/

ret=pthread_mutex_trylock(&mutex);

if(ret==EBUSY)

printf("pthread2:the  variable is locked by pthread1\n");

else

{

printf("pthread2:pthread2  got lock.The variable is%d\n",lock_var);

/*互斥锁接锁*/

if(pthread_mutex_unlock(&mutex)!=0)

{

perror("pthread_mutex_unlock");

exit (-1);

}

printf("pthread2:pthread2  unlock the variable\n");

}

sleep(3);

}//while

}

4 关于条件变量的程序

#include

#include

#include

#include

int a[256];

int windex=0;


int rindex=0;

pthread_mutex_t  mutex = PTHREAD_MUTEX_INITIALIZER;/*初始化互斥锁*/

pthread_cond_t  cond = PTHREAD_COND_INITIALIZER;//init cond

void *thread1(void*);

void *thread2(void*);

int num1=0;//global

int num2=0;

int main(void){

pthread_t t_a;

pthread_t t_b;//two thread

pthread_create(&t_a,NULL,thread2,(void*)NULL);

pthread_create(&t_b,NULL,thread1,(void*)NULL);//Create  thread

pthread_join(t_b,NULL);//wait  a_b thread end

pthread_mutex_destroy(&mutex);

pthread_cond_destroy(&cond);

exit(0);

}

void *thread1(void  *junk){

int i=0;

for(i=0;i<30;i++)

{

if(i%7==0)sleep(1);

pthread_mutex_lock(&mutex);

int temp=rand();

a[rindex]=temp;

rindex++;

printf("thread1puta

num1=%d\r\n",temp,rindex,++num1);

pthread_mutex_unlock(&mutex);

pthread_cond_signal(&cond);

}

}

void *thread2(void*junk){

int i;

for(i=0;i<30;i++)

{

number

%d

to

windex

%d


pthread_mutex_lock(&mutex);

while(windex>=rindex)

pthread_cond_wait(&cond,&mutex);

printf("thread2:data  is %d, index is %d num2=%d\r\n",a[windex],windex,++num2);

windex++;

pthread_mutex_unlock(&mutex);

}

}

理解该程序,改写为三线程模式,1 个线程写数据,2 个线程读数据

5 综合应用生产者消费者程序

#include

#include

#include

#include

#define BUFSIZE  10

int buf[BUFSIZE];

int front=0;

int rail=0;

void printbuf()

{

printf("front=%d  rail=%d",front,rail);

}

int isEmpty()

{

if(front==rail)return 1;

else return  0;

}

int isFull()

{

if(rail-front==BUFSIZE-1||front-rail==1)return  1;

else return 0;

}

void enqueue(int  x)

{

if(isFull()){

fprintf(stderr,"error  in produce thread id is %d\r\n",pthread_self());

exit(-1);


};

buf[rail]=x;

rail=(++rail)%BUFSIZE;

};

int dequeue()

{

if(isEmpty()){

fprintf(stderr,"error in produce thread  id is %d\r\n",pthread_self());

exit(-1);

};

front=(++front)%BUFSIZE;

}

int produce()

{

int ret=0;

//check

if(isFull()){

fprintf(stderr,"error in produce thread  id is %d\r\n",pthread_self());

exit(-1);

}

if(isEmpty())ret=1;

buf[rail]=rand()%1000;

printf("thread %u  produce %d to Buf[%d]\r\n",pthread_self(),buf[rail],rail);

rail= (++rail)%BUFSIZE;

sleep(1);

return ret;

};

int consume()

{

int ret=0;

//check

if(isEmpty()){

fprintf(stderr,"error in produce  thread id is %d\r\n",pthread_self());

exit(-1);

}

if(isFull())ret=1;

printf("thread  %u consume %d from Buf[%d]\r\n",pthread_self(),buf[front],front);

front++;

front=front%BUFSIZE;

sleep(1);

return ret;


};

pthread_mutex_t  mutex = PTHREAD_MUTEX_INITIALIZER;/*初始化互斥锁*/

pthread_cond_t  condempty = PTHREAD_COND_INITIALIZER;//init cond

pthread_cond_t  condfull = PTHREAD_COND_INITIALIZER;//init cond

void *threadcos(void*);

void *threadpro(void*);

int main(void){

pthread_t  t_a,t_b,t_c,t_d;

rand();

pthread_create(&t_a,NULL,threadpro,(void*)NULL);

pthread_create(&t_b,NULL,threadcos,(void*)NULL);//Create  thread

pthread_create(&t_c,NULL,threadpro,(void*)NULL);

pthread_create(&t_d,NULL,threadcos,(void*)NULL);

pthread_join(t_a,NULL);//wait  a_b thread end

pthread_join(t_b,NULL);//wait a_b thread end

pthread_join(t_c,NULL);

pthread_join(t_d,NULL);

pthread_mutex_destroy(&mutex);

pthread_cond_destroy(&condempty);

pthread_cond_destroy(&condfull);

exit(0);

}

void *threadpro(void  *junk)

{

for(;;)

{

pthread_mutex_lock(&mutex);

if(!isFull())

{

int ret=produce();

if(ret!=0)pthread_cond_signal(&condempty);//只有向空队列中放

入元素时候才返回1,否则返回0

}

else

{

//printbuf();

printf("\r\n  队列满,线程%u 开始等待\r\n",pthread_self());

pthread_cond_wait(&condfull,&mutex);

}


}

}

void *threadcos(void*junk){

for(;;)

{

pthread_mutex_lock(&mutex);

if(!isEmpty())

{

int ret=consume();//从满的队列中消费,返回1,否则返回0

if(ret==0)pthread_cond_signal(&condfull);

}

else

{

printf("队列空,线程%u 开始等待\r\n",pthread_self());

pthread_cond_wait(&condempty,&mutex);

}

pthread_mutex_unlock(&mutex);

}

}