Solved

Worker thread calling a global function?

Posted on 2004-04-07
3
398 Views
Last Modified: 2013-11-20
MFC / VC60 / Win9x,Win2k, WinXP

I use AfxBeginThread ( (AFX_THREADPROC) ::MyWorkerThread, this ); to call a worker thread global function UINT MyWorkerThread(LPVOID pParam).  Inside my global function MyWorkerThread() if I call other global functions, say MyFunctionCalledFromInsideMyThread(), do I have to worry about another thread calling this function at the sametime?  Or is the fuction data dublicated for each time MyFunctionCalledFromInsideMyThread() is called inside a different MyTWorkerThread()?
0
Comment
Question by:Surfer
3 Comments
 
LVL 23

Assisted Solution

by:chensu
chensu earned 50 total points
ID: 10778218
You need synchronization for global and static data but not data on the stack.
0
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 10778232
>>if I call other global functions, say MyFunctionCalledFromInsideMyThread(), do I have to worry about another
>>thread calling this function at the sametime?

Only if any function you call from within the thread uses global variables. If not, there is nothing to worry about, since each thread has its own stack to store non-global data.

However, if you *have* global variables, it'd be best to not access them directly, but via accessor/manipulator functions that can ensure synchronization, e.g.

int GetGlobalInt () {

    EnterCriticalSection ( &g_csLock);

    int n = g_nGlobalInt;

    LeaveCriticalSection ( &g_csLock);

    return n;
}

void SetGlobalInt ( int n) {

    EnterCriticalSection ( &g_csLock);

    g_nGlobalInt = n;

    LeaveCriticalSection ( &g_csLock);
}
0
 

Author Comment

by:Surfer
ID: 10778427
chensu good job on the correct answer but jkr answer really made me understand the concept.  Thanks guys!!



0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question