Solved

Worker thread calling a global function?

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
repeateFront java challenge 31 94
Macro to save highlighted rows as a separate file 5 45
Change owner from userA to server Local Administrators 7 66
wordmultiple challenge 12 141
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…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

829 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