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
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
- linux上创建线程的类
- Linux线程的创建
- Linux下线程的创建
- Linux下线程的创建
- Linux下线程的创建
- linux下线程的创建
- linux内核线程的创建
- 浅析Linux线程的创建
- 浅析Linux线程的创建
- linux下线程的创建
- linux下线程的创建
- Linux的线程创建限制
- linux 下线程的创建
- Linux线程(1): 线程的创建
- linux内核线程的创建及在QEMU上的测试方法
- linux内核线程的创建及在QEMU上的测试方法
- linux内核线程的创建及在QEMU上的测试方法
- Linux多线程----直接上代码----线程创建、终止(二)
- 【Android】50、碎片的简单用法
- hdu2256Problem of Precision+矩阵快速幂+广义斐波拉契
- C++实现链表
- hadoop启动错误:Path /XXX should be specified as a URI in configuration file
- 大数据入门,这10点你是必须要知道的
- linux上创建线程的类
- 金蝶打开软件时提示以独占的方式被打开
- block,inline和inline-block概念和区别
- sqlmap用户手册
- 薛开宇caffe学习笔记的补充笔记1
- SQL Server 2008重装系统后还原原来的数据库
- Http协议
- PAT甲级.1077. Kuchiguse (20)
- C++实现顺序表