943,884 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2502
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Jun 12th, 2008
1

Re: Semafore in Windows problem

Initialize outside the class i.e.
C++ Syntax (Toggle Plain Text)
  1. // outside your class ...
  2. const HANDLE MultiReader::hReaderMutex = CreateMutex( NULL, TRUE, "ReaderMutex");
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Jun 13th, 2008
0

Re: Semafore in Windows problem

I replaced that Mutex with Semaphore object. There should be no much difference, but anyway, this is pretty much my code now:
C++ Syntax (Toggle Plain Text)
  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. };

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

Re: Semafore in Windows problem

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

Re: Semafore in Windows problem

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

Re: Semafore in Windows problem

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

Re: Semafore in Windows problem

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

Re: Semafore in Windows problem

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

Re: Semafore in Windows problem

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;
}

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

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