多线程

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<memory.h>
#include<pthread.h>
#include<semaphore.h>
#include<time.h>



void *printf_c(void *b)
{
    char *p = (char *)b;
    int i=0;
    for (i=0;i<10;i++)
        printf("%8s %4d \n", p, i);

    return NULL;
}

void simple_printf()
{
    pthread_t thread1;
    pthread_t thread2;

    char *msg1 = "thread1";
    char *msg2 = "thread2";

    
    if (-1 == pthread_create(&thread1, NULL, (void*)printf_c, (void*)msg1))
    {
        printf("fail to create pthread t0\n");
        exit(1);
    }
    if (-1 == pthread_create(&thread2, NULL, (void*)printf_c, (void*)msg2))
    {
        printf("fail to create pthread t1\n");
        exit(1);
    }

    void* result;
    if (-1 == pthread_join(thread1, &result))
    {
        printf("fail to  recollect t0\n");
        exit(1);
    }
    printf("thread1 result = %d\n",(int*)result);
    if (-1 == pthread_join(thread2, &result))
    {
        printf("fail to  recollect t1\n");
        exit(1);
    }
    printf("thread2 result = %d\n",(int*)result);
    
    return;
}

int shareid = 0;
pthread_mutex_t mutex;
//pthread_mutex_init(&mutex, PTHREAD_MUTEX_INITALIZER);

void increase_num_no_suo(void)
{
    long i,tmp;
    for(i=0;i<1000;i++)
    {
        tmp = shareid;
        tmp++;
        shareid = tmp;
    }
}

void increase_num_suo(void)
{
    long i,tmp;
    for(i=0;i<1000;i++)
    {
        if(0 != pthread_mutex_lock(&mutex))
        {
            printf("pthread_mutex_lock\n");
            exit(EXIT_FAILURE);
        }
        tmp = shareid;
        tmp++;
        shareid = tmp;
        
        if(0 != pthread_mutex_unlock(&mutex))
        {
            printf("pthread_mutex_unlock\n");
            exit(EXIT_FAILURE);
        }
    }
}

void pthread_opr(void *fun)
{
    pthread_t thread1;
    pthread_t thread2;
    pthread_t thread3;
    
    char *msg1 = "thread1";
    char *msg2 = "thread2";
    char *msg3 = "thread3";
    
    if (-1 == pthread_create(&thread1, NULL, (void*)fun, (void*)msg1))
    {
        printf("fail to create pthread thread1\n");
        exit(1);
    }
    if (-1 == pthread_create(&thread2, NULL, (void*)fun, (void*)msg2))
    {
        printf("fail to create pthread thread2\n");
        exit(1);
    }
    if (-1 == pthread_create(&thread3, NULL, (void*)fun, (void*)msg3))
    {
        printf("fail to create pthread thread3\n");
        exit(1);
    }
    
    void* result;
    if (-1 == pthread_join(thread1, &result))
    {
        printf("fail to  recollect thread1\n");
        exit(1);
    }
    printf("thread1 result = %d\n",(int*)result);
    if (-1 == pthread_join(thread2, &result))
    {
        printf("fail to  recollect thread2\n");
        exit(1);
    }
    printf("thread2 result = %d\n",(int*)result);
    if (-1 == pthread_join(thread3, &result))
    {
        printf("fail to  recollect thread3\n");
        exit(1);
    }
    printf("thread3 result = %d\n",(int*)result);

    printf("shareid = %d\n", shareid);
    return;
}

#define isSleep  0
#define isPrintf 0
#define maxsize  10000000
long long stack[maxsize];
long long size;
sem_t sem;

void calc_data()
{
    long long i;
    for (i=0;i<maxsize;i++)
    {
        stack[i]=i;
#if isPrintf
        printf("%lld X %lld = %lld\n", stack[i], stack[i], stack[i]*stack[i]);
#endif
#if isSleep
        sleep(1);
#endif
    }
}

void provide_data()
{
    long long i;
    for (i=0;i<maxsize;i++)
    {
        stack[i]=i;
        sem_post(&sem);   //+1
    }
}

