c++ map嵌套队列(队列嵌套结构体指针)ok
map中嵌套队列,队列中是结构体,有一个问题,结构体中的值不更新
#include <iostream>
#include <queue>
#include<map>
//#include <Windows.h>
using namespace std;
struct RevData{
int size;
char data[1000];
} recv_data;
map<string, queue<RevData>> amap;
HANDLE hMutex;
DWORD WINAPI Fun1(LPVOID)//创建多线程函数,函数返回值为DWORD WINAPI,一定要是这个,否则会出错
{
int index = 0;
while (true) {
char* aaa = "abcde";
WaitForSingleObject(hMutex, INFINITE);//获取等待资源,一直等待,知道获取"screen"资源后,才返回
queue<RevData> queue1 = amap[aaa];
RevData recvd;
recvd.size = index;
char* chard = "abcde";
memset(recvd.data, '\0', 1000);
memcpy(recvd.data, chard, 5);
//recvd.data = chard;
queue1.push(recvd);
amap[aaa] = queue1;
ReleaseMutex(hMutex);//释放资源独占函数
index++;
//Sleep(20);
}
}
DWORD WINAPI Fun2(LPVOID)//创建多线程函数,函数返回值为DWORD WINAPI,一定要是这个,否则会出错
{
while (true){
char* aaa = "abcde";
map<string, queue<RevData>>::iterator iter;
WaitForSingleObject(hMutex, INFINITE);//获取等待资源,一直等待,知道获取"screen"资源后,才返回
iter = amap.find(aaa);
if (iter != amap.end()) {
queue<RevData> queue1 = amap[aaa];
if (!queue1.empty()) {
RevData recv = queue1.front();
cout << recv.data << endl;
cout << recv.size << endl;
queue1.pop();
ReleaseMutex(hMutex);//释放资源独占函数
//Sleep(20);
}
}
else {
cout << "支线程1" << endl;
}
}
}
调用代码:
AllocConsole();
freopen("CONOUT$", "w", stdout);
hMutex = CreateMutex(NULL, FALSE, _T("screen"));
queue<RevData> queue1;
char* aaa = "abcde";
amap[aaa] = queue1;
HANDLE handle1 = CreateThread(NULL, 0, Fun1, NULL, 0, NULL);//创建多线程
//hMutex = CreateMutex(NULL, FALSE, "screen");//创建资源独占函数CreateMutex,该函数的功能是独占一个资源或者线程
CloseHandle(handle1);//释放线程
HANDLE handle2 = CreateThread(NULL, 0, Fun2, NULL, 0, NULL);//创建多线程
//hMutex = CreateMutex(NULL, FALSE, "screen");//创建资源独占函数CreateMutex,该函数的功能是独占一个资源或者线程
CloseHandle(handle2);//释放线程
问题原因:队列中是对象,等于号时,是复制对象,不是地址传递,queue<RevData> queue1 = amap[aaa];
这句代码中,queue1并不是map中的队列,而是map中队列的拷贝,所以pop也不是map中队列的pop,
如果不用指针,解决方法是再设置一下,代码如下:
DWORD WINAPI Fun2(LPVOID)//创建多线程函数,函数返回值为DWORD WINAPI,一定要是这个,否则会出错
{
while (true){
Sleep(20);
char* aaa = "abcde";
map<string, queue<RevData>>::iterator iter;
WaitForSingleObject(hMutex, INFINITE);//获取等待资源,一直等待,知道获取"screen"资源后,才返回
iter = amap.find(aaa);
if (iter != amap.end()) {
//queue<RevData> queue1 = amap[aaa];
if (!amap[aaa].empty()) {
RevData recv = amap[aaa].front();
cout << recv.data << endl;
cout << recv.size << endl;
int lenga = amap[aaa].size();
amap[aaa].pop();
int lenga2 = amap[aaa].size();
ReleaseMutex(hMutex);//释放资源独占函数
}
}
else {
cout << "支线程1" << endl;
}
}
}
队列里面是指针:
#include <iostream>
#include <queue>
#include<map>
//#include <Windows.h>
using namespace std;
struct RevData{
int size;
char data[1000];
} recv_data;
map<string, queue<RevData*>> amap;
HANDLE hMutex;
DWORD WINAPI Fun1(LPVOID)//创建多线程函数,函数返回值为DWORD WINAPI,一定要是这个,否则会出错
{
int index = 0;
while (true) {
char* aaa = "abcde";
WaitForSingleObject(hMutex, INFINITE);//获取等待资源,一直等待,知道获取"screen"资源后,才返回
queue<RevData*> queue1 = amap[aaa];
RevData recvd;
recvd.size = index;
char* chard = "abcde";
memset(recvd.data, '\0', 1000);
memcpy(recvd.data, chard, 5);
//recvd.data = chard;
queue1.push(&recvd);
amap[aaa] = queue1;
ReleaseMutex(hMutex);//释放资源独占函数
index++;
//Sleep(20);
}
}
DWORD WINAPI Fun2(LPVOID)//创建多线程函数,函数返回值为DWORD WINAPI,一定要是这个,否则会出错
{
while (true)
{
char* aaa = "abcde";
map<string, queue<RevData*>>::iterator iter;
WaitForSingleObject(hMutex, INFINITE);//获取等待资源,一直等待,知道获取"screen"资源后,才返回
iter = amap.find(aaa);
if (iter != amap.end()) {
queue<RevData*> queue1 = amap[aaa];
if (!queue1.empty()) {
RevData* recv = queue1.front();
cout << recv->data << endl;
cout << recv->size << endl;
queue1.pop();
ReleaseMutex(hMutex);//释放资源独占函数
//Sleep(20);
}
}
else {
cout << "支线程1" << endl;
}
}
}
AllocConsole();
freopen("CONOUT$", "w", stdout);
hMutex = CreateMutex(NULL, FALSE, _T("screen"));
queue<RevData*> queue1;
char* aaa = "abcde";
amap[aaa] = queue1;
HANDLE handle1 = CreateThread(NULL, 0, Fun1, NULL, 0, NULL);//创建多线程
//hMutex = CreateMutex(NULL, FALSE, "screen");//创建资源独占函数CreateMutex,该函数的功能是独占一个资源或者线程
CloseHandle(handle1);//释放线程
HANDLE handle2 = CreateThread(NULL, 0, Fun2, NULL, 0, NULL);//创建多线程
//hMutex = CreateMutex(NULL, FALSE, "screen");//创建资源独占函数CreateMutex,该函数的功能是独占一个资源或者线程
CloseHandle(handle2);//释放线程
下面是失败的,map中放队列指针,队列指针中放结构体指针
#include <iostream>
#include <queue>
#include<map>
#include <string>
#include <mutex>
//#include <Windows.h>
using namespace std;
struct RevData{
int size;
char data[1000];
} recv_data;
map<string, queue<RevData*>*> amap;
HANDLE hMutex;
DWORD WINAPI Fun1(LPVOID)//创建多线程函数,函数返回值为DWORD WINAPI,一定要是这个,否则会出错
{
int index = 0;
while (true) {
char* aaa = "abcde";
WaitForSingleObject(hMutex, INFINITE);//获取等待资源,一直等待,知道获取"screen"资源后,才返回
queue<RevData*>* queue1 = amap[aaa];
RevData recvd;
RevData *recvp;
char* chard = "abcde";
//memset(&recvd, '\0', sizeof(struct RevData));
strcpy(recvd.data, chard);
recvd.size = index;
//recvd.data = chard;
recvp = (RevData*)malloc(sizeof(RevData));
memcpy(recvp, &recvd, sizeof(RevData));
queue1->push(recvp);
index++;
Sleep(50);
//amap[aaa] = queue1;
ReleaseMutex(hMutex);//释放资源独占函数
}
}
DWORD WINAPI Fun2(LPVOID)//创建多线程函数,函数返回值为DWORD WINAPI,一定要是这个,否则会出错
{
while (true){
char* aaa = "abcde";
WaitForSingleObject(hMutex, INFINITE);//获取等待资源,一直等待,知道获取"screen"资源后,才返回
map<string, queue<RevData*>*>::iterator iter;
iter = amap.find(aaa);
if (iter != amap.end()) {
queue<RevData*>* queue1 = amap[aaa];
if (!queue1->empty()) {
RevData* recv = queue1->front();
cout << (*recv).data << endl;
cout << recv->size << endl;
int lenga = queue1->size();
queue1->pop();
int lenga2 = queue1->size();
}
}
else {
cout << "支线程1" << endl;
}
Sleep(50);
ReleaseMutex(hMutex);//释放资源独占函数
}
}
调用代码:
hMutex = CreateMutex(NULL, FALSE, _T("screen"));
queue<RevData*> queue1;
queue<RevData*>* queue2;
for (int i = 0; i < 10; i++) {
RevData recvD;
recvD.size = i;
strcpy(recvD.data, "hello");
RevData* recvp = (RevData*)malloc(sizeof(RevData));
memcpy(recvp, &recvD, sizeof(RevData));
queue1.push(recvp);// recvp);
//queue1.push(&recvD);
}
//queue<RevData*> queue1;
char* aaa = "abcde";
queue2 = (queue<RevData*>*)malloc(sizeof(queue<RevData*>));
memcpy(queue2, &queue1, sizeof(queue<RevData*>));
amap[aaa] = queue2;
HANDLE handle1 = CreateThread(NULL, 0, Fun1, NULL, 0, NULL);//创建多线程
//hMutex = CreateMutex(NULL, FALSE, "screen");//创建资源独占函数CreateMutex,该函数的功能是独占一个资源或者线程
CloseHandle(handle1);//释放线程
HANDLE handle2 = CreateThread(NULL, 0, Fun2, NULL, 0, NULL);//创建多线程
//hMutex = CreateMutex(NULL, FALSE, "screen");//创建资源独占函数CreateMutex,该函数的功能是独占一个资源或者线程
CloseHandle(handle2);//释放线程
文章来源: blog.csdn.net,作者:网奇,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/jacke121/article/details/79366992
- 点赞
- 收藏
- 关注作者
评论(0)