值得剖析的pthread_create测试线程

/*
 * pthread.c:
 * Tiny test program to see whether POSIX threads work.
 */

static const char rcsid[] = "$Id: pthread.c,v 1.4 2005/10/26 22:56:05 chris Exp $";

#include <sys/types.h>

#include <errno.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
static int return_value = -1;

void *worker_thread(void *v) {
    /* Record successful return and signal parent to wake up. */
    return_value = 0;
    pthread_mutex_lock(&mtx);
    pthread_cond_signal(&cond);
    pthread_mutex_unlock(&mtx);
    while (1) {
        sleep(1);
        pthread_testcancel();
    }
}

/* Start a thread, and have it set a variable to some other value, then signal
 * a condition variable. If this doesn‘t happen within some set time, we assume
 * that something‘s gone badly wrong and abort (for instance, the thread never
 * got started). */
int main(void) {
    pthread_t thr;
    int res;
    struct timespec deadline = {0};
    if ((res = pthread_mutex_lock(&mtx)) != 0
        || (res = pthread_create(&thr, NULL, worker_thread, NULL)) != 0) {
        fprintf(stderr, "%s\n", strerror(res));
        return -1;
    }

/* Thread should now be running; we should wait on the condition
     * variable. */
    do
        deadline.tv_sec = 2 + time(NULL);
    while ((res = pthread_cond_timedwait(&cond, &mtx, &deadline)) == EINTR);

if (res != 0) {
        fprintf(stderr, "%s\n", strerror(res));
        return -1;
    }

if ((res = pthread_cancel(thr)) != 0
        || (res = pthread_join(thr, NULL)) != 0) {
        fprintf(stderr, "%s\n", strerror(res));
        return -1;
    }

return return_value;
}

值得剖析的pthread_create测试线程,布布扣,bubuko.com

时间: 2024-08-26 00:21:23

值得剖析的pthread_create测试线程的相关文章

在C++的类中,普通成员函数不能作为pthread_create的线程函数,如果要作为pthread_create中的线程函数,必须是static

在C++的类中,普通成员函数不能作为pthread_create的线程函数,如果要作为pthread_create中的线程函数,必须是static ! 在C语言中,我们使用pthread_create创建线程,线程函数是一个全局函数,所以在C++中,创建线程时,也应该使用一个全局函数.static定义的类的成员函数就是一个全局函数. 更多 参考  http://blog.csdn.net/ksn13/article/details/40538083 #include <pthread.h> #

Nodejs事件引擎libuv源码剖析之:高效线程池(threadpool)的实现

声明:本文为原创博文,转载请注明出处. Nodejs编程是全异步的,这就意味着我们不必每次都阻塞等待该次操作的结果,而事件完成(就绪)时会主动回调通知我们.在网络编程中,一般都是基于Reactor线程模型的变种,无论其怎么演化,其核心组件都包含了Reactor实例(提供事件注册.注销.通知功能).多路复用器(由操作系统提供,比如kqueue.select.epoll等).事件处理器(负责事件的处理)以及事件源(linux中这就是描述符)这四个组件.一般,会单独启动一个线程运行Reactor实例来

pthread_create()创建线程时传入多个参数

由于接口只定义了一个入参void *arg int pthread_create(pthread_t *tidp,const pthread_attr_t *attr, (void*)(*start_rtn)(void*),void *arg); 所以,如果想传参数,需要封装结构体,将多个参数通过一个结构体传入线程. typedef struct { FUNCPTR entry; /* 函数入口*/ void *arg[10]; /* 参数*/ }FUNC; void *start(void *

pthread_create()创建线程时传入多个參数

因为接口仅仅定义了一个入參void *arg int pthread_create(pthread_t *tidp,const pthread_attr_t *attr, (void*)(*start_rtn)(void*),void *arg); 所以,假设想传參数,须要封装结构体.将多个參数通过一个结构体传入线程. typedef struct { FUNCPTR entry; /* 函数入口*/ void *arg[10]; /* 參数*/ }FUNC; void *start(void

【java】使用Junit测试线程过程中出现的小问题

本文内容介绍在使用Junit进行线程测试的时候出现的一个小问题,自己简单做一个记录,以便后续查看 在使用java编写多线程并发实验程序时在Juint写了测试程序,但并没有得到预想的效果. 直接上代码 线程类: package sm.examples.threaddemo; import org.apache.log4j.Logger; public class Thread1 implements Runnable { private static final Logger logger = L

Python测试线程应用程序

在本章中,我们将学习线程应用程序的测试.我们还将了解测试的重要性. 为什么要测试? 在我们深入讨论测试的重要性之前,我们需要知道测试的内容.一般来说,测试是一种了解某些东西是如何运作的技术.另一方面,特别是如果我们谈论计算机程序或软件,那么测试就是访问软件程序功能的技术. 在本节中,我们将讨论软件测试的重要性.在软件开发中,必须在向客户端发布软件之前进行双重检查.这就是由经验丰富的测试团队测试软件非常重要的原因.请考虑以下几点来理解软件测试的重要性 提高软件质量 当然,没有公司想要提供低质量的软

分析线程池源码测试线程池

import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; /** * 线程池测试类 */ public class TestThreadPool { public static void main(String[] args) { // 实例化线程池对象 corePoolSize--线程池

Delphi测试线程的时间

在16位时代,当我们在Windows3.x下编程时,经常会用到GetTickCount()或者timeGetTime()来判断一段代码的执行时间.示例如下 var StartTime, Total: Longint; begin StartTime:= GetTickCount; {进行一些操作} Total:= GetTickCount - StartTime; end; 在多线程环境下,这是很困难的,因为在执行程序中间,操作系统可能会把CPU时间片分给别的进程.所以,用上述方法测出的时间并不

pthread_create()并发线程最大个数

并发线程最大个数的最大个数取决于两个因素:栈的大小和用户空间大小 32位系统中,用户空间地址范围为0x00000000 ~ 0xC0000000 大小为3GB 64位系统中,用户空间地址范围为0x00000000 00000000 ~ 0x00007FFF FFFFFFFF 大小为128TB 栈的大小可以通过ulimit -s 查看或修改 一般,最大个数 ≈ 用户空间大小/栈的大小. 原文地址:https://www.cnblogs.com/wanfeng-42/p/9390962.html