#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. 【博弈论】【SG函数】【枚举】bzoj1188 [HNOI2007]分裂游戏

    因为第i个瓶子里的所有豆子都是等价的,设sg(i)表示第i个瓶子的sg值,可以转移到sg(j)^sg(k)(i<j<n,j<=k<n)的状态. 只需要考虑豆子数是奇数的瓶子啦, ...

  2. 【神奇の做法】bzoj2456 mode

    题解:http://www.tuicool.com/articles/BfQBzif #include<cstdio> using namespace std; int n,x,ans,t ...

  3. [CF468D]Tree

    [CF468D]Tree 题目大意: ​ 一棵\(n(n\le10^5)\)个编号为\(1\sim n\)的点的带边权的树,求一个排列\(p_{1\sim n}\),使\(\sum dis(i,p_i ...

  4. [CF864F]Cities Excursions

    题目大意: 一个$n(n\le3000)$个点的有向图,$q(q\le4\times10^5)$组询问,每次询问$s_i,t_i$之间是否存在一条字典序最小的路径(可以重复经过不为$t_i$的结点). ...

  5. 如何移除inline-block元素之间的空白

    我们想要的是<li>元素可以紧贴在一起,但是很显然,结果“出乎意料”.那么有什么方法可以让结果符合我们的预期呢?所能想到的解决方法至少有以下四种,而每种方法也都有其优劣所在,至于要如何选择 ...

  6. mybatis-xml特殊字符处理

    1. 使用CDATA区: 它的全称为character data,以"<![CDATA[ "开始,以" ]]>" 结束,在两者之间嵌入不想被解析程序 ...

  7. Maven多模块项目新建技巧-解决公共项目install之后可以在单独模块中直接编译

    说明:如果按照这种方式http://www.cnblogs.com/EasonJim/p/8303878.html,且按照常规的install方式在子项目中编译项目,那么需要先install一下par ...

  8. Admin Finder

    #Created for coded32 and his teamopenfire Eliminated Some bugs from my last code shared here as Gues ...

  9. Windows利用命令行快速清除以及建立密码

    我们Win10一般是没有管理员权限的!这就要求我们获取管理员权限了,一般有两种方法获取,我就介绍下面一种最简单的 老操作:WIn+R打开本窗口,输入:taskmgr 建立密码(administrato ...

  10. Tornado框架的初步使用

    Tornado的搭建很简单,使用pip,或者下载源码均可.   我们先看一个最简单的程序: import tornado.ioloop import tornado.web class MainHan ...