| | |
Semafore in Windows problem
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2007
Posts: 981
Reputation:
Solved Threads: 210
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
Views: 1681 | Replies: 17
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






