multi-thread win32 programming

This is a discussion on multi-thread win32 programming within the Windows Programming forums, part of the Platform Specific Boards category; hi, im trying to get a part of my program as a new thread. now, i know i need to ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    28

    multi-thread win32 programming

    hi,

    im trying to get a part of my program as a new thread. now, i know i need to define the thread as:
    Code:
     void ThreadName( void* pParams )
    and start it with
    Code:
     
        long handle,code;
        handle=_beginthread( Thread, 0, NULL );
    but since the thread is a special kind of function, i need to pass several vars to it except that i dont know how... can anyone please explain this? or point me to a tutorial about this?

    im programming in C

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,897
    It's easy, really.
    If you know what you're passing, you can easily cast it back:

    Code:
    void MyThread(void* pParam)
    {
    	int n = (int*)pParam;
    }
    
    int main()
    {
    	int n = 100;
    	_beginthread(&MyThread, 0, &n);
    	return 0;
    }
    If you need to pass more than argument, use a struct, then create the struct, fill it with information and pass it along.
    However, I must warn you that you should not be passing data to a thread that will be used by both the new thread and the main thread at the same time. Only one thread should use the data; otherwise you'll be walking into deeper waters.
    Last edited by Elysia; 01-12-2008 at 12:11 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    28
    i thank you very much!
    i`ll go try it out now

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    28
    well... it became too complex... ive noticed that some globar vars are being used throughout my program and so i`ll have to synchronise between too many sections... not worth it.
    but at least ive learned sometihng! thanks again

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,897
    Avoid global variables! They are not thread safe, sometimes they aren't optimized and they are hard to keep track of and easily creates bugs!
    Just an advice for you.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    28
    im talking bout my window handles.
    i wanted to use the new thread to resize my childwindows but im also using the child windows at other places of the main thread so all i get instead is a great big access violation....................
    i`ll abandon this course of action for now

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,897
    You can easily pass data to threads if they receive a local copy or make a local copy. As long as two threads aren't modifying the same data, all is fine.
    Code:
    void MyThread(void* pParam)
    {
    	HANDLE hMyHandle = (HANDLE)pParam;
    }
    
    int main()
    {
    	HANDLE hMyHandle; /* Assume it's a valid handle */
    	_beginthread(&MyThread, 0, (void*)hMyHandle);
    	return 0;
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    28
    ok then,
    how do i keep the program from running over the same thread? (since its being called from under WM_SIZE) i mean, if it gets back to WM_SIZE before the thread has ended, will it give me any trouble?

    and do i need to use the _endthread(); command?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,897
    Quote Originally Posted by robig2 View Post
    ok then,
    how do i keep the program from running over the same thread? (since its being called from under WM_SIZE) i mean, if it gets back to WM_SIZE before the thread has ended, will it give me any trouble?
    Not sure what you mean. Do you expect a thread to do some work inside your window proc and end before the window proc ends?

    and do i need to use the _endthread(); command?
    You can use it to explicitly kill a thread, but it's not recommended. It will cause the thread's stack to be leaked and you can find your program in a zombie state since the thread can't clean up (what will happen if you kill it in the middle of updating something?).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    28
    Not sure what you mean. Do you expect a thread to do some work inside your window proc and end before the window proc ends?
    well, yea. i want it to do all the MoveWindow calls that ive done under WM_SIZE so that it will happen faster and the screen wont blink when i resize the window.
    Code:
    		  case WM_SIZE:
    
    // on every WM_SIZE msg to parent window, we want the children windows to resize as well
    
    			  hdc = GetDC (hwnd) ;
    			  GetTextMetrics (hdc, &tm) ;
    			  ReleaseDC (hwnd, hdc) ;
    
    
    						//rect.right is how wide the list window is
    			   rect.right= 
    						// new width of parent window				   
    				   LOWORD(lParam) 
    						// - size mod tmAveCharWidth to avoid letter clipping
    				- (LOWORD(lParam)%tm.tmAveCharWidth) 
    						// + vScrollSize
    				+ vScrollSize 
    						// - area dedicated for IDD_LIST_BUTTONS dialog				
    				- tm.tmAveCharWidth*18;
    
    						//calculates the size of one colume
    			   rect.right=rect.right/4-rect.right%tm.tmAveCharWidth;
    
    			   
    						//rect.bottom is how high the list window is
    			   rect.bottom= 
    						// new height of parent window				   
    				   HIWORD(lParam) 
    						// - size mod tmHeight to avoid letter clipping
    				   - (HIWORD(lParam)%tm.tmHeight) 
    						// - Y position of list window to leave some empty space under it	
    				   - tm.tmHeight * 3;
    
    
    
    			   MoveWindow(hwndListFname,
    				   tm.tmAveCharWidth, // 1 char from the left side of main window
    				   tm.tmHeight * 3,   // 3 chars from the top side of main window
    				   rect.right,
    				   rect.bottom,
    				   FALSE);
    			   MoveWindow(hwndListLname,
    				   tm.tmAveCharWidth+rect.right, // starts where 1st one stops
    				   tm.tmHeight * 3,   // 3 chars from the top side of main window
    				   rect.right,
    				   rect.bottom,
    				   FALSE);
    			   MoveWindow(hwndListHphone,
    				   tm.tmAveCharWidth+2*rect.right, // starts where 2nd one stops
    				   tm.tmHeight * 3,   // 3 chars from the top side of main window
    				   rect.right,
    				   rect.bottom,
    				   FALSE);
    			   MoveWindow(hwndListCphone,
    				   tm.tmAveCharWidth+3*rect.right, // starts where 3rd one stops
    				   tm.tmHeight * 3,   // 3 chars from the top side of main window
    				   rect.right,
    				   rect.bottom,
    				   FALSE);
    			   MoveWindow(hwndListButtons,
    				   4*rect.right+tm.tmAveCharWidth,
    				   tm.tmHeight * 3,
    				   13*tm.tmAveCharWidth,
    				   8*tm.tmHeight, FALSE);
    	return 0 ;
    and ive changed it to this:

    Code:
    	case WM_SIZE:
    			 EnterCriticalSection( &cs );
    			 
    			  
    			   handle=_beginthread( MoveWindowThread, 0, NULL );
    			 LeaveCriticalSection( &cs );
    
    			   GetExitCodeThread(handle,&code);
    			   ExitThread(code);
    return 0 ;
    where MoveWindowThread does all of the above. should i post it as well? i didnt coz i dont want to fill up with too much code....

    oh, and ive just saw there is a CreateThread function for win32 progs... should i use that one instead?
    Last edited by robig2; 01-12-2008 at 01:54 PM.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,897
    It doesn't work that way. Threads are meant to be functions executing in parallel with other code and not to take up some insignificant task and finish it before the other thread finishes.
    In other words, threads are good for asynchronous work or parallel work, but not for synchronous work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    28
    so its supposed to be used on an entirly independed part of the program?

    in that case, i cant use it here....

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,897
    It's supposed to be used if you want code to execute in parallel (ie while you do task A, you also do task B and not first task A and then task B).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Jan 2008
    Posts
    28
    for example if i had 4 windows to move and i move each on at the same time on a diffrent thread. got it
    any chance and you can answer my 2nd question about the blinking windows? or is it too complex? coz i dont have to do it but i would like to.....

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,897
    Quote Originally Posted by robig2 View Post
    for example if i had 4 windows to move and i move each on at the same time on a diffrent thread. got it
    Well, in theory, yes, but you need to be careful here.
    On a single processor, threads are scheduled to it. So thread A executes for 10 ms, then thread B for 10 ms and so on. They don't actually execute in parallel. It's just an illusion that they do.
    On multi-core systems, they actually do execute in parallel, but the theory behind your idea is sound.

    any chance and you can answer my 2nd question about the blinking windows? or is it too complex? coz i dont have to do it but i would like to.....
    Well, Windows programming is difficult. It's hard to say right out of the bat what's wrong.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multithreading (flag stopping a thread, ring buffer) volatile
    By ShwangShwing in forum C Programming
    Replies: 3
    Last Post: 05-19-2009, 08:27 AM
  2. Replies: 2
    Last Post: 07-01-2007, 08:11 AM
  3. Thread Synchronization in Win32
    By passionate_guy in forum C Programming
    Replies: 0
    Last Post: 02-06-2006, 05:34 AM
  4. multithreading question
    By ichijoji in forum C++ Programming
    Replies: 7
    Last Post: 04-12-2005, 11:59 PM
  5. Replies: 12
    Last Post: 05-17-2003, 06:58 AM

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