Solved

Worker thread calling a global function?

Posted on 2004-04-07
3
403 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: 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

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…
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.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…

691 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