测试sleep()和pthread_cond_timewait()之间的区别

测试sleep()和pthread_cond_timewait()之间的区别用来测试sleep()和pthread_cond_timewait()之间的区别通过#if0/1来分别测试当从终端输入q时,通过打印来判断是否可以立即返回结束线程,还是要等睡眠时间到了才能结束线程。当条件满足时,pthread_cond_signal()来触发代码#include<stdio.h>#include<stdlib.h>#include<strin…

大家好,又见面了,我是你们的朋友全栈君。

用来测试sleep()和pthread_cond_timewait()之间的区别

通过#if 0/1 来分别测试

当从终端输入q时,通过打印来判断是否可以立即返回结束线程,还是要等睡眠时间到了才能结束线程。

当条件满足时,pthread_cond_signal()来触发

代码

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>

#include<pthread.h>
#include <signal.h> 

int flag = 1;
void* print_a(void*);
void* print_b(void*);

pthread_cond_t		cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_t		cond_mutex = PTHREAD_MUTEX_INITIALIZER;

int main()
{
	pthread_t pth[2];
	char c;

	if(pthread_create(&pth[0],NULL,print_a,NULL) ==-1)
	{
		printf("----pth[0]error--------\n");
		exit(-1);
	}


	if(pthread_create(&pth[1],NULL,print_b,NULL) ==-1)
	{
		printf("----pth[1]error--------\n");
		exit(-1);
	}


	while ((c = getchar()) != 'q');
	
	flag = 0;
	
	
	printf("----end----------\n");
	
	pthread_cond_signal(&cond);  
	
        if(pthread_join(pth[0], NULL) == -1){
            puts("fail to recollect t0");
            exit(1);
        }
    
	pthread_cond_signal(&cond);  
	
        if(pthread_join(pth[1], NULL) == -1){
        puts("fail to recollect t1");
        exit(1);
        }

    
	printf("-bye\n");

	return 0 ;
}

void* print_a(void* a)
{
	
	struct	timespec	timeout;
	
	while(flag){

#if 0
		timeout.tv_sec = time(NULL) + 30;
		timeout.tv_nsec = 0;

		//printf("aa\n");
		/* Mutex must be locked for pthread_cond_timedwait... */
		
		pthread_mutex_lock(&cond_mutex);
		/* Thread safe "sleep" */
		pthread_cond_timedwait(&cond, &cond_mutex, &timeout);

		/* No longer needs to be locked */
		pthread_mutex_unlock(&cond_mutex);
#else
		sleep(30);
#endif
	}
	

	printf("----aa-----------\n");
	
}



void* print_b(void* b)
{
	struct	timespec	timeout;
	
	while(flag){
#if 0
		timeout.tv_sec = time(NULL) + 40;
		timeout.tv_nsec = 0;
		pthread_mutex_lock(&cond_mutex);
		printf("bb\n");
		/* Mutex must be locked for pthread_cond_timedwait... */
		
		/* Thread safe "sleep" */
		pthread_cond_timedwait(&cond, &cond_mutex, &timeout);

		/* No longer needs to be locked */
		pthread_mutex_unlock(&cond_mutex);
#else
		sleep(50);
#endif
	}
	
	printf("----bb-----------\n");
}


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/143758.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • vuex mapGetters的使用「建议收藏」

    vuex mapGetters的使用「建议收藏」importVuefrom’vue’;importVuexfrom’vuex’;Vue.use(Vuex);conststore=newVuex.Store({state:{name:’张三’,number:0,list:[{id:1,name:’111′},{id:2,name:’222′},{id:3,

    2022年5月29日
    40
  • c浅拷贝和深拷贝的区别_js中深拷贝和浅拷贝的区别

    c浅拷贝和深拷贝的区别_js中深拷贝和浅拷贝的区别先考虑一种情况,对一个已知对象进行拷贝,编译系统会自动调用一种构造函数——拷贝构造函数,如果用户未定义拷贝构造函数,则会调用默认拷贝构造函数。先看一个例子,有一个学生类,数据成员时学生的人数和名字:#include<iostream>usingnam…

    2022年9月1日
    0
  • 安装VMware 和Ubuntu教程 以及ubuntu更新源「建议收藏」

    安装VMware 和Ubuntu教程 以及ubuntu更新源「建议收藏」1、VMwareWorkstation安装教程,见链接https://jingyan.baidu.com/article/9f7e7ec09da5906f281554d6.html2.VMware虚拟机下安装ubuntu操作系统(1)、下载ubuntu操作系统镜像(2)、点击文件–新建虚拟机,或者点击右边新建虚拟机。(3)、这里选择典型的安装模式(4)、点击下一步,选择安装…

    2022年7月22日
    6
  • AWE2017今日召开 智能家居发展势头强劲

    AWE2017今日召开 智能家居发展势头强劲

    2022年3月4日
    36
  • vue-cli-service serve报错_token怎么获取

    vue-cli-service serve报错_token怎么获取前端调试使用node环境调试代码时,返回token无效,线上环境和其他浏览器都没有错误解决方案:打开chrome输入chrome://flags/搜索SameSitebydefaultcookies找到SameSitebydefaultcookies和CookieswithoutSameSitemustbesecure将两个都设置成disable参考:https://www.cnblogs.com/yuan31415/p/13745139.html原因Chrome5

    2022年9月12日
    0
  • 无限弹窗(bat代码 整人恶作剧)

    无限弹窗(bat代码 整人恶作剧)炸弹弹窗,是使用bat制作的一个小程序,效果就是执行程序后会一直不停地弹出窗口,用来恶作剧。下面我们就来看看详细的教程。打开文件,输入以下代码::startstartcmdgotostart点击文件,选择另存为把文件名后面加上.bat保存类型选择所有文件点击保存…

    2022年6月17日
    41

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号