关闭

linux上创建线程的类

标签: 线程
114人阅读 评论(0) 收藏 举报
分类:

http://www.oschina.net/code/snippet_2812_6277

1. [代码][C/C++]代码     

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#ifndef THREAD_H_HEADER_INCLUDED_B223B067
#define THREAD_H_HEADER_INCLUDED_B223B067
 
class CCriticalSection
{
public:
    CCriticalSection()
    {
        m_pLock =(pthread_mutex_t*) malloc (sizeof (pthread_mutex_t));
        pthread_mutexattr_t attr;
        pthread_mutexattr_init (&attr);
        pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
        pthread_mutex_init (m_pLock, &attr);
        pthread_mutexattr_destroy (&attr);
    }
    virtual ~CCriticalSection()
    {
        pthread_mutex_destroy (m_pLock);
        free (m_pLock);
        m_pLock = NULL;
    }
    void Lock(){pthread_mutex_lock (m_pLock);}
    void Unlock(){pthread_mutex_unlock (m_pLock);}
 
protected:
    pthread_mutex_t *m_pLock;
};
 
// 线程基类,实现线程的创建及删除
class CThread
{
public:
    CThread();
    ~CThread();
 
    BOOL CreateThread(size_t nStackSize = 1024 * 1024/*set to 1M*/);
    virtual BOOL Run();
    virtual int ExitInstance(); // default will 'delete this'
    virtual void Delete();
 
protected:
    // 计算当前时间
    QWORD GetCurTime(){struct timespec tv_date;clock_gettime(CLOCK_MONOTONIC, &tv_date);return tv_date.tv_sec * 1000 + tv_date.tv_nsec / 1000000;};
 
protected:
    pthread_t m_hThread;       // this thread's HANDLE
};
 
 
 
#endif /* THREAD_H_HEADER_INCLUDED_B223B067 */

2. [文件] Thread.h ~ 1KB     下载(83)     

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#ifndef THREAD_H_HEADER_INCLUDED_B223B067
#define THREAD_H_HEADER_INCLUDED_B223B067
 
class CCriticalSection
{
public:
    CCriticalSection()
    {
        m_pLock =(pthread_mutex_t*) malloc (sizeof (pthread_mutex_t));
        pthread_mutexattr_t attr;
        pthread_mutexattr_init (&attr);
        pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE);
        pthread_mutex_init (m_pLock, &attr);
        pthread_mutexattr_destroy (&attr);
    }
    virtual ~CCriticalSection()
    {
        pthread_mutex_destroy (m_pLock);
        free (m_pLock);
        m_pLock = NULL;
    }
    void Lock(){pthread_mutex_lock (m_pLock);}
    void Unlock(){pthread_mutex_unlock (m_pLock);}
 
protected:
    pthread_mutex_t *m_pLock;
};
 
// 线程基类,实现线程的创建及删除
class CThread
{
public:
    CThread();
    ~CThread();
 
    BOOL CreateThread(size_t nStackSize = 1024 * 1024/*set to 1M*/);
    virtual BOOL Run();
    virtual int ExitInstance(); // default will 'delete this'
    virtual void Delete();
 
protected:
    // 计算当前时间
    QWORD GetCurTime(){struct timespec tv_date;clock_gettime(CLOCK_MONOTONIC, &tv_date);return tv_date.tv_sec * 1000 + tv_date.tv_nsec / 1000000;};
 
protected:
    pthread_t m_hThread;       // this thread's HANDLE
};
 
 
 
#endif /* THREAD_H_HEADER_INCLUDED_B223B067 */

3. [文件] Thread.cpp ~ 2KB     下载(75)     

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "stdhead.h"
#include <semaphore.h>
#include "Thread.h"
 
struct _MY_THREAD_STARTUP
{
    CThread* pThread;           // CThread for new thread
    sem_t hEvent;               // event triggered after success/non-success
    sem_t hEvent2;              // event triggered after thread is resumed
    // strictly "out" -- set after hEvent is triggered
    BOOL bError;                // TRUE if error during startup
};
 
