本文整理汇总了C++中thread_safe_queue类的典型用法代码示例。如果您正苦于以下问题:C++ thread_safe_queue类的具体用法?C++ thread_safe_queue怎么用?C++ thread_safe_queue使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了thread_safe_queue类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: producer
void producer(thread_safe_queue<int> &q, unsigned int num_iterations, unsigned int num_consumers)
{
std::cout << "producer" << std::endl;
for(int i = 0; i < num_iterations; ++i)
q.push(i);
for(int i = 0; i < num_consumers; ++i)
q.push(-1);
}
示例2: pop_thread
void pop_thread(thread_safe_queue<long long int>& q)
{
for(int i = 0; i < 10000; ++i)
{
long long int a;
q.wait_and_pop(a);
}
}
示例3: push_thread
void push_thread(thread_safe_queue<long long int>& q, long long int j)
{
long long int start = queue_size * j;
for(long long int i = start; i < start + queue_size; ++i)
{
q.push(i);
}
}
示例4: push_thread
void push_thread(thread_safe_queue<long long int>& q, long long int j)
{
long long int start = 10000 * j;
for(long long int i = start; i < start + 10000; ++i)
{
q.push(i);
}
}
示例5: consume
void consume(thread_safe_queue<int>& tasks, mutex& writemtx, int num){
srand((unsigned int)time(NULL));
while (true) {
int mod;
tasks.pop(mod);
writemtx.lock();
cout <<mod <<" "<< (mod^rand()) << " " << num << "\n"; //âîçâðàùàåò â ñòåïåíü ñëó÷àéíîãî ÷èñëà
writemtx.unlock();
}
}
示例6: consumer
void consumer(thread_safe_queue<int> &q)
{
int value;
while(true)
{
q.pop(value);
if (value == -1)
{
std::cout << "consumer quits" << std::endl;
break;
}
else
{
std::cout << "pop: " << value << std::endl;
}
}
}
示例7: produce
void produce(thread_safe_queue<int>& tasks){
for (int i = 1; i < 5600; i++) {
tasks.enqueue(i);
}
tasks.shutdown();
}
示例8: produsers_func
void produsers_func()
{
srand(magic_number);
int rand_number;
for (size_t i = 0; i < 100; i++)
{
rand_number = 0 + rand() % 1000;
try
{
my_queue.enqueue(rand_number);
}
catch (std::exception & e) {
std::cout << e.what() << std::endl;
}
}
//to stop all threads
for (size_t i = 0; i < threads_number; i++)
{
try
{
my_queue.enqueue(stop_flag);
}
catch (std::exception & e) {
std::cout << e.what() << std::endl;
}
}
my_queue.shutdown();
std::cout << "producer has finished his work\n";
}
示例9: cti_callout
void voice_card_control::cti_callout(boost::shared_ptr<cti_call_out_param> cti_call_out_param_)
{
std::size_t chID;
try
{
chID = m_channel_queue.take();
}
catch (std::out_of_range)
{
BOOST_LOG_SEV(cia_g_logger, RuntimeInfo) << "业务流水:" << cti_call_out_param_->m_transId << " 获取通道失败, 通道全部繁忙";
//继续下一次呼叫
cti_callout_again(cti_call_out_param_);
return;
}
BOOST_LOG_SEV(cia_g_logger, RuntimeInfo) << "业务流水:" << cti_call_out_param_->m_transId << " 获取到得通道状态为:" << SsmChkAutoDial(chID) << ", 通道号码:" << chID;
SsmSetTxCallerId(chID, cti_call_out_param_->m_authCode.c_str());
if (SsmAutoDial(chID, cti_call_out_param_->m_pn.c_str()) == 0){
BOOST_LOG_SEV(cia_g_logger, RuntimeInfo) << "业务流水:" << cti_call_out_param_->m_transId << " 已发送请求, 已将此通道对应状态清空, 通道号码:" << chID;
boost::shared_ptr<trunk> t = m_trunk_vector.at(chID);
boost::unique_lock<boost::mutex> unique_lock_(t->m_trunk_mutex, boost::defer_lock);
if (unique_lock_.try_lock())
{
t->m_client_socket = cti_call_out_param_->m_base_client;
t->m_transId = cti_call_out_param_->m_transId;
t->m_caller_id = cti_call_out_param_->m_authCode;
t->m_called_id = cti_call_out_param_->m_pn;
t->m_hungup_by_echo_tone = cti_call_out_param_->m_hungup_by_echo_tone;
t->m_step = TRK_CALLOUT_DAIL;
t->m_callTime.restart();
}
else
{
BOOST_LOG_SEV(cia_g_logger, Critical) << "业务流水:" << cti_call_out_param_->m_transId << ", 严重异常, 被分配的语音通道处于占用状态, 请程序猿通宵解决问题";
}
}
else {
m_channel_queue.put(chID);
//上一次呼叫失败,继续呼叫
if (cti_call_out_param_->m_repeat_call_out)
{
cti_call_out_param_->m_repeat_call_out = false;
BOOST_LOG_SEV(cia_g_logger, RuntimeInfo) << "业务流水:" << cti_call_out_param_->m_transId << "上一次呼叫失败,继续呼叫";
cti_callout_again(cti_call_out_param_);
}
//已经连续两次呼叫失败, 直接返回失败
else
{
BOOST_LOG_SEV(cia_g_logger, RuntimeInfo) << "业务流水:" << cti_call_out_param_->m_transId << "已经连续两次呼叫失败, 直接返回失败";
ciaMessage msg;
msg.set_type(CIA_CALL_RESPONSE);
msg.set_transid(cti_call_out_param_->m_transId);
msg.set_status(CIA_CALL_FAIL);
cti_call_out_param_->m_base_client->do_write(chat_message(msg));
return;
}
}
}
示例10: cti_callout
void voice_card_control::cti_callout(boost::shared_ptr<cti_call_out_param> cti_call_out_param_)
{
std::size_t chID;
ciaMessage& msg_ = cti_call_out_param_->m_ch_msg->m_procbuffer_msg;
try
{
chID = m_channel_queue.take();
}
catch (std::out_of_range)
{
//BOOST_LOG_SEV(cia_g_logger, RuntimeInfo) << "业务流水:" << msg_.transid() << " 获取通道失败, 通道全部繁忙";
//继续下一次呼叫
cti_callout_again(cti_call_out_param_);
return;
}
int ch_state = SsmGetChState(chID);
if (ch_state != 0 && ch_state != 123) //空闲或本地闭塞
{
BOOST_LOG_SEV(cia_g_logger, RuntimeInfo) << "业务流水:" << msg_.transid() << " , 获取到得通道状态为:" << ch_state << ", 不可用,将此通道重新放回。通道号码:" << chID;
m_channel_queue.put(chID);
cti_callout_again(cti_call_out_param_);
return;
}
//BOOST_LOG_SEV(cia_g_logger, RuntimeInfo) << "业务流水:" << msg_.transid() << ", 获取到的通道号码:" << chID;
SsmSetTxCallerId(chID, msg_.authcode().c_str());
boost::shared_ptr<trunk> t = m_trunk_vector.at(chID);
boost::unique_lock<boost::mutex> unique_lock_(t->m_trunk_mutex);
if (SsmAutoDial(chID, msg_.pn().c_str()) == 0){
BOOST_LOG_SEV(cia_g_logger, RuntimeInfo) << "业务流水:" << msg_.transid() << " 已发送请求, 已将此通道对应状态清空, 通道号码:" << chID;
t->reset_trunk(cti_call_out_param_);
}
else {
show_error();
unique_lock_.unlock();
m_channel_queue.put(chID);
//上一次呼叫失败,继续呼叫
if (cti_call_out_param_->m_repeat_call_out)
{
cti_call_out_param_->m_repeat_call_out = false;
BOOST_LOG_SEV(cia_g_logger, RuntimeInfo) << "业务流水:" << msg_.transid() << "上一次呼叫失败,继续呼叫, 失败原因向上查询SsmGetLastErrMsg";
cti_callout_again(cti_call_out_param_);
}
//已经连续两次呼叫失败, 直接返回失败
else
{
BOOST_LOG_SEV(cia_g_logger, RuntimeInfo) << "业务流水:" << msg_.transid() << "已经连续两次呼叫失败, 直接返回失败, 失败原因向上查询SsmGetLastErrMsg";
std::string transid_ = msg_.transid();
msg_.Clear();
msg_.set_type(CIA_CALL_RESPONSE);
msg_.set_transid(transid_);
msg_.set_status(CIA_CALL_FAIL);
cti_call_out_param_->m_base_client->do_write(cti_call_out_param_->m_ch_msg);
return;
}
}
}
示例11: producer_function
// функция для поставщика
void producer_function()
{
int curTask = 0;
while (curTask < tasks)
{
safeQueue.enqueue(rand()%100);
curTask++;
}
//shutdown consumers
for (int i = 0; i < numberOfConsumers; i++)
safeQueue.enqueue(-1);
}
示例12: deal_hungup_strategy
void voice_card_control::deal_hungup_strategy()
{
int chID;
while (true)
{
try
{
chID = m_sleep_channel_queue.take();
}
catch (std::out_of_range)
{
boost::this_thread::sleep_for(boost::chrono::milliseconds(100));
continue;
}
boost::shared_ptr<trunk> t = m_trunk_vector.at(chID);
t->m_trunk_mutex.lock();
int sleeping_elapse = m_cti_sleeping_elapse - t->elpased();
if (sleeping_elapse > 0)
{
BOOST_LOG_SEV(cia_g_logger, RuntimeInfo) << "业务流水:" << t->m_transId << " 通道号: " << chID
<< "触发延迟挂机条件, 开始休眠" << sleeping_elapse << "毫秒";
boost::this_thread::sleep_for(boost::chrono::milliseconds(sleeping_elapse));
}
else
{
BOOST_LOG_SEV(cia_g_logger, RuntimeInfo) << "业务流水:" << t->m_transId << " 通道号: " << chID
<< "睡个毛, 去干活. 睡眠时间为:" << sleeping_elapse << "毫秒";
}
t->m_step = TRK_HUNGUP;
t->m_trunk_mutex.unlock();
cti_hangUp(chID, CIA_CALL_SUCCESS);
}
}
示例13: init_cti
void voice_card_control::init_cti()
{
BOOST_LOG_SEV(cia_g_logger, RuntimeInfo) << "读取语音卡配置文件";
if (SsmStartCti("ShConfig.ini", "ShIndex.ini") != 0)
{
show_error();
BOOST_LOG_SEV(cia_g_logger, Critical) << "读取语音卡配置文件失败";
throw std::runtime_error("读取语音卡配置文件失败");
}
else{
BOOST_LOG_SEV(cia_g_logger, RuntimeInfo) << "语音卡配置文件读取成功";
}
//设置驱动程序抛出事件的模式
EVENT_SET_INFO EventMode;
EventMode.dwWorkMode = EVENT_CALLBACK; // 事件回调模式
EventMode.lpHandlerParam = (LPVOID)voice_card_control::cti_callback; // 注册回调函数
SsmSetEvent(0xffff, -1, true, &EventMode); // 启动事件触发模式
//如果要在程序中获取SS7消息, 则需要同时注意以下两点
//配置文件 ShConfig.ini 中, GetMsuOnAutoHandle = 1
//程序中: SsmSetEvent(E_RCV_Ss7Msu, -1, true, &EventMode);
SsmSetEvent(E_RCV_Ss7Msu, -1, true, &EventMode);
BOOST_LOG_SEV(cia_g_logger, RuntimeInfo) << "正在初始化语音卡通道, 预期耗时10秒";
while (SsmSearchIdleCallOutCh(160, 0x1E0000) < 0){ // 循环等待, 直到能够获取语音卡空闲通道号, 语音卡初始化完毕
boost::this_thread::sleep_for(boost::chrono::milliseconds(500));
}
BOOST_LOG_SEV(cia_g_logger, RuntimeInfo) << "语音卡通道初始化完毕";
for (std::size_t i = 0; i < m_numChannelCount; i++){
m_channel_queue.put(i);
SsmBlockRemoteCh(i); // 设置通道远端堵塞(禁止来话,但仍可以呼出)
}
}
示例14: consumer_function
//функция для потребителя(который обрабатывает данные от поставщика)
void consumer_function(int consumerId)
{
int number;
while (true)
{
safeQueue.pop(number);
if (number == -1)
{
//безопасная печать
std::lock_guard<std::mutex> lock(printing_mutex);
std::cout << "consumer is shutdowning" << std::endl;
break;
}
else
{
std::lock_guard<std::mutex> lock(printing_mutex);
if (isPrime(number) == true)
{
std::cout << number << " is prime" << std::endl;
}
else
{
std::cout << number << " is not prime" << std::endl;
}
}
}
}
示例15: cti_hangUp
int voice_card_control::cti_hangUp(std::size_t channelID, std::string status)
{
int retVal = 0;
boost::shared_ptr<trunk> t = m_trunk_vector.at(channelID);
boost::unique_lock<boost::mutex> unique_lock_(t->m_trunk_mutex, boost::defer_lock);
if (!unique_lock_.try_lock())
{
BOOST_LOG_SEV(cia_g_logger, Warning) << "cti_hangUp函数尝试锁定通道失败, 放弃挂机, 当前通道Step值为:" << t->m_step;
return -1;
}
else
{
switch (t->m_step)
{
case TRK_IDLE:
BOOST_LOG_SEV(cia_g_logger, Warning) << "依据当前通道状态判断可能出现重复挂机, 请在后续版本调查原因, 通道号码:" << channelID;
return -1;
case TRK_SLEEP:
BOOST_LOG_SEV(cia_g_logger, Warning) << "当前通道正在延迟挂机, 通道号码:" << channelID;
return -1;
default:
break;
}
if (!t->m_call_out_param->m_hungup_by_echo_tone)
{
BOOST_LOG_SEV(cia_g_logger, CalloutMsg) << "由于设置非响一声挂断, 未挂断本次呼叫请求, 等待超时挂断, 通道号码:" << channelID;
return -1;
}
retVal = SsmHangup(channelID);
if (retVal == -1){
show_error();
BOOST_LOG_SEV(cia_g_logger, Warning) << "挂机失败, 请在后续版本调查原因, 通道号码:" << channelID;
}
else{
BOOST_LOG_SEV(cia_g_logger, CalloutMsg) << "已挂断本次呼叫请求, 通道号码:" << channelID;
}
ciaMessage& msg_ = t->m_call_out_param->m_ch_msg->m_procbuffer_msg;
std::string transid_ = msg_.transid();
msg_.Clear();
msg_.set_type(CIA_CALL_RESPONSE);
msg_.set_transid(transid_);
msg_.set_status(status);
if (t->m_call_out_param == nullptr)
{
BOOST_LOG_SEV(cia_g_logger, Critical) << "严重异常: 挂机时发现客户端socket为空";
}
else
{
LOG_MSG_QUEUE.put(boost::make_shared<LOG_MSG>(Debug, "呼叫返回数据:\n" + t->m_call_out_param->m_ch_msg->m_procbuffer_msg.DebugString()));
t->m_call_out_param->m_base_client->do_write(t->m_call_out_param->m_ch_msg);
}
t->realseTrunk();
unique_lock_.unlock();
m_channel_queue.put(channelID);
}
return retVal;
}