Archived

This topic is now archived and is closed to further replies.

Threads question

This topic is 5648 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have two independent threads that call functions of a single class instance. The functions they call do not overlap, but the variables that are modified/read within those functions, do. I suspect that is the reason why my program crashes without an error message. The pseudocode would be something like this:
    
  
struct DataChunk
 {
 int Size;
 void* Data;
 int Flag;
 int Offset;
 };

DataChunk* Chunk = new DataChunk();
DataChunk* Buffer = new DataChunk();

Buffer->Flag = NEED_FILL;

///////////////

Thread 1:
 polls Func1();
///////////////


///////////////

Thread 2:
 polls Func2();
///////////////


void Func1()
{
  if(Buffer->Flag = NEED_FILL)
    {
    Read data into Buffer->Data
    Buffer->Size = buffer_size;// (buffer_size is preset)

    Buffer->Flag = FULL;
    Buffer->Offset = 0;
    }
}
 
void* Func2()
{
  if(Buffer->Flag = FULL)
    {
    Chunk->Data = &Buffer->Data[Buffer->Offset];
    Chunk->Size = chunk_size; 
    //(chunk_size is always a remainderless quotient of Buffer->Size,

    //or the remainder of (Buffer->Size -  Buffer->Offset) if 

    //there isn''t enough data to fill the entire buffer)

    Buffer->Offset += chunk_size;
    If((Buffer->Offset + chunk_size) > Buffer->Size)
      {
      Buffer->Flag = NEED_FILL;
      }
    }
}
  
I''m not sure, but I suspect that when Func1() assigns something to one the Buffer->variable''s, and Func2() does the very same thing at the same time, the program crashes. Can this be true? And if it is, what would be the best way around it? Furthermore, the real code is actually further optimised and I''m using two buffers instead of one. Thanks, Crispy

Share this post


Link to post
Share on other sites
Use syncronization functions from Win32: CreateMutex, WaitForSingleObject and ReleaseMutex, or Initialize/Enter/LeaveCriticalSection, for example. Basically, you first create a syncronization object. Before you attempt to read/write shared data, you want on that object. If no other threads are holding the lock on the object, then you get the lock and execution continues. Otherwise, you wait until another thread releases the lock. After you are done with the shared data, you release the synchronization object. [link]

Share this post


Link to post
Share on other sites
In a way, yes. What you achieve with __finally is usually achieved with C++ destructors. For example, you have a Lock class that you instantiate:

void foo() {
Lock lock;
int a = bar();
}

Regardless whether exception is thrown in bar or not, Lock's destructor will be called, releasing the lock. Lock's constructor may, for instance, call EnterCriticalSection and its destructor may call LeaveCriticalSection.

[edited by - IndirectX on June 26, 2002 5:59:06 PM]

Share this post


Link to post
Share on other sites
Those sort of classes are called 'guards' or sometimes 'scope guards' if you want to search for more info.

Specificly you want syncronization or mutex guards. (However in your case on Win32 you want to use their CriticalSection object, not the full-blown mutex).

[edited by - Magmai Kai Holmlor on June 26, 2002 12:01:45 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Magmai Kai Holmlor
However in your case on Win32 you want to use their CriticalSection object, not the full-blown mutex.



Brr - I already wrote a Mutex class... I''ll check out the overhead relative to nCriticalSection, though

Thanks, guys

Crispy

Share this post


Link to post
Share on other sites