Linux论坛's Archiver

《开源》旗舰电子杂志2008年11期发布,火热下载中!

littlefrog 发表于 2008-8-4 13:17

求助:线程应用pthread_

最近研究linux线程操作时,想对里面提供的函数封装一下,实现任务创建,删除,等待等操作。。。
任务创建与等待相对容易
任务删除,我用的是pthread_Cancel,可用它有一定的危险性。。。请各位给些建议了。。
typedef struct
{
        pthread_t  tid;                     //任务的PID
        char       delete_flag;             //任务需要删除标志
        char       used;                    //本块任务块是否可用标志
}task_id;

char APPTaskDelete(task_id *task_id, char method)
{
        if (task_id == NULL)
        {
                TASK_PRINT(("%s:%u Argument has invalid value: NULL...\n", __FUNCTION__, __LINE__));
                return -1;
        }
       
        switch (method)
        {
                case 0:
                        if (task_id->used != 1)
                        {
                                TASK_PRINT(("task is not create....%s\n", __FUNCTION__));
                                return -1;
                        }
                        task_id->delete_flag = 1;
                       
                        if (pthread_join(task_id->tid, NULL) != 0)
                        {
                                while (task_id->delete_flag != 2)   //分离线程时
                                   usleep(10);
                               
                        }
                        task_id->delete_flag = 0;
                        return 0;
                       
                case 1:
                        pthread_cancel(task_id->tid);
                        pthread_join(task_id->tid, NULL);
                        task_id->used = 0;
                        return 0;
                       
                case 2:                               //通知线程需要结束,并立即返回
                        if (task_id->used != 1)
                        {
                                TASK_PRINT(("task is not create....%s\n", __FUNCTION__));
                                return -1;
                        }
                        task_id->delete_flag = 1;
                        pthread_detach(task_id->tid);
                        return 0;
                       
                default:
                        TASK_PRINT(("%s:%u Argument has invalid value: NULL...\n", __FUNCTION__, __LINE__));
                        return -1;                       
               
               
        }

       
}
qq:94643108  email:zhangshuai-0116@163.com

littlefrog 发表于 2008-8-4 13:43

在线等

littlefrog 发表于 2008-8-4 15:02

:'(

littlefrog 发表于 2008-8-4 16:46

也是这么冷清

littlefrog 发表于 2008-8-5 15:41

冷清依旧…………以后不来了:L

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.