void handle_data()
{
    long long i;
    while ((i=size++)<maxsize)
    {
        sem_wait(&sem);   //-1
#if isPrintf
        printf("%lld X %lld = %lld\n", stack[i], stack[i], stack[i]*stack[i]);
#endif
#if isSleep
        sleep(1);
#endif
    }
}

void opr_sem()
{
    pthread_t provide,handle;

    sem_init(&sem, 0, 0);
    if (-1 == pthread_create(&provide, NULL, (void*)provide_data, NULL))
    {
        printf("fail to create pthread provide\n");
        exit(1);
    }
    if (-1 == pthread_create(&handle, NULL, (void*)handle_data, NULL))
    {
        printf("fail to create pthread handle\n");
        exit(1);
    }

    void* result;
    if (-1 == pthread_join(provide, &result))
    {
        printf("fail to  recollect provide\n");
        exit(1);
    }
    printf("provide result = %d\n",(int*)result);
    if (-1 == pthread_join(handle, &result))
    {
        printf("fail to  recollect handle\n");
        exit(1);
    }
    printf("handle result  = %d\n",(int*)result);

    sem_destroy(&sem);
}

void main()
{
    simple_printf();
#if 0
    printf("*************bujiasuo*************\n");
    pthread_opr(increase_num_no_suo);
#else
    printf("*************jiasuo*************\n");
    pthread_opr(increase_num_suo);
#endif

#if 1
    time_t t0,t1,t2;
    int it0,it1,it2;
    t0 = time(NULL);
    it0 = time(&t0);
    printf("*************single pthread*************\n");
    calc_data();
//#else
    t1 = time(NULL);
    it1 = time(&t1);
    printf("*************sem muti pthread*************\n");
    opr_sem();
    t2 = time(NULL);
    it2 = time(&t2);
    printf("*************time*************\n");
    printf("t0 = %d\n", t0);
    printf("t1 = %d\n", t1);
    printf("t2 = %d\n", t2);
    printf("time_single = %d \n", it1 - it0);
    printf("time_muti   = %d \n", it2 - it1);
    printf("time_single - time_muti = %d\n", 2*it1-it0-it2);
#endif
    
    return;
}
[root@10.67.9.137 /newhome/wuyaoyao/Linux_Bash/thread]# gcc -o mutithread mutithread.c -lpthread
[root@10.67.9.137 /newhome/wuyaoyao/Linux_Bash/thread]# ./mutithread
[root@10.67.9.137 /newhome/wuyaoyao/Linux_Bash/thread]# ./mutithread
 thread1    0
 thread1    1
 thread1    2
 thread1    3
 thread1    4
 thread1    5
 thread1    6
 thread1    7
 thread1    8
 thread1    9
 thread2    0
 thread2    1
 thread2    2
 thread2    3
 thread2    4
 thread2    5
 thread2    6
 thread2    7
 thread2    8
 thread2    9
thread1 result = 0
thread2 result = 0
*************jiasuo*************
thread1 result = 0
thread2 result = 0
thread3 result = 0
shareid = 3000
*************single pthread*************
*************sem muti pthread*************
provide result = 0
handle result  = 10000000
*************time*************
t0 = 1556248505
t1 = 1556248505
t2 = 1556248507
time_single = 0
time_muti   = 2
time_single - time_muti = -2
[root@10.67.9.137 /newhome/wuyaoyao/Linux_Bash/thread]# ./mutithread
 thread2    0
 thread2    1
 thread2    2
 thread2    3
 thread2    4
 thread2    5
 thread2    6
 thread1    0
 thread1    1
 thread1    2
 thread1    3
 thread1    4
 thread1    5
 thread1    6
 thread1    7
 thread1    8
 thread1    9
 thread2    7
thread1 result = 0
 thread2    8
 thread2    9
thread2 result = 0
*************jiasuo*************
thread1 result = 0
thread2 result = 0
thread3 result = 0
shareid = 3000
*************single pthread*************
*************sem muti pthread*************
provide result = 0
handle result  = 10000000
*************time*************
t0 = 1556248508
t1 = 1556248508
t2 = 1556248511
time_single = 0
time_muti   = 3
time_single - time_muti = -3
[root@10.67.9.137 /newhome/wuyaoyao/Linux_Bash/thread]#

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值