#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. Linux批量管理工具Ansible

    Ansible-批量linux管理工具:https://github.com/ansible/ansible Ansible有如下优点: 1.轻量级,他不需要去客户端安装agent,更新时,只需要在操 ...

  2. tcpreplay 发包速率控制算法研究

    一.  序 1.1  tcpreplay历史 Tcpreplay 的作者是Aaron Turner,该项目开始于2000年,早期的功能是对tcpdump等抓包工具生成的网络包(即pcap文件)的回放, ...

  3. ext js layout and tree

    数据     <configuration> <configSections> <section name="hibernate-configuration&q ...

  4. Linux tftp配置

    TFTP是用来下载远程文件的最简单网络协议,它其于UDP协议而实现. xinetd(eXtended InterNET services daemon)是新一代的网络守护进程服务程序,又叫超级INTE ...

  5. javascript快速入门16--表格

    表格的层次结构 <table border="1"> <caption>表格标题</caption> <thead> <tr& ...

  6. javascript快速入门11--正则表达式

    正则表达式可以: 测试字符串的某个模式.例如,可以对一个输入字符串进行测试,看在该字符串是否存在一个电话号码模式或一个信用卡号码模式.这称为数据有效性验证 替换文本.可以在文档中使用一个正则表达式来标 ...

  7. (原创)sklearn中 F1-micro 与 F1-macro区别和计算原理

    最近在使用sklearn做分类时候,用到metrics中的评价函数,其中有一个非常重要的评价函数是F1值,(关于这个值的原理自行google或者百度) 在sklearn中的计算F1的函数为 f1_sc ...

  8. http://blog.chinaunix.net/uid-20577907-id-3519578.html

    http://blog.chinaunix.net/uid-20577907-id-3519578.html

  9. 使用Python开发SQLite代理服务器(转载)

    转载:https://mp.weixin.qq.com/s?timestamp=1498531736&src=3&ver=1&signature=Eq6DPvkuGJi*G5s ...

  10. Java笔记4:JDBC纯驱动方式连接Oracle

    JDBC纯驱动方式连接Oracle 1 下载Oracle提供的驱动程序包 下载地址: http://www.oracle.com/technetwork/database/enterprise-edi ...