-2

I'm trying to understand a class, which is part of an assignment we have to do, but I don't understand some of the syntax:

void sleep() 
    { 
        Thread::yield(); 
    } // implicit unlock()

What does Thread::yield(); mean? Which thread is that? There is no declaration in the code for this Thread object. What is :: ?

Tuma
  • 603
  • 1
  • 7
  • 35
  • 3
    need more context...What's `Thread`? Some class in one of the numerous C++ threading libraries? Your own home-grown class? – T.C. Sep 02 '14 at 22:43
  • In this context, `X::Y()` means that the static method named `Y` of type `X` is called. So the next step is to go to the definition of `Y` and see what it does. – Jon Sep 02 '14 at 22:44
  • You basically just need to learn C++. – David Schwartz Sep 02 '14 at 22:55
  • 1
    @Jon it can be also a global method enclosed in Thread namespace – 4pie0 Sep 02 '14 at 23:01
  • 1
    Or calling a base class member function explicitly in a derived class member function. The point is that it's impossible to tell because there's not enough context. – T.C. Sep 02 '14 at 23:01
  • 0d0a and T.C.: True on both counts. I just guesstimated in order to be able to give some kind of concrete suggestion. – Jon Sep 02 '14 at 23:07
  • 2
    I find it hilarious that the title asks "in this context", and there is no context given whatsoever. – Captain Giraffe Sep 02 '14 at 23:16

3 Answers3

3

A call to

Thread::yield();

means a call of a method named yield which is enclosed in Thread class or namespace ( it can be global method just enclosed in a namespace). The name yield suggests it is a call made to give up the CPU cycles and enable other threads running. Search in your project for Thread and you will find what it is exactly. Method named yield should be present within Thread class or namespace.

What is :: ?

:: is a scope resolution operator

C++ Standard n3337 § 3.4.3 Qualified name lookup

The name of a class or namespace member or enumerator can be referred to after the :: scope resolution operator (5.1) applied to a nested-name-specifier that denotes its class, namespace, or enumeration. If a :: scope resolution operator in a nested-name-specifier is not preceded by a decltype-specifier, lookup of the name preceding that :: considers only namespaces, types, and templates whose specializations are types. If the name found does not designate a namespace or a class, enumeration, or dependent type, the program is ill-formed.[ Example:

class A {
public:
    static int n;
};

int main() {
    int A;
    A::n = 42; // OK
    A b; // ill-formed: A does not name a type
}

—end example ]

https://stackoverflow.com/a/9338301/1141471

Community
  • 1
  • 1
4pie0
  • 29,204
  • 9
  • 82
  • 118
0

Probably it means the function you are looking at is derived from Thread and sleep is merely delegating responsibility for "sleeping" to the parent class's static yield function. BUT, we really need to see more before we can say for sure.

Dwayne Towell
  • 8,154
  • 4
  • 36
  • 49
0

So basically the :: means that you are calling a static class method on the Thread class (notice you did not have to create an instance of the class Thread to call that method).

The yield operation causes the thread that called the sleep method to yield execution to other waiting threads. Basically that thread is paused and other waiting threads are allowed to run. The OS reschedules the sleeping thread to run in the future, based on the threads priority and the status of other threads.

Ron
  • 1,249
  • 9
  • 20