Home Help Center

Sample Application run time error

this is a sample application in ice development guide 19.10.
when i build project(debug),link is ok, but have 2 warning:

:\program files\microsoft visual studio\vc98\include\stlport\stl\debug\_debug.h(166) : warning C4005: '_STLP_DEBUG_CHECK' : macro redefinition
d:\program files\microsoft visual studio\vc98\include\stlport\stl\_config.h(375) : see previous definition of '_STLP_DEBUG_CHECK'
d:\program files\microsoft visual studio\vc98\include\stlport\stl\debug\_debug.h(167) : warning C4005: '_STLP_DEBUG_DO' : macro redefinition
d:\program files\microsoft visual studio\vc98\include\stlport\stl\_config.h(376) : see previous definition of '_STLP_DEBUG_DO'

i skip this,and run it. a error message show(see attachment): invaild memory address access.

who can tell me what is the problem?



// test1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <vector>
#include <list>
#include <IceUtil/Thread.h>
using namespace std;

template <class T> class Queue {
public:
void put(const T & item) {
_q.push_back(item);
}
T get() {
T item = _q.front();
_q.pop_front();
return item;
}
private:
list<T> _q;
};

Queue<int> q;

class ReaderThread : public IceUtil::Thread {
virtual void run() {
for (int i = 0; i < 100; ++i)
cout << q.get() << endl;
}
};

class WriterThread : public IceUtil::Thread {
virtual void run() {
for (int i = 0; i < 100; ++i)
q.put(i);
}
};

int main() {
vector<IceUtil::ThreadControl> threads;
int i;
// Create five reader threads and start them
//
for (i = 0; i < 5; ++i) {
IceUtil::ThreadPtr t = new ReaderThread;
threads.push_back(t->start());
}
// Create five writer threads and start them
//
for (i = 0; i < 5; ++i) {
IceUtil::ThreadPtr t = new WriterThread;
threads.push_back(t->start());
}
// Wait for all threads to finish
//
for (vector<IceUtil::ThreadControl>::iterator ii
= threads.begin(); ii != threads.end(); ++ii) {
ii->join();
}
}

Comments

  • marcmarc FloridaAdministrators, ZeroC Staff Marc LaukienOrganization: ZeroC, Inc.Project: The Internet Communications Engine ZeroC Staff
    The code is broken, because the get/put methods don't use any mutex protection. It would have to look like this:

    template <class T> class Queue : public IceUtil::Monitor<IceUtil::Mutex> {
    public:
    void put(const T & item) {
    IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this);
    _q.push_back(item);
    notify();
    }
    T get() {
    IceUtil::Monitor<IceUtil::Mutex>::Lock sync(*this);
    while(_q.empty())
    {
    wait();
    }
    T item = _q.front();
    _q.pop_front();
    return item;
    }
    private:
    list<T> _q;
    };

    Please read the information about IceUtil::Monitor in the Ice manual for more information.

    Regarding the warning, I believe that you are mixing the debug and non-debug version of STLport, i.e., some of your files are compiled with debug while others are compiled in release mode.
  • chenhong_szchenhong_sz Member ✭✭
    thank you!
    I am so hurry want to run this sample that lost to check the code ;(.
    about the warning,you said i mixing the debug and non-debug version of STLport,i build debug version only,if i want to remove the warning,how can i do?
  • marcmarc FloridaAdministrators, ZeroC Staff Marc LaukienOrganization: ZeroC, Inc.Project: The Internet Communications Engine ZeroC Staff
    As for the warning, I don't really know what the problem is, other than that I believe that it is due to mixing debug and release libraries. It is also not really an Ice issue, so I suggest that you post your question on the STLport mailing list.

    You can also use one of the demos in the Ice source distribution as an example for how to set up a Visual C++ 6.0 project. There are no warnings there.
Sign In or Register to comment.