#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的更多相关文章

  1. ZT 为什么pthread_cond_t要和pthread_mutex_t同时使用 || pthread/Linux多线程编程

    为什么线程同步的时候pthread_cond_t要和pthread_mutex_t同时使用 (2009-10-27 11:07:23) 转载▼ 标签: 杂谈 分类: 计算机 举一个例子(http:// ...

  2. [pthread]Linux C 多线程简单示例

    #include <stdio.h> #include <pthread.h> pthread_mutex_t mutex; pthread_cond_t cond; void ...

  3. 深入理解Java AIO(三)—— Linux中的AIO实现

    我们调用的Java AIO底层也是要调用OS的AIO实现,而OS主要也就Windows和Linux这两大类,当然还有Solaris和mac这些小众的. 在 Windows 操作系统中,提供了一个叫做 ...

  4. Linux 驱动开发

    linux驱动开发总结(一) 基础性总结 1, linux驱动一般分为3大类: * 字符设备 * 块设备 * 网络设备 2, 开发环境构建: * 交叉工具链构建 * NFS和tftp服务器安装 3, ...

  5. 深入理解 iOS 开发中的锁

    来源:伯乐在线 - 夏天然后 链接:http://ios.jobbole.com/89474/ 点击 → 申请加入伯乐在线专栏作者 摘要 本文的目的不是介绍 iOS 中各种锁如何使用,一方面笔者没有大 ...

  6. 线程(thread)

    线程(thread): 现代操作系统引入进程概念,为了并发(行)任务 1.进程之间的这种切换代价很高 2.通信方式的代价也很大基本概念: 1.线程是比进程更小的资源单位,它是进程中的一个执行路线(分支 ...

  7. java框架---->quartz的使用(一)

    Quartz 是个开源的作业调度框架,为在 Java 应用程序中进行作业调度提供了简单却强大的机制.今天我们就来学习一下它的使用,这里会分篇章对它进行介绍.只是希望能有个人,在我说没事的时候,知道我不 ...

  8. linux的<pthread.h>

    转自:http://blog.sina.com.cn/s/blog_66cc44d00100in5b.html Linux系统下的多线程遵循POSIX线程接口,称为pthread.编写Linux下的多 ...

  9. linux,pthread(转)

    互斥量.条件变量与pthread_cond_wait()函数的使用,详解(二)   1.Linux“线程” 进程与线程之间是有区别的,不过linux内核只提供了轻量进程的支持,未实现线程模型.Linu ...

随机推荐

  1. 【状压DP】旅行商问题

    给定一张带权有向图,要求从顶点0出发,经过每个结点恰好一次后再返回0,求边权和的最小值. 2<=n<=15 0<=d(i,j)<=1000 样例 5 80 1 30 3 41 ...

  2. xcode中一些便捷用法@literals简写

    总结一下,新的属性绑定规则如下: ●  除非开发者在实现文件中提供getter或setter,否则将自动生成 ● 除非开发者同时提供getter和setter,否则将自动生成实例变量 ●  只要写了s ...

  3. awk-使用

    http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858470.html 命令格式: awk [-F field-separator] 'pat ...

  4. shell基本计算、逻辑运算、位运算详解

    转:http://blog.chinaunix.net/uid-8504518-id-3918531.html Shell 提供大量的基本运算操作,在脚本中非常有用.Shell 对您提供的算术表达式求 ...

  5. VUE -- vue.js中$watch的用法示例

    Vue.js 提供了一个方法 watch,它用于观察Vue实例上的数据变动.对应一个对象,键是观察表达式,值是对应回调.值也可以是方法名,或者是对象,包含选项. 在实例化时为每个键调用 $watch( ...

  6. Nice Messager隐私权政策

    重视用户的隐私.您在使用我们的服务时,我们可能会收集和使用您的相关信息.我们希望通过本<隐私政策>向您说明,在使用我们的服务时,我们如何收集.使用.储存和分享这些信息,以及我们为您提供的访 ...

  7. 8、面向对象class

    对象的概念同其他语言的对象相同 一个基本的类 #!/usr/bin/python class person: def hi(self,name): print 'Hello,%s'%name p1= ...

  8. Java程序运行的内存分配

    Java的内存分配 A:栈 存储局部变量 B:堆 存储所有new出来的 C:方法区(程序代码及方法相关) D:本地方法区(系统相关) E:寄存器(CPU使用) 注意: a:局部变量 在方法定义中或者方 ...

  9. ubuntu下wine操作usb串口

    呃,换成ubuntu后想玩下文曲星,貌似没有linux下的下载工具,只好用wine. 用的是ch340的usb转串口,不得不说ubuntu果然集大成,这些驱动都有了,用minicom设置设备为/dev ...

  10. maven 打包不全(xml,properties文件没打进包)解决方案

    在pom.xml的build标签中加入以下代码即可 <build> <resources>            <resource>               ...