Thread: Threading problem

  1. #1
    Android geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470

    Threading problem

    Greetings!

    I learnt little bit about multithreading, and tried to create a program with 2 threads. But the program won’t work. It seems like the thread function won’t execute. When I tried to debug into dirWatch(), the execution pointer doesn’t seems to enter the function. Though CreateThread() doesn’t return NULL and I don’t get any error messages. (I'm running this on WinXP)
    Here what it looks like so far:

    Code:
    ......
    ......
    
    // Thread function
    
    void WINAPI dirWatch(char dir[256]){
    
    .......
    .......
    
    }
    
    void main(){
    
    ........
    ........
    ........
    
    lstrcpy(dir,temp);
    // Create 1st thread
    if(CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)dirWatch,dir,0,NULL)==NULL)
    	MessageBox(NULL,"CreateThread() failed.", "Message",0);
    
    .........
    .........
    .........
    
    lstrcpy(dir,temp);
    // Create 2nd thread
    if(CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)dirWatch,dir,0,NULL)==NULL)
    	MessageBox(NULL,"CreateThread() failed.", "Message",0);
    .......
    .......
    
    }
    What I’m doing wrong here please?

    Thanks for reading.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Your thread procedure is prototyped wrong, it should be:

    Code:
    DWORD WINAPI dirWatch(LPVOID lParam) {
       char *dir = (char*)lParam;
       ...
    Make sure that the string you are passing (dir in your case) is allocated with malloc if you are using C or new if you are using C++, and don't forget to free that memory in your thread procedure. The reason you do this is so that multiple threads are not using the same memory at the same time.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Oh, and change void main to int main before someone else here yells at you

  4. #4
    Android geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470
    I changed the code the way you pointed out. But I don’t know why, it still doesn’t work.
    new code:
    Code:
    // Thread function
    	......
    DWORD WINAPI dirWatch(LPVOID lParam){
    	char *dir = (char*)lParam;
    	......
    	delete[] dir;
    }
    
    void main(){
    	......
    	char *dir = new char[256];
    	......
    	lstrcpy(dir,temp);
    	// Create 1st thread
    	if(CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)dirWatch,dir,0,NULL)==NULL)
    		MessageBox(NULL,"CreateThread() failed.", "Message",0);
    	.........
    		
    	dir = new char[256];
    	.........
    	lstrcpy(dir,temp);
    	// Create 2nd thread
    	if(CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)dirWatch,dir,0,NULL)==NULL)
    		MessageBox(NULL,"CreateThread() failed.", "Message",0);
    	.......
    }

  5. #5
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    I assume that in the real code dir Watch is actually dirWatch (or it wouldn't compile)?

    Apart from that the code looks ok..you've put a breakpoint in dirWatch and it never gets hit?

  6. #6
    Android geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470
    Yes, it's dirWatch.
    And yes, the breakpoint in dirWatch never get hit. The program just ends without executing dirWatch. Why is that?

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    95
    Does your main function end after you create your threads or do you wait?
    (It should wait).

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Your program will exit when main returns, even if other threads are still executing or just starting to execute. The easiest way to prevent this is to store the thread handles returned by CreateThread and wait on them before returning from main. A thread handle is set to the signaled state when the thread exits. The WaitForSingleObject function will return when the object passed to it is in the signaled state or the timeout expires.
    Code:
       hThread1 = CreateThread(...);
    
       ...
    
       WaitForSingleObject(hThread1, INFINITE);
       WaitForSingleObject(hThread2, INFINITE);
       return 0;

  9. #9
    Android geek@02's Avatar
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    470
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM