#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. python3-开发面试题(python)6.23基础篇(2)

    1.请至少列举5个 PEP8 规范(越多越好). 一.代码编排 1.缩进.4个空格的缩进,不使用Tap,更不能混合使用Tap和空格 2.每行最大长度79,换行可以使用反斜杠,最好使用圆括号.换行点要在 ...

  2. Docker实践3: Docker常用命令(未完)

    查看容器及运行进程 docker ps 查看容器内部信息 docker inspect container_id 进入容器 docker attach container_id 退出容器 docker ...

  3. Win7用IIS发布网站系统 部署项目

    1.首先确保系统上已经安装IIS [控制面板]→[程序]→[程序和功能]→[打开或关闭Windows功能] 选中Internet 信息服务下面的所有选项,点击确定. 2. 获得发布好的程序文件 若没有 ...

  4. convert image to base64

    ylbtech-Unitity-cs:convert image to base64 convert image to base64 1.A,效果图返回顶部   1.B,源代码返回顶部 1.B.1,c ...

  5. 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:3.安装Oracle RAC-3.3.安装前检查

    3.3. 安装前检查 1.检查节点连通性 或:./runcluvfy.sh stage -pre crsinst -n linuxrac1,linuxrac2 -fixup -verbose [gri ...

  6. Linux GPRS模块问题

    这是一个硬件问题,不过被我这个学软件的给遇到了.很尴尬,纠结了很久. GPRS模块如果没有插上sim卡,开机之后一切正常.一旦插上卡之后开机大约过十秒钟之后会自动关机.并在串口上面打印一下信息: II ...

  7. 常用vim命令合集

    移动命令: h:左移 l:右移 k:上移 j:下移 ^:移动到本行第一个非空白字符上 0:移动到本行第一个字符上 gg:移动到文件头 G = shift + g:移动到文件尾 %:从一个"{ ...

  8. Yii2-核心框架代码规范

    1.概述 简单说,我们使用PSR-2兼容规范,所以应用于PSR-2的一切对我们的代码也同样适用. 文件必须使用 <?php 或 <?= 标签. 文件未尾应该有一个新行. PHP代码文件必须 ...

  9. jstack来分析。当linux出现cpu被java程序消耗过高时

    我们使用jdk自带的jstack来分析.当linux出现cpu被java程序消耗过高时,以下过程说不定可以帮上你的忙: 1.top查找出哪个进程消耗的cpu高 21125 co_ad2    18   ...

  10. angularJS 中的two-way data binding.

    原文: https://stackoverflow.com/questions/11616636/how-to-do-two-way-filtering-in-angularjs ---------- ...