linux上创建线程的类

转载 2016年08月31日 10:54:37

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

在VS2010的环境下,给mfc下的对话框添加启动画面

摸索了半天,终于完成简单的开机启动画面了,不过大部分都是套用别人的模板的,即下载Spash.cpp 以及Splash.h文件并添加至工程中,该文件在附件中有讲。 首先下载一张.BMP图形,该图形最好...

[转载]在MFC窗口中嵌入VLC ActiveX控件

经过一番的google和折腾,终于把VLC ActiveX控件嵌入MFC窗口并实现简单的视频播放了,虽然发现很多问题(比如vlc参数设置[注1],程序关闭后进程不结束等等),觉得最终可能还是会选择li...

linux下创建线程内存泄漏,php的json_encode等

这次还是把遇到的几个问题整理一下,希望再遇到的同学能轻松解决。另外最近博客的feeds延迟更新的原因也会一起说明一下。 1.linux下创建线程导致内存泄漏 今天在外网发布了一个server之后,...
  • wide288
  • wide288
  • 2012年11月28日 09:25
  • 739

linux之TCP/IP创建线程实现简单聊天(2)

周末无聊,创建个多线程实现简单聊天功能,顺便温习下linux的TCP/IP。 下面贴代码大家一起学习!有些代码跟(1)篇重复,我就不注释了!   ========tcp_server.c====...

Linux多线程编程(创建线程)

创建线程 创建线程的函数是pthread_create,具体定义如下: [cpp] view plaincopy #include     ...
  • tb_huge
  • tb_huge
  • 2014年06月11日 20:54
  • 348

linux下C语言多线程(第一篇)创建线程

Linux下用C开发多线程程序,Linux系统下的多线程遵循POSIX线程接口,称为pthread。 #include int pthread_create(pthr...

Linux创建线程的属性设置

如果线程调用函数:sleep()函数,就是挂起自己,线程挂起,此时CPU的时间片就会分配给其他进程。而如果当前线程自己轮询操作,就会一直使用系统分配给自己的时间片。 对于一个系统而言:系统会分配给线程...

创建线程类轻松实现线程管理

  • 2009年08月14日 21:03
  • 24KB
  • 下载

创建线程WENJIAN

  • 2015年01月12日 20:39
  • 2KB
  • 下载

Java创建线程(Runnable接口和Thread类)

Java创建线程(Runnable接口和Thread类) 大多数情况,通过实例化一个Thread对象来创建一个线程。Java定义了两种方式: 实现Runnable 接口; 可以继承Thread类。...
  • singit
  • singit
  • 2017年04月17日 21:21
  • 284
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:linux上创建线程的类
举报原因:
原因补充:

(最多只允许输入30个字)