有关多线程的一点点错误,请高手解答

高性能架构探索 2010-05-30 06:15:00
刚开始学多线程,有很地方不懂多,下面是我刚写的代码,有错误,高手帮我指点一下


#include <iostream>
#include <windows.h>

using namespace std;

struct Node
{
Node *next;
int data;
};

struct List
{
Node *head;
HANDLE mutex;
};

List *CreateList()
{
List *list = new List;
list->head = NULL;
list->mutex = CreateMutex(NULL, FALSE, NULL);

return list;
}

void AddNode(List *list, Node* node)
{

WaitForSingleObject(list->mutex, INFINITE);
node->next = list->head;
list->head = node;

ReleaseMutex(list->mutex);
}

void SwapList(List* &list1, List *&list2)
{
List *temp = NULL;
HANDLE handArry[2];

handArry[0] = list1->mutex;
handArry[1] = list1->mutex;

WaitForMultipleObjects(2, handArry, TRUE, INFINITE);

temp->head = list1->head;
list1->head = list2->head;
list2->head = temp->head;

ReleaseMutex(handArry[0]);
ReleaseMutex(handArry[0]);
}

int main()
{
List *list1 = CreateList();
List *list2 = CreateList();

Node *node1 = new Node;
node1->data = 1;
node1->next = NULL;

AddNode(list1, node1);
Node *node2 = new Node;
node2->data = 2;
node2->next = NULL;

AddNode(list2, node2);

SwapList(list1, list2);

cout << list1->head->data;
}
...全文
72 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
ligangdili 2010-05-31
  • 打赏
  • 举报
回复
void SwapList(List* &list1, List *&list2)
中的temp为空指针,不能被当作结构使用:temp->head
ligangdili 2010-05-31
  • 打赏
  • 举报
回复
#include <iostream>
#include <windows.h>

using namespace std;

struct Node
{
Node *next;
int data;
};

struct List
{
Node *head;
HANDLE mutex;
};

List *CreateList()
{
List *list = new List;
list->head = NULL;
list->mutex = CreateMutex(NULL, FALSE, NULL);

cout << "CreateList()" << endl;
return list;
}

void AddNode(List *list, Node* node)
{

WaitForSingleObject(list->mutex, INFINITE);
node->next = list->head;
list->head = node;

ReleaseMutex(list->mutex);
cout << "AddNode()" << endl;
}

void SwapList(List* &list1, List *&list2)
{
List *temp = NULL;
HANDLE handArry[2];

handArry[0] = list1->mutex;
handArry[1] = list2->mutex;

WaitForMultipleObjects(2, handArry, TRUE, INFINITE);

temp = list1;
list1->head = list2->head;
list2->head = temp->head;

ReleaseMutex(handArry[0]);
ReleaseMutex(handArry[0]);

cout << "SwapList()" << endl;
}

int main()
{
List *list1 = CreateList();
List *list2 = CreateList();

Node *node1 = new Node;
node1->data = 1;
node1->next = NULL;

AddNode(list1, node1);
Node *node2 = new Node;
node2->data = 2;
node2->next = NULL;

AddNode(list2, node2);

SwapList(list1, list2);

cout << list1->head->data;

return 0;
}

  • 打赏
  • 举报
回复

#include <iostream>
#include <windows.h>

using namespace std;

struct Node
{
Node *next;
int data;
};

struct List
{
Node *head;
HANDLE mutex;
};

List *CreateList()
{
List *list = new List;
list->head = NULL;
list->mutex = CreateMutex(NULL, FALSE, NULL);

return list;
}

void AddNode(List *list, Node* node)
{

WaitForSingleObject(list->mutex, INFINITE);
node->next = list->head;
list->head = node;

ReleaseMutex(list->mutex);
}

void SwapList(List* &list1, List *&list2)
{
List *temp = NULL;//Node *temp;
HANDLE handArry[2];

handArry[0] = list1->mutex;
handArry[1] = list1->mutex;

WaitForMultipleObjects(2, handArry, TRUE, INFINITE);

temp = list1->head;//mark
list1->head = list2->head;
list2->head = temp;//mark

ReleaseMutex(handArry[0]);
ReleaseMutex(handArry[1]);
}

int main()
{
List *list1 = CreateList();
List *list2 = CreateList();

Node *node1 = new Node;
node1->data = 1;
node1->next = NULL;

AddNode(list1, node1);
Node *node2 = new Node;
node2->data = 2;
node2->next = NULL;

AddNode(list2, node2);

SwapList(list1, list2);

cout << list1->head->data;
}

pengzhixi 2010-05-30
  • 打赏
  • 举报
回复
怎么解决的呢?
小楫轻舟 2010-05-30
  • 打赏
  • 举报
回复
今天高手都参加比赛去了,
论坛很冷清
小楫轻舟 2010-05-30
  • 打赏
  • 举报
回复
楼主自言自语。。。
  • 打赏
  • 举报
回复
没问题了,吼吼
  • 打赏
  • 举报
回复
ReleaseMutex(handArry[0]);
ReleaseMutex(handArry[1]);

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