关于使用 CreateThread 建立线程池
2006-11-03 17:26
861 查看
下面是关于createthread在cb6中的帮助:
The CreateThread function creates a thread to execute within the address space of the calling process.
HANDLE CreateThread(
LPSECURITY_ATTRIBUTES lpThreadAttributes, / / pointer to thread security attributes
DWORD dwStackSize, // initial thread stack size, in bytes
LPTHREAD_START_ROUTINE lpStartAddress, // pointer to thread function
LPVOID lpParameter, // argument for new thread
DWORD dwCreationFlags, // creation flags
LPDWORD lpThreadId // pointer to returned thread identifier
);
Parameters
lpThreadAttributes
Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpThreadAttributes is NULL, the handle cannot be inherited.
Windows NT: The lpSecurityDescriptor member of the structure specifies a security descriptor for the new thread. If lpThreadAttributes is NULL, the thread gets a default security descriptor.
Windows 95: The lpSecurityDescriptor member of the structure is ignored.
dwStackSize
Specifies the size, in bytes, of the stack for the new thread. If 0 is specified, the stack size defaults to the same size as that of the primary thread of the process. The stack is allocated automatically in the memory space of the process and it is freed when the thread terminates. Note that the stack size grows, if necessary.
CreateThread tries to commit the number of bytes specified by dwStackSize, and fails if the size exceeds available memory.
lpStartAddress
The starting address of the new thread. This is typically the address of a function declared with the WINAPI calling convention that accepts a single 32-bit pointer as an argument and returns a 32-bit exit code. Its prototype is:
DWORD WINAPI ThreadFunc( LPVOID );
lpParameter
Specifies a single 32-bit parameter value passed to the thread.
dwCreationFlags
Specifies additional flags that control the creation of the thread. If the CREATE_SUSPENDED flag is specified, the thread is created in a suspended state, and will not run until the ResumeThread function is called. If this value is zero, the thread runs immediately after creation. At this time, no other values are supported.
lpThreadId
Points to a 32-bit variable that receives the thread identifier.
Return Values
If the function succeeds, the return value is a handle to the new thread.
If the function fails, the return value is NULL. To get extended error information, call GetLastError.
Remarks
The new thread handle is created with full access to the new thread. If a security descriptor is not provided, the handle can be used in any function that requires a thread object handle. When a security descriptor is provided, an access check is performed on all subsequent uses of the handle before access is granted. If the access check denies access, the requesting process cannot use the handle to gain access to the thread.
The thread execution begins at the function specified by the lpStartAddress parameter. If this function returns, the DWORD return value is used to terminate the thread in an implicit call to the ExitThread function. Use the GetExitCodeThread function to get the thread's return value.
The CreateThread function may succeed even if lpStartAddress points to data, code, or is not accessible. If the start address is invalid when the thread runs, an exception occurs, and the thread terminates. Thread termination due to a invalid start address is handled as an error exit for the thread's process. This behavior is similar to the asynchronous nature of CreateProcess, where the process is created even if it refers to invalid or missing dynamic-link libraries (DLLs).
The thread is created with a thread priority of THREAD_PRIORITY_NORMAL. Use the GetThreadPriority and SetThreadPriority functions to get and set the priority value of a thread.
When a thread terminates, the thread object attains a signaled state, satisfying any threads that were waiting on the object.
The thread object remains in the system until the thread has terminated and all handles to it have been closed through a call to CloseHandle.
The ExitProcess, ExitThread, CreateThread, CreateRemoteThread functions, and a process that is starting (as the result of a call by CreateProcess) are serialized between each other within a process. Only one of these events can happen in an address space at a time. This means that the following restrictions hold:
?During process startup and DLL initialization routines, new threads can be created, but they do not begin execution until DLL initialization is done for the process.
?Only one thread in a process can be in a DLL initialization or detach routine at a time.
?ExitProcess does not return until no threads are in their DLL initialization or detach routines.
A thread that uses functions from the C run-time libraries should use the beginthread and endthread C run-time functions for thread management rather than CreateThread and ExitThread. Failure to do so results in small memory leaks when ExitThread is called.
在我同事搞嵌入式的讨论中得到对线程池的应用
下面是使用单一挂起线程的代码
#include <stdio.h>
#include "windows.h"
#pragma hdrstop
#define RECV_THREAD_LPSECURITY (LPSECURITY_ATTRIBUTES)0 // 安全属性
#define RECV_THREAD_STACK_SIZE 64 * 1024 // 堆栈大小
#define RECV_THREAD_HANDLE (LPTHREAD_START_ROUTINE)thread_recv // 线程代码
#define RECV_THREAD_PARAM (LPVOID)0
#define RECV_THREAD_FLAG (DWORD)0
DWORD RecvThreadId; // ID
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int sk; // socket
char buf[100]; // recv_buf
int recv_num; // recv_num
struct sockaddr_in local_addr; // local_addr
struct sockaddr_in remote_addr; // remote_addr
int addr_in_size; // sizeof(struct sockaddr_in)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void thread_recv (void *arg);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma argsused
int main(int argc, char* argv[])
{
/* 建立线程 */
CreateThread( RECV_THREAD_LPSECURITY,
RECV_THREAD_STACK_SIZE,
RECV_THREAD_HANDLE,
RECV_THREAD_PARAM,
RECV_THREAD_FLAG,
&RecvThreadId0
);
while (1) {
Sleep(100);
}
return 1;
}
//---------------------------------------------------------------------------
void thread_recv (void *arg)
{
while (1) {
printf("aaaa/n");
Sleep(1000);
}
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//在和同事们的讨论中讨论到线程池问题,线程池就是把相同的代码映射到不同的线程执行。 代码如下
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#include <stdio.h>
#include "windows.h"
#pragma hdrstop
#define RECV_THREAD_LPSECURITY (LPSECURITY_ATTRIBUTES)0 // 安全属性
#define RECV_THREAD_STACK_SIZE 64 * 1024 // 堆栈大小
#define RECV_THREAD_HANDLE (LPTHREAD_START_ROUTINE)thread_recv // 线程代码
#define RECV_THREAD_PARAM (LPVOID)0
#define RECV_THREAD_FLAG (DWORD)0
DWORD RecvThreadId0; // ID
DWORD RecvThreadId1; // ID
DWORD RecvThreadId2; // ID
DWORD RecvThreadId3; // ID
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int sk; // socket
char buf[100]; // recv_buf
int recv_num; // recv_num
struct sockaddr_in local_addr; // local_addr
struct sockaddr_in remote_addr; // remote_addr
int addr_in_size; // sizeof(struct sockaddr_in)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void thread_recv (void *arg);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma argsused
int main(int argc, char* argv[])
{
/* 建立线程池 */
CreateThread( RECV_THREAD_LPSECURITY,
RECV_THREAD_STACK_SIZE,
RECV_THREAD_HANDLE,
RECV_THREAD_PARAM,
RECV_THREAD_FLAG,
&RecvThreadId0
);
CreateThread( RECV_THREAD_LPSECURITY,
RECV_THREAD_STACK_SIZE,
RECV_THREAD_HANDLE,
RECV_THREAD_PARAM,
RECV_THREAD_FLAG,
&RecvThreadId1
);
CreateThread( RECV_THREAD_LPSECURITY,
RECV_THREAD_STACK_SIZE,
RECV_THREAD_HANDLE,
RECV_THREAD_PARAM,
RECV_THREAD_FLAG,
&RecvThreadId2
);
CreateThread( RECV_THREAD_LPSECURITY,
RECV_THREAD_STACK_SIZE,
RECV_THREAD_HANDLE,
RECV_THREAD_PARAM,
RECV_THREAD_FLAG,
&RecvThreadId3
);
while (1) {
Sleep(100);
}
return 1;
}
//---------------------------------------------------------------------------
void thread_recv (void *arg)
{
while (1) {
printf("aaaa/n");
Sleep(1000);
}
}
代码比较简单,可以通过线程的id控制线程的挂起和再启动。同样的线程池也可以比较方便使用。这篇作为一个抛砖引玉的记录!
The CreateThread function creates a thread to execute within the address space of the calling process.
HANDLE CreateThread(
LPSECURITY_ATTRIBUTES lpThreadAttributes, / / pointer to thread security attributes
DWORD dwStackSize, // initial thread stack size, in bytes
LPTHREAD_START_ROUTINE lpStartAddress, // pointer to thread function
LPVOID lpParameter, // argument for new thread
DWORD dwCreationFlags, // creation flags
LPDWORD lpThreadId // pointer to returned thread identifier
);
Parameters
lpThreadAttributes
Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpThreadAttributes is NULL, the handle cannot be inherited.
Windows NT: The lpSecurityDescriptor member of the structure specifies a security descriptor for the new thread. If lpThreadAttributes is NULL, the thread gets a default security descriptor.
Windows 95: The lpSecurityDescriptor member of the structure is ignored.
dwStackSize
Specifies the size, in bytes, of the stack for the new thread. If 0 is specified, the stack size defaults to the same size as that of the primary thread of the process. The stack is allocated automatically in the memory space of the process and it is freed when the thread terminates. Note that the stack size grows, if necessary.
CreateThread tries to commit the number of bytes specified by dwStackSize, and fails if the size exceeds available memory.
lpStartAddress
The starting address of the new thread. This is typically the address of a function declared with the WINAPI calling convention that accepts a single 32-bit pointer as an argument and returns a 32-bit exit code. Its prototype is:
DWORD WINAPI ThreadFunc( LPVOID );
lpParameter
Specifies a single 32-bit parameter value passed to the thread.
dwCreationFlags
Specifies additional flags that control the creation of the thread. If the CREATE_SUSPENDED flag is specified, the thread is created in a suspended state, and will not run until the ResumeThread function is called. If this value is zero, the thread runs immediately after creation. At this time, no other values are supported.
lpThreadId
Points to a 32-bit variable that receives the thread identifier.
Return Values
If the function succeeds, the return value is a handle to the new thread.
If the function fails, the return value is NULL. To get extended error information, call GetLastError.
Remarks
The new thread handle is created with full access to the new thread. If a security descriptor is not provided, the handle can be used in any function that requires a thread object handle. When a security descriptor is provided, an access check is performed on all subsequent uses of the handle before access is granted. If the access check denies access, the requesting process cannot use the handle to gain access to the thread.
The thread execution begins at the function specified by the lpStartAddress parameter. If this function returns, the DWORD return value is used to terminate the thread in an implicit call to the ExitThread function. Use the GetExitCodeThread function to get the thread's return value.
The CreateThread function may succeed even if lpStartAddress points to data, code, or is not accessible. If the start address is invalid when the thread runs, an exception occurs, and the thread terminates. Thread termination due to a invalid start address is handled as an error exit for the thread's process. This behavior is similar to the asynchronous nature of CreateProcess, where the process is created even if it refers to invalid or missing dynamic-link libraries (DLLs).
The thread is created with a thread priority of THREAD_PRIORITY_NORMAL. Use the GetThreadPriority and SetThreadPriority functions to get and set the priority value of a thread.
When a thread terminates, the thread object attains a signaled state, satisfying any threads that were waiting on the object.
The thread object remains in the system until the thread has terminated and all handles to it have been closed through a call to CloseHandle.
The ExitProcess, ExitThread, CreateThread, CreateRemoteThread functions, and a process that is starting (as the result of a call by CreateProcess) are serialized between each other within a process. Only one of these events can happen in an address space at a time. This means that the following restrictions hold:
?During process startup and DLL initialization routines, new threads can be created, but they do not begin execution until DLL initialization is done for the process.
?Only one thread in a process can be in a DLL initialization or detach routine at a time.
?ExitProcess does not return until no threads are in their DLL initialization or detach routines.
A thread that uses functions from the C run-time libraries should use the beginthread and endthread C run-time functions for thread management rather than CreateThread and ExitThread. Failure to do so results in small memory leaks when ExitThread is called.
在我同事搞嵌入式的讨论中得到对线程池的应用
下面是使用单一挂起线程的代码
#include <stdio.h>
#include "windows.h"
#pragma hdrstop
#define RECV_THREAD_LPSECURITY (LPSECURITY_ATTRIBUTES)0 // 安全属性
#define RECV_THREAD_STACK_SIZE 64 * 1024 // 堆栈大小
#define RECV_THREAD_HANDLE (LPTHREAD_START_ROUTINE)thread_recv // 线程代码
#define RECV_THREAD_PARAM (LPVOID)0
#define RECV_THREAD_FLAG (DWORD)0
DWORD RecvThreadId; // ID
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int sk; // socket
char buf[100]; // recv_buf
int recv_num; // recv_num
struct sockaddr_in local_addr; // local_addr
struct sockaddr_in remote_addr; // remote_addr
int addr_in_size; // sizeof(struct sockaddr_in)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void thread_recv (void *arg);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma argsused
int main(int argc, char* argv[])
{
/* 建立线程 */
CreateThread( RECV_THREAD_LPSECURITY,
RECV_THREAD_STACK_SIZE,
RECV_THREAD_HANDLE,
RECV_THREAD_PARAM,
RECV_THREAD_FLAG,
&RecvThreadId0
);
while (1) {
Sleep(100);
}
return 1;
}
//---------------------------------------------------------------------------
void thread_recv (void *arg)
{
while (1) {
printf("aaaa/n");
Sleep(1000);
}
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//在和同事们的讨论中讨论到线程池问题,线程池就是把相同的代码映射到不同的线程执行。 代码如下
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
#include <stdio.h>
#include "windows.h"
#pragma hdrstop
#define RECV_THREAD_LPSECURITY (LPSECURITY_ATTRIBUTES)0 // 安全属性
#define RECV_THREAD_STACK_SIZE 64 * 1024 // 堆栈大小
#define RECV_THREAD_HANDLE (LPTHREAD_START_ROUTINE)thread_recv // 线程代码
#define RECV_THREAD_PARAM (LPVOID)0
#define RECV_THREAD_FLAG (DWORD)0
DWORD RecvThreadId0; // ID
DWORD RecvThreadId1; // ID
DWORD RecvThreadId2; // ID
DWORD RecvThreadId3; // ID
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int sk; // socket
char buf[100]; // recv_buf
int recv_num; // recv_num
struct sockaddr_in local_addr; // local_addr
struct sockaddr_in remote_addr; // remote_addr
int addr_in_size; // sizeof(struct sockaddr_in)
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void thread_recv (void *arg);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma argsused
int main(int argc, char* argv[])
{
/* 建立线程池 */
CreateThread( RECV_THREAD_LPSECURITY,
RECV_THREAD_STACK_SIZE,
RECV_THREAD_HANDLE,
RECV_THREAD_PARAM,
RECV_THREAD_FLAG,
&RecvThreadId0
);
CreateThread( RECV_THREAD_LPSECURITY,
RECV_THREAD_STACK_SIZE,
RECV_THREAD_HANDLE,
RECV_THREAD_PARAM,
RECV_THREAD_FLAG,
&RecvThreadId1
);
CreateThread( RECV_THREAD_LPSECURITY,
RECV_THREAD_STACK_SIZE,
RECV_THREAD_HANDLE,
RECV_THREAD_PARAM,
RECV_THREAD_FLAG,
&RecvThreadId2
);
CreateThread( RECV_THREAD_LPSECURITY,
RECV_THREAD_STACK_SIZE,
RECV_THREAD_HANDLE,
RECV_THREAD_PARAM,
RECV_THREAD_FLAG,
&RecvThreadId3
);
while (1) {
Sleep(100);
}
return 1;
}
//---------------------------------------------------------------------------
void thread_recv (void *arg)
{
while (1) {
printf("aaaa/n");
Sleep(1000);
}
}
代码比较简单,可以通过线程的id控制线程的挂起和再启动。同样的线程池也可以比较方便使用。这篇作为一个抛砖引玉的记录!
相关文章推荐
- 关于使用xml技术建立网站。
- 关于泛型队列的简单建立与使用
- Java关于线程池的使用
- 关于使用ArcMap10.0为几何网络建立流向
- 关于C#线程,线程池和并行运算的简单使用和对比
- 关于线程池ThreadPoolExecutor使用总结
- 关于使用Tomcat 建立自己JSP app目录时常出现的问题
- Android关于线程池的含义和使用
- 关于使用Xcode--Editor相关选项建立autoLayout的约束的使用介绍
- 关于C#线程,线程池和并行运算的简单使用和对比
- 关于SqlServer2014驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。问题
- 关于项目中线程池的使用
- 关于epoll服务器示例: 监听5000个端口, 使用线程池
- 关于使用DrawerLayout建立侧滑菜单时,下层布局依然可响应点击事件的解决方法
- 关于使用MySQL的ODBC驱动建立数据源出错的问题
- 关于使用Visual Studio 2008 建立Setup And Deployment Project(安装与部署项目)制作安装.net 安装程序,插入Custom Actions(自定义动作)的办法
- 关于weka的使用————模型的建立
- 一起谈.NET技术,关于C#线程,线程池和并行运算的简单使用和对比
- 关于使用navicat for mysql在建立数据库连接时出现的2003 can't connect to mysql server on'localhost'(10038)问题
- Junit 使用中,关于SessionFactory的建立