Solved

Worker thread calling a global function?

Posted on 2004-04-07
3
396 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
if loop error 4 77
method notes when mouse over in eclipse 5 68
maven archtype selection in eclipse 1 51
Making an alias 7 54
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now