线程创建和结束理解

来源:互联网 发布: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
原创粉丝点击