Sharing memory Thread<->Process

This is a discussion on Sharing memory Thread<->Process within the Windows Programming forums, part of the Platform Specific Boards category; Hello Coders, i don't have any expirence in win32 threads, but some background in programmig My question is how do ...

  1. #1
    marsface
    Guest

    Sharing memory Thread<->Process

    Hello Coders,

    i don't have any expirence in win32 threads, but some
    background in programmig

    My question is how do i share memory (varaibles) between
    the creator of the thread and the thread itself.

    Here a pseudo code:

    unsigned long bytes;

    thread = CreateThread(..,..,..,)

    while (Bytes < 1024)
    printf("Bytes: %ld\n"),

    ..
    .

    Thread Code:
    {
    bytes++;
    }


    I hope the principle is understandable ?? :-)

    Best Regards,
    Robert

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    The easiest, least sloppy way is to use the lpParameter on CreateThread()...

    Cast the data you want to send to LVOID (you can send a pointer to whatever), and recast it to what you want in the tread proc

    Code:
    #include <windows.h>
    
    DWORD WINAPI ThreadFunc(LPVOID lParam){
    	MessageBox(HWND_DESKTOP,(LPSTR)lParam,NULL,MB_OK);
    	return 0;
    }
    
    
    int main(void){
    
    	DWORD dwTID;
    	HANDLE hThread;
    	char buff[] = "Hello from thread!!!!";
    
    	hThread = CreateThread(NULL,NULL,ThreadFunc,(LPVOID)buff,NULL,&dwTID);
    	WaitForSingleObject(hThread,INFINITE);//let thread end
    	return 0;
    }
    As a word of caution, beware CreateThread when using std functions.......Your compiler will most likely have a better safer version of the func (__beginthreadex() in VC++)

  3. #3
    marsface
    Guest

    Smile

    Thanks, that help,

    but what is the difference in detail when i use
    the (__beginthreadex()) function instead of the
    API call CreateThread ???


    Regards,
    Robert

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Some functions from the std libs arent thread safe as they do stuff like use global buffers...etc.....

    So when your main thread and the created thread both call certain std funcs there is a small risk of corruption, leaks or exceptions from dereferncing NULL pointers etc........

    With __beginthreadex(), the multithreaded versions of the std libs are linked instead of the standard versions......then it calls CreateThread() to create the thread itself.......

    It's often not a problem....but if you get into the habit of using __beginthreadex() your life will be easier...



    Code:
    #include <windows.h>
    #include <process.h>//Need this
    
    
    UINT __stdcall ThreadFunc(LPVOID lParam){
    	MessageBox(HWND_DESKTOP,(LPSTR)lParam,NULL,MB_OK);
    	return 0;
    }
    
    
    int main(void){
    
    	UINT uiTID;
    	HANDLE hThread;
    	char buff[] = "Hello from thread!!!!";
    
    	hThread = (HANDLE)_beginthreadex(NULL,NULL,ThreadFunc,(LPVOID)buff,NULL,&uiTID);
    	WaitForSingleObject(hThread,INFINITE);
    	return 0;
    }
    To use this code example in VC++, go to project->settings->C/C++->Category->Code Generation->Use Runtime Library->MultiThreaded

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    I would also advocate the beginthread() or beginthreadex() functions, (Note, beginthreadex() is called beginthreadNT() on a Borland compiler).

    However, there is a difference between beginthread() and beginthreadex() in that beginthread() closes the thread handle for you when the thread exits, beginthreadex() does not. If you are running an app which is spinning lots of threads over time, use CloseHandle() to close the handle when the thread exits or you will leak handles.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by adrianxw
    I would also advocate the beginthread() or beginthreadex() functions, (Note, beginthreadex() is called beginthreadNT() on a Borland compiler).

    However, there is a difference between beginthread() and beginthreadex() in that beginthread() closes the thread handle for you when the thread exits, beginthreadex() does not. If you are running an app which is spinning lots of threads over time, use CloseHandle() to close the handle when the thread exits or you will leak handles.
    He he...yeah I heard that, but from a different viewpoint....

    In Jeff Richter's book Advanced Windows he states the _beingthreadex & _endthreadex superceded _beginthread & _endthread.....

    The reason M$ brought them out was that people still cared about the thread's return value, and a call to GetExitCodeThread() after the thread died would fail as _endthread had called CloseHandle() as you said......

    But I agree with your point that an app with multiple threads should ensure that housekeeping is done by periodically releasing these handles.......

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    47

    Thumbs up

    Thanks a lot guys!!

    Regards,
    Robert

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Relate memory allocation in struct->variable
    By Niara in forum C Programming
    Replies: 4
    Last Post: 03-23-2007, 04:06 PM
  2. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM
  3. Sharing Memory among Processes
    By doom in forum C# Programming
    Replies: 4
    Last Post: 10-04-2005, 07:51 PM
  4. Memory allocation and deallocation
    By Micko in forum C++ Programming
    Replies: 3
    Last Post: 08-19-2005, 07:45 PM
  5. Sharing memory
    By YALINI in forum C Programming
    Replies: 1
    Last Post: 08-28-2001, 01:47 PM

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