OpenThreads线程管理

OpenSceneGraph中线程(OpenThreads)的作用多种多样,涉及到信号量机制,同步异步锁等多种知识点。

OpenThreads:
Threads:创建线程,类似于windows中的_beginthread,
Mutex :创建互斥,类似于windows中的CreateMutex,用于针对多个线程访问同一资源 情况。
Condition:创建条件,用于符合条件时才启动某个线程,使用Mutex实现。
Barrier :创建壁垒,用于同步线程,比如某操作必须前面几个线程完成时才能进行。


线程调用大致分为四个方法:

其一:运用Windows.h的线程方法,通过创建线程函数,进行线程的调用以及信号量机制的占用以及
释放。这里用两个进程买票的例子。
#include
#include
#include

int tickets = 10;
HANDLE iMutex;//创建一个句柄
void sellTickets1(void *ptr)
{
while (tickets > 0)
{
//等待信号量
WaitForSingleObject(iMutex, INFINITE);
Sleep(10);
std::cout << "Thread1 sell : " << tickets << std::endl;
tickets--;
//释放信号量
ReleaseMutex(iMutex);
}
}
void sellTickets2(void *ptr)
{
while (tickets > 0)
{
//等待信号量
WaitForSingleObject(iMutex, INFINITE);
Sleep(10);
std::cout << "Thread2 sell : " << tickets << std::endl;
tickets--;
//释放信号量
ReleaseMutex(iMutex);
}
}

int main()
{
//创建一个线程,线程调用sellTickets1函数
HANDLE t1 = (HANDLE)_beginthread(&sellTickets1, 0, 0);
//创建一个线程,线程调用sellTickets2函数
HANDLE t2 = (HANDLE)_beginthread(&sellTickets2, 0, 0);
//创建一个信号量,用于互斥对象
iMutex = (HANDLE)CreateMutex(0, FALSE, 0);
Sleep(2000);
system("pause");
return 0;
}


其二:通过实例化继承Thread的类的对象,然后将其调用start函数,启动线程,通过调用
ScopedLock函数规定其线程在这个范围内执行结束后才能释放,其余线程处于等待时间。
#include
#include
#include
#include
#include

OpenThreads::Mutex mutex;
class ThreadSelf : public OpenThreads::Thread
{
public:
ThreadSelf(int a) { threadid = a; }
virtual ~ThreadSelf()
{
while (isRunning())
OpenThreads::Thread::YieldCurrentThread();
}
void run()
{
//只有在下边的全部运行之后,才会将下一个进程纳入运行
OpenThreads::ScopedLock lock(mutex);
int count = 10;
while (--count)
{
Sleep(10);
std::cout <<"Thread print:"<< threadid << std::endl;
}
}
int threadid;
};
int main()
{
ThreadSelf t1(10);
ThreadSelf t2(6);
t1.start();
t2.start();
Sleep(3000);
system("pause");
return 1;
}

其三:通过实例化继承Thread的类的对象,然后将其调用start函数,启动线程,通过调用
block(n)函数规定其之前的n个线程完成时后才能执行之后的程序,否则将一直处于等待时
间。n从0开始,如果进程执行次数大于所给数字,就会冲破壁垒,然后执行下边的进程。
#include
#include
#include
#include

OpenThreads::Barrier bar;
class ThreadSelf : public OpenThreads::Thread
{
public:
ThreadSelf(int a) { threadid = a; }
virtual ~ThreadSelf()
{
while (isRunning())
OpenThreads::Thread::YieldCurrentThread();
}
void run()
{
int count = 10;
while (--count)
{
Sleep(10);
std::cout <<"Thread print:"<< threadid << std::endl;
}
bar.block();
}
int threadid;
};
int main()
{
ThreadSelf t1(10);
ThreadSelf t2(6);
t1.start();
bar.block(2);
t2.start();
bar.block(0);
std::cout << "Here" << std::endl;
Sleep(3000);
system("pause");
return 1;
}



其四:运用Windows.h的线程方法,通过_beginthread创建线程函数,通过Condition定义的对象
cont来进行信号量的等待以及释放,通过Mutex来作为条件,运用signal函数以及wait函数进
行线程的管理。
#include
#include
#include
#include

int condition = 0;
OpenThreads::Condition cont;
OpenThreads::Mutex mutex;
void setCondition(void *ptr)
{
condition = 1;
cont.signal();
}
void ifCondition(void *ptr)
{
cont.wait(&mutex, INFINITE);
if (condition)
std::cout << "Condifion os find" << std::endl;
}

int main()
{
HANDLE t1 = (HANDLE)_beginthread(&ifCondition, 0, 0);
HANDLE t2 = (HANDLE)_beginthread(&setCondition, 0, 0);
Sleep(1000);
std::cout << "END" << std::endl;
system("pause");
return 0;
}

你可能感兴趣的:(osg类)