linux中c语言代码线程执行顺序问题

先来看这样一段代码

 /*
 pthread_create函数
 原型:int  pthread_create((pthread_t  *thread,  pthread_attr_t  *attr, 
 void  *(*start_routine)(void  *), 
 void  *arg)
 用法:#include  <pthread.h>
 功能:创建线程(实际上就是确定调用该线程函数的入口点),在线程创建以后,就开始运行相关的线程函数。
 说明:thread:线程标识符;
 attr:线程属性设置;
 start_routine:线程函数的起始地址;
 arg:传递给start_routine的参数;
 返回值:成功,返回0;出错,返回-1。
 */
#include <unistd.h>
//sleep();
#include <stdio.h>
#include <pthread.h>
void thread_1(){
    int i =0;
    while (1) {
        i+=1;
        sleep(1);
        printf("2B我是线程一 %d次\n",i);
    }
}

void thread_2(){
    int i =0;
    while (1) {
        i+=1;
        sleep(2);
        printf("2B我是线程二 %d次\n",i);
    }
}
void thread_3(){
    int i =0;
    while (3) {
        i+=1;
        sleep(3);
        printf("2B我是线程三 %d次\n",i);
    }
}

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

//线程开始的地址
    pthread_t star_location_1,star_location_2,star_location_3;
    int ret;
    //创建线程
    ret = pthread_create(&star_location_1, NULL,(void *)thread_1,NULL);
    if (ret == !0) {
        printf("creat_error_1");
    }
    ret = pthread_create(&star_location_2, NULL,(void *)thread_2,NULL);
    if (ret == !0) {
        printf("creat_error_2");
    }
    ret = pthread_create(&star_location_3, NULL,(void *)thread_3,NULL);

    if (ret == !0) {
        printf("creat_error_2");
    }
    /*等待线程结束*/
        pthread_join(star_location_1, NULL);
	pthread_join(star_location_2, NULL);
	pthread_join(star_location_3, NULL);
    return 0;
}

        pthread_join()函数是的作用是调用者等待被调用函数执行完之后才往下执行,在这个程序里调用者就是主线线程main()了,上面这段代码执行后是这样的,当执行完pthread_join()函数之后,好像主线线程没有等待而是直接接着往下运行了,其实不是这样,线程在 ret = pthread_create(&star_location_1, NULL,(void *)thread_1,NULL);这样的语句执行后就开始执行线程了,main函数在执行pthread_join(star_location_1, NULL);时就已经被挂起,之前创建的三个进程在执行这段代码之前就已经在运行了,只不过显示缓冲区并没有及时的将“2B我是线程一 "这样的字符输出到屏幕上,相对于main函数来讲,它太慢了,而且main()函数一旦结束,子线程也就消亡了。

    其实我们可以完全不需要pthread_join()但是这样main函数会非常快的执行结束导致子线程还没来得及执行就消亡了,所以程序什么也不会输出,主要原因就是缓冲区太慢了,还没有将字符送到显示器上,程序就已经执行好久了


    /*等待线程结束*/

    //pthread_join(star_location_1, NULL);
    //printf("shenghuojiuxiangyibeishui");
    //pthread_join(star_location_2, NULL)
    //pthread_join(star_location_3, NULL);
    return 0;

     如果我们在上面的这段代码里将下面的语句全部注释并且加入sleep(1000)就会发现程序在执行到sleep(1000)的时候会等待,而线程会一直执行!可以在屏幕上看到输出的字符,


    /*等待线程结束*/

    //pthread_join(star_location_1, NULL);
    //printf("shenghuojiuxiangyibeishui");
    //pthread_join(star_location_2, NULL)
    //pthread_join(star_location_3, NULL);
    sleep(1000);
    return 0;

 其实完全没必要写三个pthread_join()函数,写一个就可以把main()函数挂起了,或者直接用sleep()函数阻止main()提前结束


    /*等待线程结束*/

    pthread_join(star_location_1, NULL);
 或者
    sleep(1000);
    return 0;

上面的思绪有点乱。。可以稍稍整理一下思绪,重新思考下面讨论的问题

    既然缓冲区向屏幕输出字符的速度太慢了导致字符输出顺序乱了,那我们能不能让cpu等一等呢?我们可以在printf()函数之后假如sleep()函数,让cpu等着字符在屏幕上显示之后再执行

void thread_1(){
    int i =0;
    while (1) {
        i+=1;
		printf("2B我是线程1 %d次\n",i);
		sleep(3);

    }

}
void thread_2(){
    int i =0;
    while (1) {
        i+=1;
		printf("2B我是线程2 %d次\n",i); 
		sleep(3);
    }
}
void thread_3(){
    int i =0;
    while (3) {
        i+=1;
        sleep(3);
		printf("2B我是线程3 %d次\n",i);
    }
}

     这样应该能够正确的表示三个线程竞争处理器的先后顺序了,因为每次再printf("2B我是线程....")之后,就调用sleep()函数,让处理器处于等待,只用当等待完成,cpu才进行下一步处理,但是这样是否就一定正确呢?线程对于处理器cpu的竞争是随时随地的,是充满不确定性的,或许当第一个线程刚刚执行完i+=1;突然来个中断,cpu就跑去执行第二个线程呢?又或者sleep()函数真的会让cpu无所作为吗?在cpu,sleep过程中万一中断来临呢?我们需要一把锁来确定线程执行的顺序。

#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
int xx=0;
pthread_mutex_t mut; //互斥锁类型
void thread_1(){
    int i =0;
	pthread_mutex_lock(&mut); //加锁,用于对共享变量操作
		i+=1; 
		printf("2B我是线程1 %d次\n",i);
		sleep(3);
		xx=0;
	pthread_mutex_unlock(&mut); //解锁
}

void thread_2(){
    int i =0;
	 pthread_mutex_lock(&mut); //加锁,用于对共享变量操作
        	i+=1;
			xx=1;
			printf("2B我是线程2 %d次\n",i); 
			sleep(3);
			xx=0;
		pthread_mutex_unlock(&mut); //解锁
    

}

void thread_3(){

    int i =0;

	 	pthread_mutex_lock(&mut); //加锁,用于对共享变量操作
	        i+=1;
			printf("2B我是线程3 %d次\n",i); 
			sleep(3);
			xx=0;
		pthread_mutex_unlock(&mut); //解锁
}
int main(int argc,
const char * argv[]) {
	pthread_mutex_init( & mut,NULL );
//线程开始的地址
    pthread_t star_location_1,star_location_2,star_location_3;
    int ret;
    //创建线程
    ret = pthread_create(&star_location_2, NULL,(void *)thread_2,NULL);
    if (ret == !0) {
        printf("creat_error_2");
    }

    ret = pthread_create(&star_location_1, NULL,(void *)thread_1,NULL);
    if (ret == !0) {
        printf("creat_error_1");
    }

    ret = pthread_create(&star_location_3, NULL,(void *)thread_3,NULL);
    if (ret == !0) {
        printf("creat_error_2");
    }
    /*等待线程结束*/
	printf("-----");
    	//pthread_join(star_location_1, NULL);
	printf("shenghuojiuixangyibeishui");
	sleep(10000);
	//pthread_join(star_location_2, NULL);
	//printf("-------------------------");
	//pthread_join(star_location_3, NULL);
	
    return 0;

}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值