I'm new to threading and wanted to make a program that was going to receive data from two separate pieces of hardware and populate a queu with their data. I then wanted to pull the data out of the queue and draw it to the screen graphically. So each piece of hardware will get its own thread while the graphics work inside the main thread. So my question is this: is it possible to read and write to the queue at the same time or do I have to lock the queue each time on of the threads accesses the queue.

Evan

Recommended Answers

All 8 Replies

You will have to lock the queue in order to synchronize the two threads. Disaster will strike your program dead if you allow multiple threads to access a single common object without first locking them. One way to do it is use mutex's. The main thread should create a named mutex so that other threads can attempt to access it. The os will only allow one thread at a time to access the mutex -- all other threads are blocked until the current thread releases the mutex.

Here is an article for MS-Windows mutex.

Ok thanks for the reply. That's what I was expecting to hear.

So i'm going to use Glib for threading since I am using gtkmm anyway. Each piece of hardware has its own object in the application that it will be communicating with and each object will contain a function that will be used in the worker threads. Should I pass the mutex by reference to each object if I declare the mutext object in the main object where both my datalogger objects are going to be declared in.

You don't pass it at all. Typically the thread would call CreateMutex() to get a handle to the mutex object, then WaitForSingleObject() to gain access

// This goes in each of the two threads
HANDLE hMutex = CreateMutex(0, FALSE, "MyMutexObject");
if( hMutex )
{
    // wait for access
    WaitForSingleObject(hMutex);
    // do whatever work needs to be done goes here
    //
    // release the mutex to other threads
    ReleaseMutex(hMutex);
}

This is kind of what I am talking about

class DataLogger_One {
  private:
	Glib::Mutex mutex
	
  public:
	DataLogger_One(Glib::Mutex &mutex) {
		this->mutex = mutex;
	}
	
	void workerFunc() {
		while(true) {
			{
				Glib::Mutex::Lock lock (mutex);
				// Do Something
			}
		}
	}
};

class DataLogger_Two {
  private:
	Glib::Mutex mutex
	
  public:
	DataLogger_Two(Glib::Mutex &mutex) {
		this->mutex = mutex;
	}
  
	void workerFunc() {
		while(true) {
			{
				Glib::Mutex::Lock lock (mutex);
				// Do Something
			}
		}
	}
};

class mainClass {
  private:
	Glib::Mutex mutex
	
  public:
	mainClass() {
		DataLogger_One Aobj(mutex);
		DataLogger_Two Bobj(mutex);
		
		Glib::Thread *Athread = Glib::Thread::create(sigc::mem_fun(Aobj, &DataLogger_One::workerFunc));
		Glib::Thread *Bthread = Glib::Thread::create(sigc::mem_fun(Bobj, &DataLogger_Two::workerFunc));
	}
};

I was wondering if anyone would be able to tell me if the code I wrote above is the right way to do multi threading with multiple objects.

I've never done it quite like that, so I don't know if it will work or not. But you never called CreateMutex() andwhere. I would put it in mainClass() before anything else.

i was looking at this example gtkmm provided in its glibmm documentation found here. to come up with the stuff that I wrote above

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.