glibmm 2.56.0

Classes | Public Member Functions | Static Public Member Functions | Related Functions | List of all members
Glib::Threads::Thread Class Reference
Threads

Represents a running thread. More...

`#include <glibmm/threads.h>`

## Classes
class | Exit
| Exception class used to exit from a thread. More...

## Public Member Functions
| Thread (const Thread&)=delete
Thread& | operator= (const Thread&)=delete
void | join ()
| Waits until the thread finishes. More...
GThread* | gobj ()
const GThread* | gobj () const

## Static Public Member Functions
static Thread* | create (const sigc::slot< void >& slot)
| Creates a new thread. More...
static Thread* | create (const sigc::slot< void >& slot, const std::string& name)
| Creates a new named thread. More...
static Thread* | self ()
| Returns the Thread* corresponding to the calling thread. More...
static void | yield ()
| Gives way to other threads waiting to be scheduled. More...

## Related Functions
(Note that these are not member functions.)
Thread* | wrap (GThread* gobject)
| A C++ wrapper for the C object. More...

## Detailed Description

Represents a running thread.

An instance of this class can only be obtained with create(), self(), or wrap(GThread*). It's not possible to delete a Thread object. You must call join() to avoid a memory leak.

Note
  g_thread_exit() is not wrapped, because that function exits a thread without any cleanup. That's especially dangerous in C++ code, since the destructors of automatic objects won't be invoked. Instead, you can throw a Threads::Thread::Exit exception, which will be caught by the internal thread entry function.
  The thread entry slot doesn't have the void* return value that a GThreadFunc has. If you want to return any data from your thread, you can pass an additional output argument to the thread's entry slot.

Deprecated:
  Please use std::thread instead.

## Constructor & Destructor Documentation

## ◆ Thread()

Glib::Threads::Thread::Thread | ( | const Thread& |  | ) |
| delete

## Member Function Documentation

## ◆ create() [1/2]

static Thread* Glib::Threads::Thread::create | ( | const sigc::slot< void > & | slot | ) |
| static

Creates a new thread.

You can wait for this thread's termination by calling join().

The new thread executes the function or method slot points to. You can pass additional arguments using sigc::bind(). If the thread was created successfully, it is returned, otherwise a Threads::ThreadError exception is thrown.

Because sigc::trackable is not thread-safe, if the slot represents a non-static class method and is created by sigc::mem_fun(), the class concerned should not derive from sigc::trackable. You can use, say, boost::bind() or, in C++11, std::bind() or a C++11 lambda expression instead of sigc::mem_fun().

Parameters

  slot | A slot to execute in the new thread.

Returns
  The new Thread* on success.

Exceptions

  Glib::Threads::ThreadError |

## ◆ create() [2/2]

static Thread* Glib::Threads::Thread::create | ( | const sigc::slot< void > & | slot,
|  | const std::string & | name
| ) |  |
| static

Creates a new named thread.

You can wait for this thread's termination by calling join().

The new thread executes the function or method slot points to. You can pass additional arguments using sigc::bind(). If the thread was created successfully, it is returned, otherwise a Threads::ThreadError exception is thrown.

Because sigc::trackable is not thread-safe, if the slot represents a non-static class method and is created by sigc::mem_fun(), the class concerned should not derive from sigc::trackable. You can use, say, boost::bind() or, in C++11, std::bind() or a C++11 lambda expression instead of sigc::mem_fun().

The name can be useful for discriminating threads in a debugger. It is not used for other purposes and does not have to be unique. Some systems restrict the length of name to 16 bytes.

Parameters

  slot | A slot to execute in the new thread.
  name | A name for the new thread.

Returns
  The new Thread* on success.

Exceptions

  Glib::Threads::ThreadError |

Since glibmm 2.36:

## ◆ gobj() [1/2]

GThread* Glib::Threads::Thread::gobj | ( |  | ) |

## ◆ gobj() [2/2]

const GThread* Glib::Threads::Thread::gobj | ( |  | ) | const

## ◆ join()

void Glib::Threads::Thread::join | ( |  | ) |

Waits until the thread finishes.

Waits until the thread finishes, i.e. the slot, as given to create(), returns or g_thread_exit() is called by the thread. (Calling g_thread_exit() in a C++ program should be avoided.) All resources of the thread including the Glib::Threads::Thread object are released.

## ◆ operator=()

Thread& Glib::Threads::Thread::operator= | ( | const Thread& |  | ) |
| delete

## ◆ self()

static Thread* Glib::Threads::Thread::self | ( |  | ) |
| static

Returns the Thread* corresponding to the calling thread.

Returns
  The current thread.

## ◆ yield()

static void Glib::Threads::Thread::yield | ( |  | ) |
| static

Gives way to other threads waiting to be scheduled.

This function is often used as a method to make busy wait less evil. But in most cases, you will encounter, there are better methods to do that. So in general you shouldn't use this function.