用代码来思考自旋锁和信号量

Posted on 2008-04-30 16:45 MichaelCao 阅读(953) 评论(4)  编辑 收藏 引用 所属分类: OS
    觉得昨天的思考似乎还是不怎么过瘾,有些问题还不是很清楚.到底应用方面两个有什么区别呢?
自己学着别人小小的动了下手.
先贴信号量的代码.
#include<pthread.h>
#include<stdio.h>
#include<sys/time.h>

#define MAX 10
pthread_t thread[2];
pthread_mutex_t mut;
int number=0,i;

void * thread1()
{
    printf("thread1: I'm thread 1 \n");
    for(i =0;i<MAX ;i++)
    {
        printf("thread 1: number=%d \n",number);
        pthread_mutex_lock(&mut);
        number++;
        pthread_mutex_unlock(&mut);
        sleep(2);
    }
    printf("thread1: 主函数在等我完成任务吗?\n");
    pthread_exit(NULL);
}
void *  thread2()
{
    printf("thread2: I'm thread 2 \n");
    for(i =0; i<MAX;i++)
    {
        printf("thread2 : number=%d\n",number);
        pthread_mutex_lock(&mut);
        number++;
        pthread_mutex_unlock(&mut);
        sleep(3);
    }
    printf("thread2 : 主函数在等我完成任务么?\n");
    pthread_exit(NULL);

}

void thread_create(void)
{
    /*创建线程*/
    pthread_create(&thread[0],NULL,thread1,NULL);
    printf("线程1被创建!\n");
    pthread_create(&thread[1],NULL,thread2,NULL);
    printf("线程2被创建!\n");
}
void thread_wait(void)
{
    /*等待线程结束*/
    pthread_join(thread[0],NULL);
    printf("线程1已经结束!\n");
    pthread_join(thread[1],NULL);
    printf("线程2已经结束!\n");
}
int main()
{
    /*用默认属性初始化互斥锁*/
    pthread_mutex_init(&mut,NULL);
    printf("我是主函数,我正在创建线程!\n");
    thread_create();
    printf("我是主函数,我正在等待线程完成任务!\n");
    thread_wait();
}

执行的结果是:
我是主函数,我正在创建线程!
thread1: I'm thread 1
thread 1: number=0
线程1被创建!
thread2: I'm thread 2
thread2 : number=1
线程2被创建!
我是主函数,我正在等待线程完成任务!
thread 1: number=2
thread2 : number=3
thread 1: number=4
thread 1: number=5
thread2 : number=6
thread 1: number=7
thread2 : number=8
thread 1: number=9
thread2 : number=10
thread1: 主函数在等我完成任务吗?
线程1已经结束!
thread2 : 主函数在等我完成任务么?
线程2已经结束!

 重要:这个执行的过程大概要10秒!!!!!!
而我们用自旋锁,代码:
/*
 * time :2008.4.30
 * author:will cao
 * Email:sei_michael@126.com
 * 探索自旋锁与信号量的区别
 */
#include<pthread.h>
#include<stdio.h>

pthread_t thread[2];
pthread_spinlock_t lock ;

#define MAX 10

int number=0,i;

void * thread1()
{
    printf ("thread 1 :I began to run !");
    for(i=0;i<MAX;i++)
    {
        printf("thread 1 :number=%d \n",number);
        pthread_spin_lock(&lock);
        number++;
        pthread_spin_unlock(&lock);
    }
    printf("ok ,I am over !\n");
    pthread_exit(NULL);
}
void * thread2 ()
{
    printf("thread2 : I start !!!\n");
    for(i=0;i<MAX;i++)
    {
        printf("thread2 : number = %d \n",number);
        pthread_spin_lock(&lock);
        number++;
        pthread_spin_unlock(&lock);
    }
    printf("thread 2: I am over!!!");
    pthread_exit(NULL);
}

void thread_create(void)
{
    /*create the threads */
    pthread_create(&thread[0],NULL,thread1,NULL);
    printf("create the thread 1\n ");
    pthread_create(&thread[1],NULL,thread2,NULL);
    printf("create the thread 2 \n");
}
void thread_wait(void )
{
    /*wait for the thread to be over */
    pthread_join(thread[0],NULL);
    printf("the thread 1 is over !\n");
    pthread_join(thread[1],NULL);
    printf("the thread 2 is over ! \n");
}
int main()
{
    /* init the spin lock */
    pthread_spin_init(&lock,0);
    printf("i am the main,and I am creating the threads ");
    thread_create();
    printf("i am the main,and I am wait for the thread to be over!");
    thread_wait();
}
 执行结果为:
i am the main,and I am creating the threads thread 1 :I began to run !thread 1 :number=0
thread 1 :number=1
thread 1 :number=2
thread 1 :number=3
thread 1 :number=4
thread 1 :number=5
thread 1 :number=6
thread 1 :number=7
thread 1 :number=8
thread 1 :number=9
ok ,I am over !
create the thread 1
 thread2 : I start !!!
create the thread 2
i am the main,and I am wait for the thread to be over!thread2 : number = 10
thread2 : number = 11
thread2 : number = 12
thread2 : number = 13
thread2 : number = 14
thread2 : number = 15
thread2 : number = 16
thread2 : number = 17
thread2 : number = 18
thread2 : number = 19
thread 2: I am over!!!the thread 1 is over !
the thread 2 is over !
   执行时间:我没用系统调用,但肯定是用不了0.1秒的...
总结:从表面上来看,很明显的区别是当我们用的是信号量的时候,这个时候是有调度的.因为从运行结果上来看,主线程在创建其他两个线程后,其他线程开始运行.并且主线程也在运行.但怎么运行这个是无法确定的,这是一个并发的过程.
    当使用自旋锁后,这个就不一样了.当运行到临界区的时候,它是直接的过去,不是会产生一个等待,或者一个调度.
不知道编译器是怎么编译的.很想知道编译后二进制代码有什么区别.但这个好像有点太难....不过我觉得从运行结果上来看这么多,应该差不多了.


Feedback

# re: 用代码来思考自旋锁和信号量  回复  更多评论   

2008-09-19 10:01 by 郭石
博主,你前面不是在用互斥锁吗?哪里用了信号量呢?

# re: 用代码来思考自旋锁和信号量  回复  更多评论   

2008-10-27 18:27 by mach
如果把上面代码中的sleep去掉,估计运行时间也不会超过0.1秒 哈哈

# re: 用代码来思考自旋锁和信号量  回复  更多评论   

2009-03-02 11:51 by galilio
只有代码,没有思考

# re: 用代码来思考自旋锁和信号量  回复  更多评论   

2009-06-02 17:34 by falconflying
我觉得楼主是想当然了,对于spinlock的确会出现你所说的结果,但是也会出现mutex类似的结果,在我的机器上就出现了,redhat5 企业版
另外,mutex出现10秒的原因是你使用了sleep。。。。。

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


posts - 16, comments - 16, trackbacks - 0, articles - 0

Copyright © MichaelCao