Thread Safety Question

This is a discussion on Thread Safety Question within the C++ Programming forums, part of the General Programming Boards category; I have a priority queue, as demonstrated below: Code: #include <iostream> #include <queue> #include <string> #include <windows.h> class Message { ...

  1. #1
    That Creepy Network Guy DeepBlackMagic's Avatar
    Join Date
    Feb 2003
    Posts
    265

    Thread Safety Question

    I have a priority queue, as demonstrated below:

    Code:
    #include <iostream>
    #include <queue>
    #include <string>
    #include <windows.h>
    
    class Message
    {
    	public:
    		std::string text;
    		int priority;
    		friend bool operator < (Message firstmsg, Message secondmsg);
    };
    
    bool operator < (Message firstmsg, Message secondmsg)
    {
    	if(firstmsg.priority<secondmsg.priority)
    	{ return true; }else{ return false; }
    }
    
    void Send(std::string);
    
    class Message_Queue
    {
    	public:
    		friend DWORD WINAPI Send_Priority_Queue(LPVOID param);
    		void Add_Queue(std::string text, int priority);
    	protected:
    		std::priority_queue<Message> MessageQueue;
    };
    
    void Message_Queue::Add_Queue(std::string text, int priority)
    {
    	Message message;
    	message.text=text;
    	message.priority=priority;
    	MessageQueue.push(message);
    	std::cout << "Queue Added:  " << text << std::endl;
    }
    
    
    void Send(std::string text)
    {
    	std::cout << "Queue Dumped: " << text << std::endl;
    }
    
    //Yes Global variables are naughty however im in a hurry
    Message_Queue messages;
    
    int main()
    {
    	std::cout << "Use CTRL+C To Break." << std::endl;
    	int tempint = 0;
    	std::string tempstr;
    	DWORD ThreadID=0;
    	CreateThread(NULL, 0, Send_Priority_Queue, NULL, 0, &ThreadID);
    	while (1)
    	{
    		std::cin >> tempstr;
    //		std::cin >> tempint;
    		messages.Add_Queue(tempstr,tempint);
    	}
    
    	return 1;
    }
    
    
    
    
    DWORD WINAPI Send_Priority_Queue(LPVOID user)
    {
    	while(1)
    	{
    		if (!messages.MessageQueue.empty())
    		{
    			Send(messages.MessageQueue.top().text);
    			messages.MessageQueue.pop();
    			Sleep(5000);
    		}
    	}
    	return 0;
    }
    What i want to know is if i have 3 threads running that are all using Add_Queue(std::string text, int priority); to add messages to the queue, is it thread safe? Will i need to use a mutex or something to prevent crashes? Id like to hear any ideas you guys might have, and possible quick solutions to it.

    Thanks for your time.

    .

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,659
    The code you have isn't thread safe, and adding 2 more threads won't help.
    You should provide synchronized access to all std:: containers.

    The easiest solution is to provide singleton access to the container using:
    InitializeCriticalSection()
    EnterCriticalSection()
    LeaveCriticalSection()
    DeleteCriticalSection()

    gg

  3. #3
    That Creepy Network Guy DeepBlackMagic's Avatar
    Join Date
    Feb 2003
    Posts
    265
    Thanks for your quick reply but that raises another question or 2. First and most important is, I have a working thread wrapper i use for cross-platform compatibility, so im wondering if the critical-section stuff is a windows only thing, or if there are POSIX equivilants. The second question is, (im reading the MSDN page right now, and its confusing as ever) where do each of those functions go? do i initialize and delete in the constructor and destructors, and then the threads that want to call the function call the EnterCriticalSection and LeaveCriticalSection() functions? am i on the right path here? Thanks.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,659
    This thread demonstrates how to use a critical section to protect multiple threads from accessing printf() at the same time.

    In Pthreads, you would use a mutex or condition variable to achieve something similiar.

    gg

  5. #5
    That Creepy Network Guy DeepBlackMagic's Avatar
    Join Date
    Feb 2003
    Posts
    265
    I got it working, works great, ill port it over to my pthreads wrapper sometime later when i need it there.


    THANK YOU THANK YOU THANK YOU!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-26-2009, 10:48 PM
  2. Question on thread
    By flexo87 in forum C Programming
    Replies: 10
    Last Post: 01-28-2009, 09:52 AM
  3. [code] Win32 Thread Object
    By Codeplug in forum Windows Programming
    Replies: 0
    Last Post: 06-03-2005, 03:55 PM
  4. STL thread safety
    By Hunter2 in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2004, 08:09 PM
  5. How to make a thread sleep or std::recv timeout?
    By BrianK in forum Linux Programming
    Replies: 3
    Last Post: 02-26-2003, 09:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21