| | |
Semafore in Windows problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
I'm trying to implement critical section using objects listed in this article: http://www.mycplus.com/tutorial.asp?TID=290
Somehow, it doesn't work like intended, or maybe I do something wrong. Basically my concept looks like this:
For some reason blocking doesn't work, for example if I call WaitForSingleObject(); right after cerating mutex (it should put semaphore down), I can still access it later in MakeList method. Do I need pass some options in creating mutex? I don't really know how there security attributes work.
Any ideas?
[edit] I want call MakeList() in multiple threads later, I expect problems too. I also don't want use MFC, since I already started project in WIN32, adding MFC class is impossible it seems.
Somehow, it doesn't work like intended, or maybe I do something wrong. Basically my concept looks like this:
C++ Syntax (Toggle Plain Text)
DirectoryReader::DirectoryReader(char* p_sConfigPath) { GetConfig(p_sConfigPath); if( hReaderMutex = CreateMutex( NULL, FALSE, "ReaderMutex") ) cout << "Mutex created!" << endl; } DirectoryReader::MakeList(string sPath) { While( reading filenames in specified directory ) { WaitForSingleObject( hReaderMutex, INFINITE ); m_vFilenames.push_back( file name ); ReleaseMutex( hReaderMutex ); } }
For some reason blocking doesn't work, for example if I call WaitForSingleObject(); right after cerating mutex (it should put semaphore down), I can still access it later in MakeList method. Do I need pass some options in creating mutex? I don't really know how there security attributes work.
Any ideas?
[edit] I want call MakeList() in multiple threads later, I expect problems too. I also don't want use MFC, since I already started project in WIN32, adding MFC class is impossible it seems.
Last edited by Cybulski; May 31st, 2008 at 8:56 am.
Mutexs work with multiple threads or multiple processes. What you describe is not a problem if there are no other threads or processes that want access to the mutex at the same time. Add a Sleep() before the WaitForSingleObject() to allow time for other threads to gain access to the mutex and see if the wait function blocks until the other thread(s) have released the mutex.
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.
I'm pretty sure semaphore is not working correctly anyway. There should be no difference between single thread and more threads acquiring semaphore. For example, if I call WaitForSingleObject() twice in row, thread should be locked, but it isn't. Semaphore represents resource.
I need to read more about parameters of these functions when I get back home, but MSDN is pretty hard lecture for me. I must say POSIX is far easier platform for multitasking programming.
I need to read more about parameters of these functions when I get back home, but MSDN is pretty hard lecture for me. I must say POSIX is far easier platform for multitasking programming.
>>There should be no difference between single thread and more threads acquiring semaphore
Maybe you don't think so, but there is a big difference.
Its working correctly -- Read the MSDN description for that function and you will see that it doesn't block after the first call when called from within the same thread.
Maybe you don't think so, but there is a big difference.
Its working correctly -- Read the MSDN description for that function and you will see that it doesn't block after the first call when called from within the same thread.
•
•
•
•
Originally Posted by MSDN
The thread that owns a mutex can specify the same mutex in repeated wait function calls without blocking its execution
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.
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
Most probably the problems are not about security attributes but about you not using threads. Ancient Dragon made a good point above.
Think what would happen if the wait call would block when you are doing it from the same thread that created the mutex in the first place (and you are not having any other threads operating on the mutex).
I think you can forget the security attributes for now and try grasping the basic understanding how the mutexes work. You can use the mutexes also with NULL security attributes, so you don't have to wrestle with those attributes.
Think what would happen if the wait call would block when you are doing it from the same thread that created the mutex in the first place (and you are not having any other threads operating on the mutex).
I think you can forget the security attributes for now and try grasping the basic understanding how the mutexes work. You can use the mutexes also with NULL security attributes, so you don't have to wrestle with those attributes.
Ok, I got working threads, but still can't make proper synchronization. It's hard to find good example.
So, there is header file:
And that's how I'm trying synchronize threads:
I get this:
It is after taking compiler advice to make
So, there is header file:
C++ Syntax (Toggle Plain Text)
class MultiReader : public DirectoryReader { public: MultiReader(char* p_sConfigPath); ~MultiReader(void); static const HANDLE hReaderMutex; void CreateThreads(void); static DWORD WINAPI ReaderThread(PVOID p_pVoid); };
And that's how I'm trying synchronize threads:
C++ Syntax (Toggle Plain Text)
MultiReader::MultiReader(char* p_sConfigPath):DirectoryReader(p_sConfigPath) { hReaderMutex = CreateMutex( NULL, TRUE, "ReaderMutex"); } MultiReader::~MultiReader(void) { } void MultiReader::CreateThreads() { unsigned int nThreadNo; for( nThreadNo = 0; nThreadNo < vDirectories.size(); nThreadNo++) { CreateThread( NULL, 0, MultiReader::ReaderThread, (void*)vDirectories.at(nThreadNo).sDirectoryName.c_str(), 0, NULL); } } DWORD WINAPI MultiReader::ReaderThread(LPVOID p_pVoid) { WaitForSingleObject( hReaderMutex, 0); cout << "Thread: " << (char*)p_pVoid << endl; ReleaseMutex( hReaderMutex ); return NULL; }
I get this:
C++ Syntax (Toggle Plain Text)
error LNK2001: unresolved external symbol "public: static void * MultiReader::hReaderMutex" (?hReaderMutex@MultiReader@@2PAXA) C:\Documents and Settings\hp\My Documents\Visual Studio 2008\Projects\FileFactory\Debug\FileFactory.exe : fatal error LNK1120: 1 unresolved externals
hReaderMutex handle static. I don't want it static too! Class will be part of singleton. Last edited by Cybulski; Jun 12th, 2008 at 10:20 am.
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
hReaderMutex is not defined anywhere, hence the error. To get over this problem you need something like ...
However, this is a bit bad construct, because when the CreateMutex() fails, you will not know the reason for the failure (GetLastError()).
C++ Syntax (Toggle Plain Text)
// in MultiReader.cpp ... const HANDLE MultiReader::hReaderMutex = CreateMutex( NULL, TRUE, "ReaderMutex");
Ok I figured out that static const must be initialize within class body, but still:
Gives me this:
error C2864: 'MultiReader::hReaderMutex' : only static const integral data members can be initialized within a class
C++ Syntax (Toggle Plain Text)
class MultiReader : public DirectoryReader { public: MultiReader(char* p_sConfigPath); ~MultiReader(void); static const HANDLE hReaderMutex = CreateMutex( NULL, TRUE, "ReaderMutex"); };
Gives me this:
error C2864: 'MultiReader::hReaderMutex' : only static const integral data members can be initialized within a class
![]() |
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 based beginner binary bitmap c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector visualstudio win32 windows winsock word wordfrequency wxwidgets






