Discussion:
Two threads accessing a global function problem
(too old to reply)
Nithin
2009-11-27 17:24:43 UTC
Permalink
Hi all,

I want to create two threads in VC++.Both threads will access the same
function.

Say the function has some ten lines of code.The first thread excutes
only 5 lines and calls
the second thread.the second thread calls the same function executes
the full function(10
lines) and ends.The first thread resumes and executes the remaining
five lines and
terminates.

Can someone please share code/suggestions or pointers to good links on
how I can do
this..

Thanks in adavnce,
Nithin
Igor Tandetnik
2009-11-27 18:31:16 UTC
Permalink
Post by Nithin
I want to create two threads in VC++.Both threads will access the same
function.
Say the function has some ten lines of code.The first thread excutes
only 5 lines and calls
the second thread.the second thread calls the same function executes
the full function(10
lines) and ends.The first thread resumes and executes the remaining
five lines and
terminates.
What's the point of these bizarre requirements? What are you really trying to achieve?

Anyway, if you really, really insist:

DWORD WINAPI ThreadProc(LPVOID param) {
; // line 1
; // line 2
; // line 3
; // line 4
; // line 5
if (!param) {
DWORD thread_id;
HANDLE h = CreateThread(NULL, 0, ThreadProc, (LPVOID)1, &thread_id);
WaitForSingleObject(h, INFINITE);
CloseHandle(h);
}
; // line 6
; // line 7
; // line 8
; // line 9
; // line 10
}

int main() {
DWORD thread_id;
HANDLE h = CreateThread(NULL, 0, ThreadProc, NULL, &thread_id);
WaitForSingleObject(h, INFINITE);
CloseHandle(h);
return 0;
}
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925
Tim Roberts
2009-11-28 20:05:44 UTC
Permalink
Post by Nithin
I want to create two threads in VC++.Both threads will access the same
function.
Say the function has some ten lines of code.The first thread excutes
only 5 lines and calls the second thread.the second thread calls
the same function executes the full function(10 lines) and ends.
The first thread resumes and executes the remaining five lines and
terminates.
Threads do not "call" other threads. Threads execute independently of each
other.

You may be thinking about a problem of "synchronizing" two threads, where
one thread has to block until another has accomplished some task. This is
exactly what synchronization objects like events, mutexes, and semaphores
were designed for.

If you really need low-level control of the execution of threads, you could
consider using fibers instead. However, with very few exceptions, fibers
are never the right answer.
--
Tim Roberts, ***@probo.com
Providenza & Boekelheide, Inc.
Ulrich Eckhardt
2009-11-30 09:53:31 UTC
Permalink
Post by Nithin
I want to create two threads in VC++.Both threads will access the same
function.
This is normally not a problem. The problem is when they access the same
data and at least one of them writes that data. Note that a function-static
object is not a local variable.
Post by Nithin
Say the function has some ten lines of code.The first thread excutes
only 5 lines and calls the second thread.
Sorry, but threads don't "call" each other. As mentioned, they execute
independently.
Post by Nithin
the second thread calls the same function executes the full function(10
lines) and ends.The first thread resumes and executes the remaining
five lines and terminates.
Can someone please share code/suggestions or pointers to good links on
how I can do this..
There is no problem, at least not in the description you show.

Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite

Sator Laser GmbH
Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
Continue reading on narkive:
Loading...