线程创建和结束理解
来源:互联网 发布:qq for linux 64位 编辑:程序博客网 时间:2024/05/13 01:14
主函数创建线程:
#include <stdio.h>#include <stdlib.h>#include <unistd.h> #include "messageRun.h"#include "tasklist.h"#include "messageFunc.h"#include "messageDef.h"int main(int argc,char* argv[]){VOBG VoBgPar ={.lightness = 50,.ColorTemp = 50,.Contrast = 50,};MsgParamter MsgPar={.MsgCmd =MESSAGE_CMD_TEST,.funcParamter =(void*)"hello test !",.func = testCmdFunc,};StartMessageOS(&MsgPar);sleep(2);MsgPar.MsgCmd =MESSAGE_CMD_SETBG;MsgPar.funcParamter = (void*)&VoBgPar;MsgPar.func = testCmdSetBG;SetMsgParamter((void*)&MsgPar);sleep(2);DestroyMessageOs();//sleep(2);printf("I am running \n");sleep(20);//printf("main thread exit \n");pthread_exit(NULL);//直接退出 //return 0;}
线程创建文件:
#include <pthread.h>#include <stdio.h>#include <unistd.h> #include "messageDef.h"#include "messageRun.h"void* MsgOsThread(void* MsgInfo);static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;static pthread_t thread; //创建线程函数返回类型MsgParamter* pMsgPar;void StartMessageOS(MsgParamter* pMsgPar){ pthread_create(&thread,0,MsgOsThread,(void*)pMsgPar);}void DestroyMessageOs(){printf("Destroy it \n");pthread_cancel(thread);//给子线程发送结束信息,//printf("join:%d\n",pthread_join(thread,NULL));//主线程等待子线程退出,<span style="font-family: Arial, Helvetica, sans-serif;">结束后清理资源。</span>printf("detach result:%d \n",pthread_detach(thread));//<span style="font-family: Arial, Helvetica, sans-serif;">主线程不需要等待子线程退出,剥离主线程,设置子线程的状态标志,以便结束后清理资源</span>}void* MsgOsThread(void* MsgInfo){ pMsgPar = (MsgParamter*)MsgInfo; while(1){pthread_mutex_lock (&mutex); switch(pMsgPar->MsgCmd){ case MESSAGE_CMD_TEST: //test cmdpMsgPar->func(pMsgPar->funcParamter); break; case MESSAGE_CMD_SETBG:pMsgPar->func(pMsgPar->funcParamter); break; default: break; }pthread_mutex_unlock (&mutex);sleep(1); } return NULL;}void SetMsgParamter(void* MsgInfo){ pthread_mutex_lock (&mutex); pMsgPar = (MsgParamter*)MsgInfo; pthread_mutex_unlock (&mutex);}文件定义:
typedef void (*MsgFunc)(void*);typedef struct _tagMsgParamter{ unsigned short int MsgCmd;void* funcParamter;MsgFunc func;}MsgParamter;void StartMessageOS(MsgParamter* pMsgPar);void SetMsgParamter(void* MsgInfo);
typedef struct _tagVOBg{<span style="white-space:pre"></span>unsigned int lightness;//100<span style="white-space:pre"></span>unsigned int ColorTemp;//100<span style="white-space:pre"></span>unsigned int Contrast;//100}VOBG;
void testCmdFunc(void* paramter);void testCmdSetBG(void* paramter);
void testCmdFunc(void* paramter){<span style="white-space:pre"></span>char* testText = (char*)paramter;<span style="white-space:pre"></span>printf("%s \n",testText);}void testCmdSetBG(void* paramter){<span style="white-space:pre"></span>VOBG *VoBgPara = (VOBG *)paramter;<span style="white-space:pre"></span>printf("lightness:%d, colorTemp:%d, Contrast:%d\n",VoBgPara->lightness,VoBgPara->ColorTemp,VoBgPara->Contrast);}
#define MESSAGE_CMD_TEST<span style="white-space:pre"></span>0x8000#define MESSAGE_CMD_SETBG <span style="white-space:pre"></span>0x8001
0 0
- 线程创建和结束理解
- 创建线程和结束线程
- linux 线程创建和结束
- Java创建和结束线程
- 线程的创建和结束的方法
- vc 线程的创建,暂停和结束
- vc 线程的创建,暂停和结束
- vc 线程的创建、暂停和结束
- AfxBeginThread: Worker线程的创建和结束
- vc 线程的创建、暂停和结束
- AfxBeginThread创建和事件对象结束线程的方法
- WM_QUIT和结束线程
- 线程初步(1)—— 线程的创建、参数和返回值、结束、状态、取消
- AfxBeginThread 开始和结束线程
- java线程开启和结束
- Linux 线程的创建与结束
- VC 创建和结束进程
- VC++创建和结束进程
- poj3259:spfa解法
- linux中banner命令的用法
- 在线教育克星:sikuli
- protobuf 使用示例
- 关联分析——Apriori算法
- 线程创建和结束理解
- MyBatis入门05 一对多关联
- HDU 5349 MZL's simple problem(水题)
- Git笔记
- Win32开发 - VS2012常用插件
- Kotlin学习笔记(一)
- ListView.setSelection(int)方法执行流程源码解析(API 21)
- 从0开始<二>:1
- linux查看文件大小,卸载挂载硬盘