ACE线程管理(一)

这是一个官方的例子,这里先做记录,主要是创建两个线程(读写)

线程A:为读:

线程B为写:

当线程B写完后再进行读取

下面上代码

// $Id: auto_event.cpp 97383 2013-10-23 08:44:20Z mhengstmengel $

// This test shows the use of an ACE_Auto_Event as a signaling
// mechanism. Two threads are created (one a reader, the other a
// writer). The reader waits till the writer has completed
// calculations. Upon waking up the reader prints the data calculated
// by the writer. The writer thread calculates the value and signals
// the reader when the calculation completes.

#include "ace/OS_NS_unistd.h"
#include "ace/OS_main.h"
#include "ace/Service_Config.h"
#include "ace/Auto_Event.h"
#include "ace/Singleton.h"
#include "ace/Thread_Manager.h"

#if defined (ACE_HAS_THREADS)
// Shared event between reader and writer.  The ACE_Thread_Mutex is
// necessary to make sure that only one ACE_Auto_Event is created.
// The default constructor for ACE_Auto_Event sets it initially into
// the non-signaled state.

typedef ACE_Singleton <ACE_Auto_Event, ACE_Thread_Mutex> EVENT;

// work time for writer
static int work_time;

// Reader thread.
static void *reader(void *arg)
{
	// Shared data via a reference.
	int& data = *(int *)arg;

	// Wait for writer to complete.

	ACE_DEBUG((LM_DEBUG, "(%t) reader: waiting......\n"));
        <pre name="code" class="cpp">         //等待信号
	if (EVENT::instance()->wait() == -1)
	{
		ACE_ERROR((LM_ERROR, "thread wait failed"));
		ACE_OS::exit(0);
	}

	// Read shared data.
	ACE_DEBUG((LM_DEBUG, "(%t) reader: value of data is: %d\n", data));

	return 0;
}

// Writer thread.
static void *writer(void *arg)
{
	int& data = *(int *)arg;

	// Calculate (work).
	ACE_DEBUG((LM_DEBUG, "(%t) writer: working for %d secs\n", work_time));
	ACE_OS::sleep(work_time);

	// Write shared data.
	data = 42;

	// Wake up reader.
	ACE_DEBUG((LM_DEBUG, "(%t) writer: calculation complete, waking reader\n"));
        //创建并发送信号
	if (EVENT::instance()->signal() == -1)
	{
		ACE_ERROR((LM_ERROR, "thread signal failed"));
		ACE_OS::exit(0);
	}

	return 0;
}

int ACE_TMAIN(int argc, ACE_TCHAR **argv)
{
	// Shared data: set by writer, read by reader.
	int data;

	// Work time for writer.
	work_time = argc == 2 ? ACE_OS::atoi(argv[1]) : 5;

	// threads manager
	ACE_Thread_Manager& tm = *ACE_Thread_Manager::instance();

	// Create reader thread.
	if (tm.spawn((ACE_THR_FUNC)reader, (void *)&data) == -1)
		ACE_ERROR_RETURN((LM_ERROR, "thread create for reader failed"), -1);

	// Create writer thread.
	if (tm.spawn((ACE_THR_FUNC)writer, (void *)&data) == -1)
		ACE_ERROR_RETURN((LM_ERROR, "thread create for writer failed"), -1);

	// Wait for both.
// 等待两者
	if (tm.wait() == -1)
		ACE_ERROR_RETURN((LM_ERROR, "thread wait failed"), -1);
	else
		ACE_DEBUG((LM_ERROR, "graceful exit\n"));

	return 0;
}

ACE_SINGLETON_TEMPLATE_INSTANTIATE(ACE_Singleton, ACE_Auto_Event, ACE_Thread_Mutex);


#else
int
ACE_TMAIN(int, ACE_TCHAR *[])
{
	ACE_ERROR((LM_ERROR, "threads not supported on this platform\n"));
	return 0;
}
#endif /* ACE_HAS_THREADS */


 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无痕Miss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值