kwikness
kwikness

Reputation: 1445

Code Blocks and Thread Priority

When I put code in blocks, like so:

    {
      f1();
      f2();
    }

Does it GUARANTEE that those two functions will be called after one another by the CPU regardless of what other processes are running?

The reason I ask is because f1, in my case, does something with the OS, then f2 relies on the the OS being in the same state in order to work properly.

If not, (which I really don't think it does), is there any C++ construct that will help me do something like this?

Also, I'll give some background information about what's being done in my real code:

f1() is actually a function that spawns a top-level window on a win32 system. f2() is a function that searches for the top-most level window (that is not the taskbar). So, my concern is that I could run into an issue wherein other top-level windows may be created in the time between when f1 finishes and f2 has not yet been executed, in which case f2 will find the wrong window.

Upvotes: 2

Views: 357

Answers (6)

king_nak
king_nak

Reputation: 11513

It is guaruanteed that the thread executing this will first finish with f1, and directly after that call f2. However, when you rely on state that is outside of the currently executing thread (some shared OS resource), than it is not guaranteed that other threads/processes/hardware will modify this resources, even while executing either of those methods. Then you need some sort of lock to protect that resource being accessed by others. When such a lock spans over that block, it is also guaranteed that it won't change between the subsequent calls.

Depending on your OS and resource, such a lock might be available. But there is too little information for this

Upvotes: 0

Oleg
Oleg

Reputation: 798

f2 will be called after f1. But something may happen between them (it can also change OS state)

Upvotes: 1

Shamim Hafiz - MSFT
Shamim Hafiz - MSFT

Reputation: 22104

Given the code block as it is and no external issue occurring, you are guaranteed that f2() will be called only after f1() terminates.

Upvotes: 1

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385194

f2 could run not directly after f1, and f2 should therefore not rely on the system being in the state that f1 left it in. In fact, neither should be playing with state on a multi-threaded system unless you have the proper locks in place.

However, it is guaranteed that f1 will have returned before f2 is called.

Upvotes: 3

K Mehta
K Mehta

Reputation: 10553

It'll always run in that order, but if you have threads which modify your underlying variables which are useding f1 and f2, you'd have to protect them using mutex locks

Upvotes: 1

Nicol Bolas
Nicol Bolas

Reputation: 473537

Does it GUARANTEE that those two functions will be called after one another by the CPU regardless of what other processes are running?

Yes. If those functions spawn threads or whatever, they may run in different orders. But the actual code for those functions will be executed in order.

If you're talking about access to anything outside of your process, that's a different matter. C and C++, as languages, have no concept of concurrency and such (outside of a few language features like volatile). Everything within a program is sequential, but the world outside of your program is unknown and unknowable (without a library that provides such hooks).

If the library in question does not provide a way to prevent another process from touching some global state that your code touched, then you have no way to be sure that the state will remain set as you set it.

Upvotes: 5

Related Questions