多執行緒環境下不安全的訊息佇列存取---執行緒不同步會造成隱患
前面, 我們把訊息佇列存取都放在主執行緒中, 而在實際應用中, 很多時候, 存訊息佇列在主執行緒, 取訊息佇列在其他執行緒(如網路執行緒)。 下面, 我們將之前的程式改為多執行緒程式:
結果為:#include <windows.h> #include <iostream> using namespace std; #define Rectangle MyRectangle // 避免Rectangle與Windows中的Rectangle衝突 // 物件的id值 typedef enum { ErrorId = -1, IntegerId = 1, PointId = 2, RectangeId = 3, }ObjectID; // 基類 struct Basic { ObjectID id; virtual Basic *copy() = 0; // 純虛擬函式 }; // 整數類 struct Integer : public Basic { int a; Basic *copy() { Integer *p = new Integer; p->a = ((Integer*)this)->a; p->id = ((Integer*)this)->id; return p; } }; // 點類 struct Point : public Basic { int x; int y; Basic *copy() { Point *p = new Point; p->x = ((Point*)this)->x; p->y = ((Point*)this)->y; p->id = ((Point*)this)->id; return p; } }; // 矩形類 struct Rectangle : public Basic { Point point; int width; int height; Basic *copy() { Rectangle *p = new Rectangle; p->point.x = ((Rectangle*)this)->point.x; p->point.y = ((Rectangle*)this)->point.y; p->width = ((Rectangle*)this)->width; p->height = ((Rectangle*)this)->height; p->id = ((Rectangle*)this)->id; return p; } }; // 抽象物件的共同點, 構造成新的結點, 便於連結 typedef struct node { node *next; Basic *pBasic; }Node; Node *head = NULL; // 指向第一結點(採用不帶頭結點的連結串列) // 往鏈式訊息佇列中塞訊息 Node *addToMsgQueue(Basic* pb) { Node *pn = new Node; Node *qn = NULL; Basic *p = pb->copy(); // 多型性 if(NULL == head) { head = pn; } else { qn = head; while(NULL != qn->next) { qn = qn->next; } qn->next = pn; } pn->pBasic = p; // 千萬別忘記啊 pn->next = NULL; // 千萬別忘記啊 return head; } // 從鏈式訊息佇列中取出訊息(結點) Node *getMsgFromQueue() { if(NULL == head) { return NULL; } Node *pn = head; head = head->next; return pn; } // 執行緒函式 DWORD WINAPI ThreadFun(LPVOID pM) { Node *p = NULL; // 從訊息佇列中取出訊息 while(1) { p = getMsgFromQueue(); if(NULL == p) { Sleep(100); continue; } // 對指標進行還原 switch(p->pBasic->id) { case IntegerId: { cout << ((Integer*)(p->pBasic))->a << endl; break; } case PointId: { cout << ((Point *)(p->pBasic))->x << endl; cout << ((Point *)(p->pBasic))->y << endl; break; } case RectangeId: { cout << ((Rectangle *)(p->pBasic))->point.x << endl; cout << ((Rectangle *)(p->pBasic))->point.y << endl; cout << ((Rectangle *)(p->pBasic))->width << endl; cout << ((Rectangle *)(p->pBasic))->height << endl; break; } default: { break; } } } return 0; } // 主執行緒 int main() { HANDLE handle = CreateThread(NULL, 0, ThreadFun, NULL, 0, NULL); CloseHandle(handle); // 定義三個物件並賦值 Integer i; Point po; Rectangle rect; i.id = IntegerId; po.id = PointId; rect.id = RectangeId; i.a = 11; po.x = 22; po.y = 33; rect.point.x = 44; rect.point.y = 55; rect.width = 66; rect.height = 77; // 塞入訊息佇列 while(1) { addToMsgQueue(&i); addToMsgQueue(&po); addToMsgQueue(&rect); Sleep(2000); } return 0; }
11
22
33
44
55
66
77
11
22
33
44
55
66
77
11
22
33
44
55
66
77
11
22
33
44
55
66
77
11
22
33
44
55
66
77
11
22
33
44
55
66
77
11
22
33
44
55
66
77
11
22
33
44
55
66
77
11
22
33
44
55
66
77
11
22
33
44
55
66
77
11
22
33
44
55
66
77
11
22
33
44
55
66
77
11
22
33
44
55
66
77
11
22
33
44
55
66
77
11
22
33
44
55
66
77
11
22
33
44
55
66
77
11
22
33
44
55
66
77
11
22
33
44
55
66
77
11
22
33
44
55
66
77
11
22
33
44
55
66
77
11
22
33
44
55
66
77
11
22
33
44
55
66
77
11
22
33
44
55
66
77
...
看似得到了正確的結果, 但是, 上述程式是有問題的, 加入主執行緒正在存, 子執行緒正在取, 那豈不是亂套了麼。 當然, 上面的程式僥倖沒有出現這種情況, 為了說明這種情況, 我在addToMsgQueue函式中, 故意加一個睡眠語句, 再看看:
程式結果為:#include <windows.h> #include <iostream> using namespace std; #define Rectangle MyRectangle // 避免Rectangle與Windows中的Rectangle衝突 // 物件的id值 typedef enum { ErrorId = -1, IntegerId = 1, PointId = 2, RectangeId = 3, }ObjectID; // 基類 struct Basic { ObjectID id; virtual Basic *copy() = 0; // 純虛擬函式 }; // 整數類 struct Integer : public Basic { int a; Basic *copy() { Integer *p = new Integer; p->a = ((Integer*)this)->a; p->id = ((Integer*)this)->id; return p; } }; // 點類 struct Point : public Basic { int x; int y; Basic *copy() { Point *p = new Point; p->x = ((Point*)this)->x; p->y = ((Point*)this)->y; p->id = ((Point*)this)->id; return p; } }; // 矩形類 struct Rectangle : public Basic { Point point; int width; int height; Basic *copy() { Rectangle *p = new Rectangle; p->point.x = ((Rectangle*)this)->point.x; p->point.y = ((Rectangle*)this)->point.y; p->width = ((Rectangle*)this)->width; p->height = ((Rectangle*)this)->height; p->id = ((Rectangle*)this)->id; return p; } }; // 抽象物件的共同點, 構造成新的結點, 便於連結 typedef struct node { node *next; Basic *pBasic; }Node; Node *head = NULL; // 指向第一結點(採用不帶頭結點的連結串列) // 往鏈式訊息佇列中塞訊息 Node *addToMsgQueue(Basic* pb) { Node *pn = new Node; Node *qn = NULL; Basic *p = pb->copy(); // 多型性 if(NULL == head) { head = pn; } else { qn = head; while(NULL != qn->next) { qn = qn->next; } Sleep(20); // 故意加的語句, 用於構造異常場景 qn->next = pn; } pn->pBasic = p; // 千萬別忘記啊 pn->next = NULL; // 千萬別忘記啊 return head; } // 從鏈式訊息佇列中取出訊息(結點) Node *getMsgFromQueue() { if(NULL == head) { return NULL; } Node *pn = head; head = head->next; return pn; } // 執行緒函式 DWORD WINAPI ThreadFun(LPVOID pM) { Node *p = NULL; // 從訊息佇列中取出訊息 while(1) { p = getMsgFromQueue(); if(NULL == p) { Sleep(100); continue; } // 對指標進行還原 switch(p->pBasic->id) { case IntegerId: { cout << ((Integer*)(p->pBasic))->a << endl; break; } case PointId: { cout << ((Point *)(p->pBasic))->x << endl; cout << ((Point *)(p->pBasic))->y << endl; break; } case RectangeId: { cout << ((Rectangle *)(p->pBasic))->point.x << endl; cout << ((Rectangle *)(p->pBasic))->point.y << endl; cout << ((Rectangle *)(p->pBasic))->width << endl; cout << ((Rectangle *)(p->pBasic))->height << endl; break; } default: { break; } } } return 0; } // 主執行緒 int main() { HANDLE handle = CreateThread(NULL, 0, ThreadFun, NULL, 0, NULL); CloseHandle(handle); // 定義三個物件並賦值 Integer i; Point po; Rectangle rect; i.id = IntegerId; po.id = PointId; rect.id = RectangeId; i.a = 11; po.x = 22; po.y = 33; rect.point.x = 44; rect.point.y = 55; rect.width = 66; rect.height = 77; // 塞入訊息佇列 while(1) { addToMsgQueue(&i); addToMsgQueue(&po); addToMsgQueue(&rect); Sleep(2000); } return 0; }
11
44
55
66
77
11
22
33
44
55
66
77
...
看看,看看, 產生異常了吧, 主要原因是執行緒不同步問題, 在後續博文中, 我們將介紹執行緒安全的情況, 那時, 我們將考慮執行緒同步。