内容

更新日: 2006/1/5
バージョン: gtkmm24

■ 概要
■ ソース
■ コンパイル
■ 結果

TOP

スレッド

概要

スレッドを実行する

スレッドはGlib::Thread::create()を使用して作ることが出来るが、スレッドを作成する前に 一度だけ Glib::thread_init()を実行する必要がある。

スレッドの停止待ちにはGlib::Thread::join()を使う。

ソース

thread.cpp

#include <gtkmm.h>
#include <iostream>

// gettid
#include <sys/types.h>
#include <linux/unistd.h>
_syscall0(pid_t,gettid)

void slot_thread()
{
    std::cout << "thread tid = " << gettid() << std::endl;
    sleep( 2 );
    std::cout << "thread stop(" << gettid() << ")" << std::endl;
}

int main()
{
    // スレッドシステムの初期化
    Glib::thread_init();

    std::cout << "main tid = " << gettid() << std::endl;

    Glib::Thread* thread = Glib::Thread::create( sigc::ptr_fun( &slot_thread ), true );
    thread->join();

    std::cout << "main stop(" << gettid() << ")" << std::endl;
}

コンパイル

必要なコンパイルオプションは pkg-config を使って取得する。今回はスレッドを 使用しているので pkg-config --libs gthread-2.0 の指定も必要である。

g++ thread.cpp -o thread `pkg-config gtkmm-2.4 --cflags --libs` `pkg-config --libs gthread-2.0`

結果

main tid = 27743
thread tid = 27744
thread stop(27744)
main stop(27743)