找回密码
 加入后院

QQ登录

只需一步,快速开始

搜索
查看: 1039|回复: 1

gjgvj

[复制链接]
发表于 2008-4-24 15:51 | 显示全部楼层 |阅读模式
#include <pthread.h>
#include <stdio.h>
#include <sys/time.h>
#include <string.h>
#define MAX 10

pthread_t thread[3];
pthread_mutex_t mut;
int number=0, i;

void gong_nen()
{
    printf("1. you can select gong_nen now\n");
}

void fu_zhu()
{
    printf("2. you can select fu_zhu now\n");
}

void lu_xian()
{
    printf("3. lun_xian ing now\n");
}

void *thread1()
{
    for (i = 0; i < MAX; i++)
        {
     pthread_mutex_lock(&mut);
        gong_nen();
     pthread_mutex_unlock(&mut);
        sleep(2);
        }
    pthread_exit(NULL);
}

void *thread2()
{
    for (i = 0; i < MAX; i++)
        {
     pthread_mutex_lock(&mut);
        fu_zhu();
     pthread_mutex_unlock(&mut);
         sleep(3);
        }
    pthread_exit(NULL);
}

void *thread3()
{
    for (i = 0; i < MAX; i++)
        {
     pthread_mutex_lock(&mut);

        lu_xian();
     pthread_mutex_unlock(&mut);
         sleep(3);
        }
    pthread_exit(NULL);
}


void thread_create(void)
{
    int temp;
    memset(&thread, 0, sizeof(thread));         

    if((temp = pthread_create(&thread[0], NULL, thread1, NULL)) != 0)      
            printf("line 1 fail\n");
    if((temp = pthread_create(&thread[1], NULL, thread2, NULL)) != 0)
            printf("line 2 fail\n");
    if((temp = pthread_create(&thread[2], NULL, thread3, NULL)) != 0)
            printf("line 3 fail\n");
}

void thread_wait(void)
{
    if(thread[0] !=0) {                  
        pthread_join(thread[0],NULL);
        }
    if(thread[1] !=0) {               
        pthread_join(thread[1],NULL);
        }
    if(thread[2] !=0) {               
        pthread_join(thread[2],NULL);
        }
}

int main()
{

    pthread_mutex_init(&mut,NULL);

    thread_create();
    thread_wait();

    return 0;
}
发表于 2008-4-24 21:53 | 显示全部楼层
这段程序结果什么啊 ?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

Archiver|手机版|小黑屋|广告业务Q|工大后院 ( 粤ICP备10013660号 )

GMT+8, 2026-4-9 19:58

Powered by Discuz! X5.0

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表