Solved

Worker thread calling a global function?

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Separate files macro - Errors on larger data set 18 73
has77  challenge 9 97
pre4 challenge 19 110
child constructor and parent constructor, overriding and overloading 6 93
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

740 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