pthread linux mutet:example1
#include<iostream>
#include<unistd.h>
#include<pthread.h>
#include<string>
using namespace std; #define START 1
#define END 0
int status = START;
void* queuewhile(void *);
void* dosomething(void *);
pthread_mutex_t mutex;
int main()
{
//int status = START;
cout << "please,enter something, if the number is larger.the process ends"<<endl;
pthread_t thread1, thread2;
sleep();
pthread_mutex_init(&mutex, NULL);
string strMsg = "hello,world";
pthread_create(&thread1, NULL, queuewhile,(void*)strMsg.c_str());
pthread_create(&thread2, NULL, dosomething, NULL);
pthread_join(thread1, NULL);
pthread_mutex_destroy(&mutex);
cout << "the process is closing"<<endl;
return ;
}
void* dosomething(void *)
{
int a = ;
int b = ;
while(cin >> b)
{
pthread_mutex_lock(&mutex);
cout<< "something happened"<< endl;
if(b>a)
{
cout<< "now,we shut down!"<<endl;
status = END;
}else
{
cout<< "ops, we just keep go on!"<<endl;
}
pthread_mutex_unlock(&mutex);
}
} void* queuewhile(void *msg)
{
string strMsg = (char*)msg;
if(strMsg.empty())
{
cout<< "there no thing"<< endl;
}else
{
while(status)
{
pthread_mutex_lock(&mutex);
cout<<strMsg<<endl;
pthread_mutex_unlock(&mutex);
sleep();
}
//cout << strMsg<<endl;
}
}
g++ -o test domything.cpp -lpthread
#include<iostream>
#include<unistd.h>
#include<pthread.h>
#include<string>
using namespace std; #define START 1
#define END 0
int status = START;
class MSG{
public:
int m_state;
string m_msg;
};
static MSG g_msg;
void* queuewhile(void *);
void* dosomething(void *);
pthread_mutex_t mutex;
int main()
{
//int status = START;
cout << "please,enter something, if the number is larger.the process ends"<<endl;
pthread_t thread1, thread2;
sleep();
pthread_mutex_init(&mutex, NULL);
string strMsg = "hello,world";
pthread_create(&thread1, NULL, queuewhile,(void*)strMsg.c_str());
pthread_create(&thread2, NULL, dosomething, NULL);
pthread_join(thread1, NULL);
pthread_mutex_destroy(&mutex);
cout << "the process is closing"<<endl;
return ;
}
void* dosomething(void *)
{
string a = "q";
string b = "";
while(cin >> b)
{
pthread_mutex_lock(&mutex);
cout<< "something happened"<< endl;
if(a.compare(b) == )
{
cout<< "now,we shut down!"<<endl;
status = END;
}else if(b.compare("w") ==)
{
cin >>b;
//cout<< "ops, we just keep go on!"<<endl;
g_msg.m_state += ;
g_msg.m_msg = b;
b.clear();
}
pthread_mutex_unlock(&mutex);
}
} void* queuewhile(void *msg)
{
string strMsg = (char*)msg;
if(strMsg.empty())
{
cout<< "there no thing"<< endl;
}else
{
while(status)
{
pthread_mutex_lock(&mutex);
cout<<strMsg<<endl;
if(g_msg.m_state > )
{
cout <<"the message is:"<< g_msg.m_msg<<endl;
g_msg.m_state --;
}
pthread_mutex_unlock(&mutex);
sleep();
}
//cout << strMsg<<endl;
}
}
//exec child process in the thread
#include<iostream>
#include<cstdlib>
#include<unistd.h>
#include<pthread.h>
#include<string>
using namespace std; #define START 1
#define END 0
int status = START;
class MSG{
public:
int m_state;
string m_msg;
};
static MSG g_msg;
void* queuewhile(void *);
void* dosomething(void *);
pthread_mutex_t mutex;
int main()
{
//int status = START;
cout << "please,enter something, if the number is larger.the process ends"<<endl;
pthread_t thread1, thread2;
sleep();
pthread_mutex_init(&mutex, NULL);
string strMsg = "hello,world";
pthread_create(&thread1, NULL, queuewhile,(void*)strMsg.c_str());
pthread_create(&thread2, NULL, dosomething, NULL);
pthread_join(thread1, NULL);
pthread_mutex_destroy(&mutex);
cout << "the process is closing"<<endl;
return ;
}
void* dosomething(void *)
{
string a = "q";
string b = "";
while(cin >> b)
{
pthread_mutex_lock(&mutex);
cout<< "something happened"<< endl;
if(a.compare(b) == )
{
cout<< "now,we shut down!"<<endl;
status = END;
}else if(b.compare("w") ==)
{
cin >>b;
//cout<< "ops, we just keep go on!"<<endl;
g_msg.m_state += ;
g_msg.m_msg = b;
b.clear();
}
pthread_mutex_unlock(&mutex);
}
} void* queuewhile(void *msg)
{
string strMsg = (char*)msg;
if(strMsg.empty())
{
cout<< "there no thing"<< endl;
}else
{
while(status)
{
pthread_mutex_lock(&mutex);
cout<<strMsg<<endl;
if(g_msg.m_state > )
{
string strPath = getenv("PWD");
//cout << "the path is :" << strPath<< endl;
strPath.append("/child ");
strPath.append(g_msg.m_msg.c_str());
system(strPath.c_str());
cout <<"the message is:"<< g_msg.m_msg<<endl;
g_msg.m_state --;
}
pthread_mutex_unlock(&mutex);
sleep();
}
//cout << strMsg<<endl;
}
}
pthread linux mutet:example1的更多相关文章
- ZT 为什么pthread_cond_t要和pthread_mutex_t同时使用 || pthread/Linux多线程编程
为什么线程同步的时候pthread_cond_t要和pthread_mutex_t同时使用 (2009-10-27 11:07:23) 转载▼ 标签: 杂谈 分类: 计算机 举一个例子(http:// ...
- [pthread]Linux C 多线程简单示例
#include <stdio.h> #include <pthread.h> pthread_mutex_t mutex; pthread_cond_t cond; void ...
- 深入理解Java AIO(三)—— Linux中的AIO实现
我们调用的Java AIO底层也是要调用OS的AIO实现,而OS主要也就Windows和Linux这两大类,当然还有Solaris和mac这些小众的. 在 Windows 操作系统中,提供了一个叫做 ...
- Linux 驱动开发
linux驱动开发总结(一) 基础性总结 1, linux驱动一般分为3大类: * 字符设备 * 块设备 * 网络设备 2, 开发环境构建: * 交叉工具链构建 * NFS和tftp服务器安装 3, ...
- 深入理解 iOS 开发中的锁
来源:伯乐在线 - 夏天然后 链接:http://ios.jobbole.com/89474/ 点击 → 申请加入伯乐在线专栏作者 摘要 本文的目的不是介绍 iOS 中各种锁如何使用,一方面笔者没有大 ...
- 线程(thread)
线程(thread): 现代操作系统引入进程概念,为了并发(行)任务 1.进程之间的这种切换代价很高 2.通信方式的代价也很大基本概念: 1.线程是比进程更小的资源单位,它是进程中的一个执行路线(分支 ...
- java框架---->quartz的使用(一)
Quartz 是个开源的作业调度框架,为在 Java 应用程序中进行作业调度提供了简单却强大的机制.今天我们就来学习一下它的使用,这里会分篇章对它进行介绍.只是希望能有个人,在我说没事的时候,知道我不 ...
- linux的<pthread.h>
转自:http://blog.sina.com.cn/s/blog_66cc44d00100in5b.html Linux系统下的多线程遵循POSIX线程接口,称为pthread.编写Linux下的多 ...
- linux,pthread(转)
互斥量.条件变量与pthread_cond_wait()函数的使用,详解(二) 1.Linux“线程” 进程与线程之间是有区别的,不过linux内核只提供了轻量进程的支持,未实现线程模型.Linu ...
随机推荐
- hadoop中URI理解
1)在编写MR程序的时候经常会有如下代码: String uri=“....”: Configuration conf=new Configuration(): FileSystem fs=FileS ...
- 冒泡排序--注意flag变量的设置
代码: #include<stdio.h> void BubbleSort(int a[],int n){ int i,j; int temp; ; // 此处flag变量的设置可以提高算 ...
- 自问自答:在VB中如何实现像C++一样printf的功能
问: 每个整型都转换成5位的字符串,不足的在前面补0.比如:1转换成“00001”,10转换成“00010”.怎么实现? 答: format(1,"00000") from: 百度 ...
- maven-pom-properties
出处: http://blog.csdn.net/taiyangdao/article/details/52358083
- JNI之常用函数大全
要素 :1. 该函数大全是基于C语言方式的,对于C++方式可以直接转换 ,例如,对于生成一个jstring类型的方法转换分别如下: C编程环境中使用方法为:(*env) ->NewString ...
- delphi中使用mediaplayer控件播放音乐
新建一个名字为media的文件夹,把要播放的音乐文件放在这个文件夹里. ExtractFilePath是用来获得产生的可执行程式所在的路径,因为我们把要播放的音乐文件放在了可执行程式的那个根目录下. ...
- mysql group_concat函数
函数语法: group_concat( [DISTINCT] 要连接的字段 [Order BY 排序字段 ASC/DESC] [Separator '分隔符'] ) 下面举例说明: select * ...
- 配置kubernetes UI图形化界面
配置Kubernetes网络 在master和nodes上都需要安装flannel yum install flannel 在master和nodes上都需要配置flannel vi /etc/sys ...
- 12、Django简易框架
安装: tar -zxvf Django-1.5.1.tar.gz cd Django-1.5.1 [root@likun Django-1.5.1]# ls [root@likun Djang ...
- ISP图像调试工程师——边缘增强(熟悉图像预处理和后处理技术)
http://blog.csdn.net/u013033431/article/details/50907907 http://dsqiu.iteye.com/blog/1638589 概念: 图像增 ...