1

I’m trying to simulate a simultaneous read and write. For some reason the threads won’t do this at the same time. I create 2 threads, these threads contain a Sleep(0) so other threads can do there processing. In the part where I create a new object I even put a sleep:

delete this->item; this->item = 0; Sleep(0); this->item = new hallo();

I ran this test for around 2 hours in release mode. There was no exception or crash. What do I do wrong?

This is how I try to do this:

#include <Windows.h>
#include <stdio.h>

#include "Source.h"

DWORD WINAPI readVar(LPVOID);
DWORD WINAPI writeVar(LPVOID);

testclass * testobject;

int main()
{
    testobject = new testclass();

    CreateThread(NULL,NULL,readVar,NULL,NULL,NULL);
    CreateThread(NULL,NULL,writeVar,NULL,NULL,NULL);

    while(true)
        Sleep(10000);
}

DWORD WINAPI readVar(LPVOID)
{
    while(true)
    {
        hallo * testObjectTest = testobject->getValue();
        if(!testObjectTest->outpoutSomeData())
            printf("ERROR\n\n");

        Sleep(0);
    }

    return NULL;
}

DWORD WINAPI writeVar(LPVOID)
{
    while(true)
    {
        testobject->resetValue();
        Sleep(0);
    }

    return NULL;
}

These are the classes:

class hallo
{
public:
    hallo() {printf("Object created\n");}
    ~hallo() {printf("Object removed\n");}
    bool outpoutSomeData() { printf("Call\n"); return true; }
};

class testclass
{
private:
    hallo * item;
public:
    testclass() {this->item = new hallo(); }
    hallo * getValue() { return item; }
    void resetValue() { delete this->item; this->item = 0; Sleep(0); this->item = new hallo(); }
};

FYI:

  • This is just a simple test. I didn’t pay much attention to “good coding”;
  • I have 4 CPU cores, so it need to be possible.
  • I had this problem multiple times in other programs, i fixed that with locking. Now I want to create a test where this will happen. But it won't work.
Laurence
  • 1,815
  • 4
  • 22
  • 35
  • Have you tried removing the Sleep(0) from readVar() and writeVar()? – neutrino Mar 06 '13 at 14:06
  • Yes I did, but I will get the same result. – Laurence Mar 06 '13 at 14:07
  • And what about creating more "writer" threads? – neutrino Mar 06 '13 at 14:16
  • Also, try replacing the Sleep(0) on resetValue() with a long loop making some "useful" processing, something like: `int j = 0; for(int i = 0; i < 10000; i++) j += i;` – neutrino Mar 06 '13 at 14:17
  • 1
    Accessing a null pointer results in undefined behavior, and one valid form of undefined behavior is "everything seems to run normally." In particular, since `hallo::outpoutSomeData` does not access any instance data, calling it with a null pointer will probably work just fine without crashing. [More details here](http://stackoverflow.com/questions/669742/accessing-class-members-on-a-null-pointer). – Raymond Chen Mar 06 '13 at 15:24
  • @Raymond Chen thanks. That is indeed what happens here. I tested this with my debugger. Thanks for the link, that was very helpful ;). – Laurence Mar 06 '13 at 15:27

0 Answers0