943,846 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2502
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
May 31st, 2008
0

Semafore in Windows problem

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 15
Solved Threads: 3
Light Poster
Cybulski is offline Offline
47 posts
since Apr 2008
May 31st, 2008
0

Re: Semafore in Windows problem

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Jun 1st, 2008
0

Re: Semafore in Windows problem

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.
Reputation Points: 15
Solved Threads: 3
Light Poster
Cybulski is offline Offline
47 posts
since Apr 2008
Jun 1st, 2008
0

Re: Semafore in Windows problem

>>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.

Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Jun 5th, 2008
0

Re: Semafore in Windows problem

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.
Reputation Points: 15
Solved Threads: 3
Light Poster
Cybulski is offline Offline
47 posts
since Apr 2008
Jun 5th, 2008
0

Re: Semafore in Windows problem

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.
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Jun 5th, 2008
0

Re: Semafore in Windows problem

Yeah sorry I missed point. I'm still thinking about semaptores in POSIX manner - acquired means it's acquired regardless of process accessing it.
Reputation Points: 15
Solved Threads: 3
Light Poster
Cybulski is offline Offline
47 posts
since Apr 2008
Jun 12th, 2008
0

Re: Semafore in Windows problem

Ok, I got working threads, but still can't make proper synchronization. It's hard to find good example.

So, there is header file:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 15
Solved Threads: 3
Light Poster
Cybulski is offline Offline
47 posts
since Apr 2008
Jun 12th, 2008
0

Re: Semafore in Windows problem

hReaderMutex is not defined anywhere, hence the error. To get over this problem you need something like ...
C++ Syntax (Toggle Plain Text)
  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()).
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Jun 12th, 2008
0

Re: Semafore in Windows problem

Ok I figured out that static const must be initialize within class body, but still:
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 15
Solved Threads: 3
Light Poster
Cybulski is offline Offline
47 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: file writing speed (API)
Next Thread in C++ Forum Timeline: BDS problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC