| | |
Semafore in Windows problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
Initialize outside the class i.e.
C++ Syntax (Toggle Plain Text)
// outside your class ... const HANDLE MultiReader::hReaderMutex = CreateMutex( NULL, TRUE, "ReaderMutex");
I replaced that Mutex with Semaphore object. There should be no much difference, but anyway, this is pretty much my code now:
I can't figure out why threads aren't synchronized. I can comment out Wait function, Release function. I can give semaphore initial value of 0 and still get same results. Console output is random. This starts frustrating me.
C++ Syntax (Toggle Plain Text)
// MultiReader.h class MultiReader : public DirectoryReader { public: MultiReader(char* p_sConfigPath); ~MultiReader(void); static const HANDLE hReaderSemaphore; void CreateThreads(void); static DWORD WINAPI ReaderThread(PVOID p_pVoid); };
C++ Syntax (Toggle Plain Text)
// MultiReader.cpp const HANDLE MultiReader::hReaderSemaphore = CreateSemaphore( NULL, 1, 1, NULL); void MultiReader::CreateThreads() { unsigned int nThreadNo; for( nThreadNo = 0; nThreadNo < vDirectories.size(); nThreadNo++) { CreateThread( NULL, 0, MultiReader::ReaderThread, (void*)&vDirectories.at(nThreadNo), 0, NULL); } } //it works and these variables are defined in derived class WORD WINAPI MultiReader::ReaderThread(LPVOID p_pVoid) { WaitForSingleObject( hReaderSemaphore, 0); OPTIONS* ThreadData = (OPTIONS*) p_pVoid; cout << ThreadData->sDirectoryName << endl; cout << ThreadData->uOperationId << endl; DisplayVector(ThreadData->vExtensions); cout << endl; ReleaseSemaphore( hReaderSemaphore, 1, NULL ); return NULL; }
I can't figure out why threads aren't synchronized. I can comment out Wait function, Release function. I can give semaphore initial value of 0 and still get same results. Console output is random. This starts frustrating me.
Ok, it seems I must still learn a lot about windows threading.
I have these threads running, but I need them to use base class method and container. Base to MultiReader (DirectoryReader) have method MakeList(string) and vector<string>.
Each threadmust call MakeList and access vector in base class to insert data there. I have declared MakeList(string) as static (do i have to do this?), but now compiler says "left of .push_back must have class/struct/union" and points this at MakeList method.
So basically I'm not done yet with threading and still need help. Any ideas?
I have these threads running, but I need them to use base class method and container. Base to MultiReader (DirectoryReader) have method MakeList(string) and vector<string>.
Each threadmust call MakeList and access vector in base class to insert data there. I have declared MakeList(string) as static (do i have to do this?), but now compiler says "left of .push_back must have class/struct/union" and points this at MakeList method.
So basically I'm not done yet with threading and still need help. Any ideas?
static methods can only access static class objects. So if that vector was not declared static then the threads can't access it. This has nothing to do with threads and everything to do with c++ classes.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
When I make vector static, I get unresolved external error.
In meantime I found this:
http://homepages.tesco.net/J.deBoyne...functions.html
Do I have to rebuild my solution?
In meantime I found this:
http://homepages.tesco.net/J.deBoyne...functions.html
Do I have to rebuild my solution?
static data objects must also be declared something like global objects
Only static class methods can be used as thread functions and that link explains why. You already made the method static so don't worry about that part.
// *.h file
#include <vector>
#include <string>
class foo
{
public:
static std::vector<std::string> lst;
// other stuff not shown
};
// *.cpp file
#include "foo.h"
std::vector<std::string> foo::lst;
int main()
{
return 0;
}Only static class methods can be used as thread functions and that link explains why. You already made the method static so don't worry about that part.
Last edited by Ancient Dragon; Jun 14th, 2008 at 7:36 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Other Threads in the C++ Forum
- Previous Thread: no newline at end of file error
- Next Thread: BDS problem
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






