Semafore in Windows problem

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

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

 
1
  #11
Jun 12th, 2008
Initialize outside the class i.e.
  1. // outside your class ...
  2. const HANDLE MultiReader::hReaderMutex = CreateMutex( NULL, TRUE, "ReaderMutex");
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
  #12
Jun 13th, 2008
I replaced that Mutex with Semaphore object. There should be no much difference, but anyway, this is pretty much my code now:
  1. // MultiReader.h
  2. class MultiReader :
  3. public DirectoryReader
  4. {
  5. public:
  6. MultiReader(char* p_sConfigPath);
  7. ~MultiReader(void);
  8.  
  9. static const HANDLE hReaderSemaphore;
  10. void CreateThreads(void);
  11. static DWORD WINAPI ReaderThread(PVOID p_pVoid);
  12. };

  1. // MultiReader.cpp
  2.  
  3. const HANDLE MultiReader::hReaderSemaphore = CreateSemaphore( NULL, 1, 1, NULL);
  4.  
  5. void MultiReader::CreateThreads()
  6. {
  7. unsigned int nThreadNo;
  8. for( nThreadNo = 0; nThreadNo < vDirectories.size(); nThreadNo++)
  9. {
  10. CreateThread( NULL, 0, MultiReader::ReaderThread, (void*)&vDirectories.at(nThreadNo), 0, NULL);
  11. }
  12. } //it works and these variables are defined in derived class
  13.  
  14. WORD WINAPI MultiReader::ReaderThread(LPVOID p_pVoid)
  15. {
  16.  
  17. WaitForSingleObject( hReaderSemaphore, 0);
  18.  
  19. OPTIONS* ThreadData = (OPTIONS*) p_pVoid;
  20. cout << ThreadData->sDirectoryName << endl;
  21. cout << ThreadData->uOperationId << endl;
  22. DisplayVector(ThreadData->vExtensions);
  23. cout << endl;
  24.  
  25. ReleaseSemaphore( hReaderSemaphore, 1, NULL );
  26. return NULL;
  27. }

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.
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
  #13
Jun 13th, 2008
You should (really) check the return value of WaitForSingleObject() and react accordingly. And also understand what it means to use zero as the time-out interval with WaitForSingleObject().
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
  #14
Jun 13th, 2008
Wow... Thanks for this tip.

I still think of these object in POSIX manner, where (as far as I remember) wait functions do not return untill they acquire semaphore, if timeout was set to infinite. These must be checked in loop... It is pretty much working now.
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
  #15
Jun 13th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,413
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: 1470
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Semafore in Windows problem

 
0
  #16
Jun 13th, 2008
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.
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
  #17
Jun 14th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,413
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: 1470
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Semafore in Windows problem

 
0
  #18
Jun 14th, 2008
static data objects must also be declared something like global objects
// *.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;
}

In meantime I found this:
http://homepages.tesco.net/J.deBoyne...functions.html
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.
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