I am trying to write a program using threads. This is the first time I am using threads, and there are a few thinks I don't know.
When two threads are editing the same memory, they should first ask for a lock on the memory before editing it. I understand this, but what do I have to do if one thread is editing the memory while the other thread is reading it? Is this a problem?
In my program, one thread is adding data to a buffer and the other thread is reading it. The system works like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// variables:
char* buffer;
unsignedlong pos,len;
// writing N bytes:
buffer = realloc(buffer,len+N);
memcpy(buffer+len,INPUT,N);
len += N; // edit length AFTER writing
// reading N bytes:
if(pos+N<=len) {
memcpy(OUTPUT,buffer+pos,N);
pos += N;
}
This way, reading only changes the 'pos' variable and writing only changes the 'len' variable. Is this correct? Or will this code cause errors if one thread is changing len while the other is reading it? Is there any way to check how many lines reading an unsigned long uses in assemby?
After some time the buffer is getting too big and I have to delete the part that is not used anymore. But how can I stop the other thread from reading while I am deleting the buffer?
The windows website says I have to use a 'mutex' object for all memory that is used by multiple threads. How do these objects work?
Your above code has synchronization problems. You are correct in that reading only changes pos, but writing changes not only len, but buffer itself, and reading accesses buffer.
Unsigned long is not guaranteed to be read or written in a single assembly instruction even though it might be. You need a lock (mutex).
A mutex is essentially a boolean variable that can be read and/or written in a single instruction. Mutexes have two operations--Lock and Unlock. Calling Lock() on the mutex while it is already locked causes the caller to block until the lock can be acquired.
If you use the same mutex around both your reader and writer code then you will not have any synchronization issues.
Threading isn't built into C++. Threading support is provided thru 3rd party libraries or calling the Operating System directly.
In answer to your questiom, yes, you need to synchronise access to shared memory.
In Windows, there are a number of things you can use to achieve this, they're generally called Synchronisation Objects.
In Unix, it's typically done using a library called the PThreads library. This provides functions for creating and managing threads as well as ways to synchronise access to resourceses.
Back to your problem, what operating system are you using?
I am trying to use the Mutex class, but this doesn't seem to work. What header is required to use it?
I tried making my own Mutex class by using CreateMutex, WaitForSingleObject (for Lock function) and ReleaseMutex (for Unlock function). It seems to work, I tried locking a mutex in one thread for 10 seconds and the other thread was paused for 10 seconds while waiting for the mutex.