Threading problem

This is a discussion on Threading problem within the Windows Programming forums, part of the Platform Specific Boards category; Greetings! I learnt little bit about multithreading, and tried to create a program with 2 threads. But the program wonít ...

  1. #1
    Android
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    386

    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,265
    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,265
    Oh, and change void main to int main before someone else here yells at you

  4. #4
    Android
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    386
    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
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    386
    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
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    386
    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, 09:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM
  5. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21