当前位置: 首页 > 工具软件 > Libjingle > 使用案例 >

libjingle thread的使用

胡鸿远
2023-12-01

libjingle支持多线程,多线程主要继承talk_base::Thread,利用其post等函数注册talk_base::MessageHandler和talk_base::Message的message_id和MessageData

#ifndef __THREAD_TEST_H
#define __THREAD_TEST_H
#include <string>
#include <stdio.h>
#include "talk/base/thread.h"
class MsgData : public talk_base::MessageData
{
	public:
		MsgData(std::string str):info(str)
		{
		}
		std::string info;
};

enum
{
	MSG_HELP
};	

class TestThread : public talk_base::Thread
{
	public:
		void sendMessage(talk_base::MessageHandler *handler, std::string info)
		{
			Post(handler, MSG_HELP,new MsgData(info)); 
		}
		
};
class Police : public talk_base::MessageHandler
{
	public:
	
	Police()
	{
		thread.Start();
	}
	void Help(const std::string &info)
	{
		
		thread.sendMessage(this, info);
	}

	void workThreadHelp(const std::string &info)
	{
		
	}

	virtual void OnMessage(talk_base::Message *msg)
	{
		switch(msg->message_id)
		{
			case MSG_HELP:
				MsgData *data = (MsgData *)msg->pdata;

				printf("thread msg:%s\r\n", data->info.c_str());
				break;
		}
	}

	private:
		TestThread thread;
};
#endif
#include "SignalTest.h"
#include "ThreadTest.h"
int main(int argc, char **argv) 
{
	Sender sender;
	Receiver recv(sender);
	sender.Panic();
	
	Police p;
	p.Help("help me!!");
	
	return 0;
}



 类似资料: