本人原先看到的多线程编程都是封装以后的,雏形却从来没了解过。今天受到高人指点,真是醍醐灌顶啊,首先要对高人的无私奉献表示极度的敬仰,接着记录一下战果,供我养的肥鸡鉴定:
int threadFunc()
{
.....
.....
return 0;
}
typedef FunctionPtrType threadFunc*;
std::list<FunctionPtrType> g_tasks; //函数指针类型的链表
int threadProcess()
{
while(!bExit)
{
FunctionPtrType fp = g_task.front();
g_tasks.pop_front();
fp();
}
return 0;
}
void myfunc2() //将所有线程需要处理的函数threadFunc()的函数指针保存在list中
{
g_tasks.push_back(threadFunc);
}
int main()
{
myfunc2();
pthread_create(&threadProcess, ...);
return 0;
}
上述中提到的std::list<FunctionPtrType> g_tasks; 就是我们平时所说的消息队列的最原始表现形式