Semafore in Windows problem

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 47
Reputation: Cybulski is an unknown quantity at this point 
Solved Threads: 3
Cybulski's Avatar
Cybulski Cybulski is offline Offline
C++ wannabe

Semafore in Windows problem

 
0
  #1
May 31st, 2008
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:

  1. DirectoryReader::DirectoryReader(char* p_sConfigPath)
  2. {
  3. GetConfig(p_sConfigPath);
  4. if( hReaderMutex = CreateMutex( NULL, FALSE, "ReaderMutex") )
  5. cout << "Mutex created!" << endl;
  6. }
  7.  
  8. DirectoryReader::MakeList(string sPath)
  9. {
  10. While( reading filenames in specified directory )
  11. {
  12. WaitForSingleObject( hReaderMutex, INFINITE );
  13. m_vFilenames.push_back( file name );
  14. ReleaseMutex( hReaderMutex );
  15. }
  16. }

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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,339
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Semafore in Windows problem

 
0
  #2
May 31st, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 47
Reputation: Cybulski is an unknown quantity at this point 
Solved Threads: 3
Cybulski's Avatar
Cybulski Cybulski is offline Offline
C++ wannabe

Re: Semafore in Windows problem

 
0
  #3
Jun 1st, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,339
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1460
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Semafore in Windows problem

 
0
  #4
Jun 1st, 2008
>>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.

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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 47
Reputation: Cybulski is an unknown quantity at this point 
Solved Threads: 3
Cybulski's Avatar
Cybulski Cybulski is offline Offline
C++ wannabe

Re: Semafore in Windows problem

 
0
  #5
Jun 5th, 2008
I am still stuck with this... It seems bit complicated to fill security attributes and security descriptor structures. Anyone have experience with this? Option is to use MFC, but I'm not familiar with GUI controls. Anyway, is it possible to add MFC class to WIN32 project? Seems not.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Semafore in Windows problem

 
0
  #6
Jun 5th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 47
Reputation: Cybulski is an unknown quantity at this point 
Solved Threads: 3
Cybulski's Avatar
Cybulski Cybulski is offline Offline
C++ wannabe

Re: Semafore in Windows problem

 
0
  #7
Jun 5th, 2008
Yeah sorry I missed point. I'm still thinking about semaptores in POSIX manner - acquired means it's acquired regardless of process accessing it.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 47
Reputation: Cybulski is an unknown quantity at this point 
Solved Threads: 3
Cybulski's Avatar
Cybulski Cybulski is offline Offline
C++ wannabe

Re: Semafore in Windows problem

 
0
  #8
Jun 12th, 2008
Ok, I got working threads, but still can't make proper synchronization. It's hard to find good example.

So, there is header file:
  1. class MultiReader :
  2. public DirectoryReader
  3. {
  4. public:
  5. MultiReader(char* p_sConfigPath);
  6. ~MultiReader(void);
  7.  
  8. static const HANDLE hReaderMutex;
  9. void CreateThreads(void);
  10. static DWORD WINAPI ReaderThread(PVOID p_pVoid);
  11. };

And that's how I'm trying synchronize threads:
  1. MultiReader::MultiReader(char* p_sConfigPath):DirectoryReader(p_sConfigPath)
  2. {
  3. hReaderMutex = CreateMutex( NULL, TRUE, "ReaderMutex");
  4. }
  5.  
  6. MultiReader::~MultiReader(void)
  7. {
  8. }
  9.  
  10. void MultiReader::CreateThreads()
  11. {
  12. unsigned int nThreadNo;
  13.  
  14. for( nThreadNo = 0; nThreadNo < vDirectories.size(); nThreadNo++)
  15. {
  16. CreateThread( NULL, 0, MultiReader::ReaderThread, (void*)vDirectories.at(nThreadNo).sDirectoryName.c_str(), 0, NULL);
  17. }
  18. }
  19.  
  20. DWORD WINAPI MultiReader::ReaderThread(LPVOID p_pVoid)
  21. {
  22. WaitForSingleObject( hReaderMutex, 0);
  23. cout << "Thread: " << (char*)p_pVoid << endl;
  24. ReleaseMutex( hReaderMutex );
  25. return NULL;
  26. }

I get this:
  1. error LNK2001: unresolved external symbol "public: static void * MultiReader::hReaderMutex" (?hReaderMutex@MultiReader@@2PAXA)
  2. C:\Documents and Settings\hp\My Documents\Visual Studio 2008\Projects\FileFactory\Debug\FileFactory.exe : fatal error LNK1120: 1 unresolved externals
It is after taking compiler advice to make 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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Semafore in Windows problem

 
0
  #9
Jun 12th, 2008
hReaderMutex is not defined anywhere, hence the error. To get over this problem you need something like ...
  1. // in MultiReader.cpp ...
  2. const HANDLE MultiReader::hReaderMutex = CreateMutex( NULL, TRUE, "ReaderMutex");
However, this is a bit bad construct, because when the CreateMutex() fails, you will not know the reason for the failure (GetLastError()).
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 47
Reputation: Cybulski is an unknown quantity at this point 
Solved Threads: 3
Cybulski's Avatar
Cybulski Cybulski is offline Offline
C++ wannabe

Re: Semafore in Windows problem

 
0
  #10
Jun 12th, 2008
Ok I figured out that static const must be initialize within class body, but still:
  1. class MultiReader :
  2. public DirectoryReader
  3. {
  4. public:
  5. MultiReader(char* p_sConfigPath);
  6. ~MultiReader(void);
  7.  
  8. static const HANDLE hReaderMutex = CreateMutex( NULL, TRUE, "ReaderMutex");
  9. };

Gives me this:
error C2864: 'MultiReader::hReaderMutex' : only static const integral data members can be initialized within a class
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC