linux上创建线程的类

来源:互联网 发布:软件市场营销方案 编辑:程序博客网 时间:2024/06/04 19:36

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
 
classCCriticalSection
{
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;
    }
    voidLock(){pthread_mutex_lock (m_pLock);}
    voidUnlock(){pthread_mutex_unlock (m_pLock);}
 
protected:
    pthread_mutex_t *m_pLock;
};
 
// 线程基类,实现线程的创建及删除
classCThread
{
public:
    CThread();
    ~CThread();
 
    BOOLCreateThread(size_tnStackSize = 1024 * 1024/*set to 1M*/);
    virtualBOOL Run();
    virtualint ExitInstance(); // default will 'delete this'
    virtualvoid Delete();
 
protected:
    // 计算当前时间
    QWORD GetCurTime(){structtimespec tv_date;clock_gettime(CLOCK_MONOTONIC, &tv_date);returntv_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
 
classCCriticalSection
{
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;
    }
    voidLock(){pthread_mutex_lock (m_pLock);}
    voidUnlock(){pthread_mutex_unlock (m_pLock);}
 
protected:
    pthread_mutex_t *m_pLock;
};
 
// 线程基类,实现线程的创建及删除
classCThread
{
public:
    CThread();
    ~CThread();
 
    BOOLCreateThread(size_tnStackSize = 1024 * 1024/*set to 1M*/);
    virtualBOOL Run();
    virtualint ExitInstance(); // default will 'delete this'
    virtualvoid Delete();
 
protected:
    // 计算当前时间
    QWORD GetCurTime(){structtimespec tv_date;clock_gettime(CLOCK_MONOTONIC, &tv_date);returntv_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
    BOOLbError;                // 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);
    DWORDnResult = 0;
    if(!pThread->Run())
    {
        nResult = pThread->ExitInstance();
    }
    pThread->Delete();
    returnNULL;
}
 
CThread::CThread()
{
    m_hThread = NULL;
}
 
CThread::~CThread()
{
    Delete();
}
 
BOOLCThread::CreateThread(size_tnStackSize)
{
    if(m_hThread != NULL)
        returnFALSE;
    // 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);
        returnFALSE;
    }
    // allow thread to continue, once resumed (it may already be resumed)
    sem_post(&startup.hEvent2);
    returnTRUE;
}
 
voidCThread::Delete()
{
    if(m_hThread != NULL)
    {
        pthread_cancel(m_hThread);
        m_hThread = NULL;
    }
}
 
BOOLCThread::Run()
{
    returnFALSE;
}
 
intCThread::ExitInstance()
{
    return0;
}

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
 
typedefchar                CHAR;
typedefunsigned char      BYTE;
typedefunsigned char      *PBYTE;
typedefshort               SHORT;
typedefunsigned short     USHORT;
typedefunsigned short     WORD;
typedefunsigned short     WCHAR;
typedefunsigned int       UINT;
typedefunsigned long      DWORD;
typedefunsigned longlong  QWORD;
typedefunsigned longlong  ULONGLONG;
typedeffloat               FLOAT;
typedefdouble              DOUBLE;
typedefstruct {
    unsignedlong Data1;
    unsignedshortData2;
    unsignedshortData3;
    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
原创粉丝点击