C ++:使用互斥体和条件变量控制线程执行顺序 [英] C++: Control order of thread execution with mutex's and conditional variables
本文介绍了C ++:使用互斥体和条件变量控制线程执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此问题是对此问题的跟进。 a>。我想要线程执行一些工作,并按顺序传递句柄到下一个线程。当尝试执行以下代码时,我会在ConsoleApplication9.exe中获得
未处理的异常0x0F7C1F5F(msvcp120d.dll):0xC0000005:访问冲突读取位置0x00000004。
#includestdafx.h
#include< iostream>
#include< thread>
#include< mutex>
#include< chrono>
#include< condition_variable>
std :: mutex * m_pMutexs;
std :: condition_variable * m_pCVs;
int m_pCurrentWorker;
void func(int i)
{
int cvCurrentInd = i;
std :: mutex * pCurMutex =& m_pMutexs [cvCurrentInd];
std :: condition_variable * pCuCV =(std :: condition_variable *)(m_pCurrentWorker + i * sizeof(std :: condition_variable));
std :: unique_lock< std :: mutex> lk(m_pMutexs [i]);
while(i!= m_pCurrentWorker)
{
pCuCV-> wait(lk);
}
std :: cout<< 进入线程< std :: this_thread :: get_id()<< std :: endl;
std :: this_thread :: sleep_for(std :: chrono :: seconds(rand()%10));
std :: cout<< 留下螺纹< std :: this_thread :: get_id()<< std :: endl;
m_pCurrentWorker ++;
lk.unlock();
pCuCV-> notify_one();
}
int _tmain(int argc,_TCHAR * argv [])
{
m_pMutexs = new std :: mutex [3]
m_pCVs = new std :: condition_variable [3];
m_pCurrentWorker = 0;
srand((unsigned int)time(0));
std :: thread t1(func,0);
std :: thread t2(func,1);
std :: thread t3(func,2);
t1.join();
t2.join();
t3.join();
return 0;
}
解决方案
<$ c $
< c> std :: condition_variable * pCuCV =(std :: condition_variable *)(m_pCurrentWorker + i * sizeof(std :: condition_variable));
我认为你应该写:
std :: condition_variable * pCuCV =& m_pCVs [i];
整个函数可能是这样的:
void func(int i)
{
std :: mutex * pCurMutex =& m_pMutexs [i];
std :: condition_variable * pCuCV =& m_pCVs [i];
std :: unique_lock< std :: mutex> lk(m_pMutexs [i]);
while(i!= m_pCurrentWorker){
pCuCV-> wait(lk);
}
std :: cout<< 进入线程< std :: this_thread :: get_id()<< std :: endl;
std :: this_thread :: sleep_for(std :: chrono :: seconds(rand()%2));
std :: cout<< 留下螺纹< std :: this_thread :: get_id()<< std :: endl;
m_pCurrentWorker ++;
lk.unlock();
if(m_pCurrentWorker> 2){
return;
}
pCuCV =& m_pCVs [m_pCurrentWorker];
pCuCV-> notify_one();
}
This question is a follow up for this question. I want threads to perform some work and pass handle to the next thread in order. When trying to execute the following code, I get
Unhandled exception at 0x0F7C1F5F (msvcp120d.dll) in ConsoleApplication9.exe: 0xC0000005 : Access violation reading location 0x00000004.
#include "stdafx.h"
#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>
#include <condition_variable>
std::mutex* m_pMutexs;
std::condition_variable* m_pCVs;
int m_pCurrentWorker;
void func(int i)
{
int cvCurrentInd = i;
std::mutex* pCurMutex = &m_pMutexs[cvCurrentInd];
std::condition_variable* pCuCV = (std::condition_variable*)(m_pCurrentWorker + i*sizeof(std::condition_variable));
std::unique_lock<std::mutex> lk(m_pMutexs[i]);
while (i != m_pCurrentWorker)
{
pCuCV->wait(lk);
}
std::cout << "entered thread " << std::this_thread::get_id() << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(rand() % 10));
std::cout << "leaving thread " << std::this_thread::get_id() << std::endl;
m_pCurrentWorker++;
lk.unlock();
pCuCV->notify_one();
}
int _tmain(int argc, _TCHAR* argv[])
{
m_pMutexs = new std::mutex[3];
m_pCVs = new std::condition_variable[3];
m_pCurrentWorker = 0;
srand((unsigned int)time(0));
std::thread t1(func,0);
std::thread t2(func,1);
std::thread t3(func,2);
t1.join();
t2.join();
t3.join();
return 0;
}
解决方案
Have no idea what you're trying to do but
You're casting integer to pointer?
std::condition_variable* pCuCV = (std::condition_variable*)(m_pCurrentWorker + i*sizeof(std::condition_variable));
I think you should write instead:
std::condition_variable* pCuCV = &m_pCVs[i];
The whole function could be something like this:
void func(int i)
{
std::mutex* pCurMutex = &m_pMutexs[i];
std::condition_variable* pCuCV = &m_pCVs[i];
std::unique_lock<std::mutex> lk(m_pMutexs[i]);
while (i != m_pCurrentWorker) {
pCuCV->wait(lk);
}
std::cout << "entered thread " << std::this_thread::get_id() << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(rand() % 2));
std::cout << "leaving thread " << std::this_thread::get_id() << std::endl;
m_pCurrentWorker++;
lk.unlock();
if (m_pCurrentWorker > 2) {
return;
}
pCuCV = &m_pCVs[m_pCurrentWorker];
pCuCV->notify_one();
}
这篇关于C ++:使用互斥体和条件变量控制线程执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文