void * MyThreadEntry(void * pParam)
{
    _MY_THREAD_STARTUP* pStartup = (_MY_THREAD_STARTUP*)pParam;
    CThread* pThread = pStartup->pThread;
    // pStartup is invlaid after the following
    // SetEvent (but hEvent2 is valid)
    //sem_t hEvent2 = pStartup->hEvent2;
    // allow the creating thread to return from CWinThread::CreateThread
    sem_post(&pStartup->hEvent);
    // wait for thread to be resumed
    sem_wait(&pStartup->hEvent2);
    sem_destroy(&pStartup->hEvent2);
    DWORD nResult = 0;
    if(!pThread->Run())
    {
        nResult = pThread->ExitInstance();
    }
    pThread->Delete();
    return NULL;
}
 
CThread::CThread()
{
    m_hThread = NULL;
}
 
CThread::~CThread()
{
    Delete();
}
 
BOOL CThread::CreateThread(size_t nStackSize)
{
    if(m_hThread != NULL)
        return FALSE;
    // setup startup structure for thread initialization
    _MY_THREAD_STARTUP startup;
    memset(&startup, 0, sizeof(startup));
    startup.pThread = this;
    sem_init(&startup.hEvent, 0, 0);
    sem_init(&startup.hEvent2, 0, 0);
    // create the thread (it may or may not start to run)
    pthread_attr_t thread_attr;
    int  nRet = pthread_attr_init(&thread_attr);
    nRet = pthread_attr_setstacksize(&thread_attr, nStackSize);
    nRet = pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
    nRet = pthread_create(&m_hThread, &thread_attr, MyThreadEntry, &startup);
    sem_wait(&startup.hEvent);
    sem_destroy(&startup.hEvent);
    // if error during startup, shut things down
    if(startup.bError)
    {
        pthread_cancel(m_hThread);
        m_hThread = NULL;
        sem_destroy(&startup.hEvent2);
        return FALSE;
    }
    // allow thread to continue, once resumed (it may already be resumed)
    sem_post(&startup.hEvent2);
    return TRUE;
}
 
void CThread::Delete()
{
    if(m_hThread != NULL)
    {
        pthread_cancel(m_hThread);
        m_hThread = NULL;
    }
}
 
BOOL CThread::Run()
{
    return FALSE;
}
 
int CThread::ExitInstance()
{
    return 0;
}

4. [代码][C/C++]代码     

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#ifndef __STDHEAD_H__
#define __STDHEAD_H__
#include <assert.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <malloc.h>
#include <memory.h>
#include <pthread.h>
#include <stddef.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <fcntl.h>
#include <arpa/inet.h>      /* inet_ntoa() to format IP address */
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/vfs.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_icmp.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <uuid/uuid.h>
#ifdef SOLARIS
#include <sys/sockio.h>
#endif
 
typedef char                CHAR;
typedef unsigned char       BYTE;
typedef unsigned char       *PBYTE;
typedef short               SHORT;
typedef unsigned short      USHORT;
typedef unsigned short      WORD;
typedef unsigned short      WCHAR;
typedef unsigned int        UINT;
typedef unsigned long       DWORD;
typedef unsigned long long  QWORD;
typedef unsigned long long  ULONGLONG;
typedef float               FLOAT;
typedef double              DOUBLE;
typedef struct {
    unsigned long  Data1;
    unsigned short Data2;
    unsigned short Data3;
    BYTE           Data4[8];
} GUID;
#define IsEqualGUID(rguid1, rguid2) (!memcmp(&rguid1, &rguid2, sizeof(GUID)))
 
#define BOOL                                int
#define TRUE                                1
#define FALSE                               0
#define max(a,b)                            (((a) > (b)) ? (a) : (b))
#define min(a,b)                            (((a) < (b)) ? (a) : (b))
 
#endif

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:254853次
    • 积分:3561
    • 等级:
    • 排名:第9708名
    • 原创:45篇
    • 转载:367篇
    • 译文:0篇
    • 评论:22条
    最新评论