thread API:实现线程类
来源:互联网 发布:全途进销存打单软件 编辑:程序博客网 时间:2024/06/06 06:42
- //定义WIN32和Linux下通用名称
- typedef void wthread;
- //强制退出线程
- int kill_thread(wthread *pthread)
- {
- #ifdef WIN32
- return(TerminateThread(pthread, 1));
- #else
- return(pthread_cancel(*(pthread_t *)pthread));
- #endif
- }
- //ExitThread是推荐使用的结束一个线程的方法,当调用该函数时,当前线程的栈被释放,然后线程终止
- void exit_thread(void)
- {
- #ifdef WIN32
- ExitThread(0);
- #else
- pthread_exit(0);
- #endif
- }
- //安全关闭THREAD,推荐使用
- //如果还有其他的进程在使用这个内核对象,那么它就不会被释放
- void free_thread(wthread *pthread)
- {
- #ifdef WIN32
- CloseHandle(pthread);
- #else
- delete (pthread_t *)pthread;
- #endif
- }
- //等待THREAD
- void wait_thread(wthread *pthread)
- {
- #ifdef WIN32
- WaitForSingleObject(pthread, INFINITE);
- #else
- pthread_join(*(pthread_t *)pthread, NULL);
- #endif
- }
- //创建线程
- wthread *create_thread(void *(thread_fn)(void *), void *thread_arg)
- {
- int rv;
- #ifdef WIN32
- int id;
- rv =_beginthreadex( NULL, 0, (unsigned int (__stdcall *)(void *))thread_fn, thread_arg, 0, (unsigned int *)&id);
- return((wthread *)rv);
- #else
- pthread_t *hThread;
- hThread = new pthread_t;
- if(hThread == NULL) {
- return((wthread *)NULL);
- }
- rv = pthread_create(hThread, NULL, (void *(*)(void *))thread_fn, thread_arg);
- if(rv != 0) {
- /* thread creation failure */
- return((wthread *)NULL);
- }
- return((wthread *)hThread);
- #endif
- }
- //获得当前THREAD 的ID
- long get_threadid(void)
- {
- #ifdef WIN32
- return GetCurrentThreadId();
- #else
- #ifdef LINUX
- unsigned long thr_id = pthread_self();
- thr_id = thr_id << 1;
- thr_id = thr_id >> 1;
- return thr_id;
- #else
- return pthread_self();
- #endif
- #endif
- }
0 0
- thread API:实现线程类
- 线程(继承Thread类实现)
- 继承Thread类的线程实现方法
- 线程的随机性之实现Thread类
- 继承Thread 实现线程
- 线程类Thread的API接口分析系列之interrupt()
- 线程类Thread的API接口分析系列之Condition
- 线程类Thread的API接口分析系列之守护线程setDaemon
- 线程类Thread的API接口分析系列之线程组ThreadGroup
- 读解Thread类API
- Runnable Thread 线程的实现
- 线程Thread与Runnable实现
- Thread线程类
- Thread线程类
- Thread线程类
- Thread线程类
- Thread线程类
- 【c++】线程thread类
- C++ 类的静态成员(static)
- C++变量的初始化方式
- C/C++ 创建线程解析
- _beginthreadex创建线程
- NYOJ 77 开灯问题
- thread API:实现线程类
- WaitForSingleObject 的用法
- 请教GetExitCodeThread函数的作用及参数
- vs2005动态链接库的隐式调用
- snmp++获取MIB表格信息
- 在细节上提升你的程序的性能
- 做一个事情
- deque 双向排序
- 线程中CreateEvent和SetEvent及WaitForSingleObject的用法