分页: 1 / 1

我也在学多线程,pthread_cond_timedwait()有点不懂

发表于 : 2008-11-29 20:02
hellojinjie

代码: 全选

/**
 * 好像没有想像的那样简单啊
 */
#include <stdio.h>
#include <pthread.h>
#include <time.h>

void my_thread_func_1();
struct timespec waitcount;
pthread_cond_t cond_t;
pthread_mutex_t mutex_t;

int main(void)
{
	int i = 5;
	pthread_t pid;
	void *retval;

	waitcount.tv_sec = 10;
	waitcount.tv_nsec = 0;
	
	// 初始化线程相关的一些变量
	pthread_cond_init(&cond_t, NULL);
	pthread_mutex_init(&mutex_t, NULL);

	while (i > 0)
	{
		pthread_create(&pid, NULL, (void *)&my_thread_func_1, NULL);
		i--;
	}
	// pthread_join(pid, &retval);
}

void my_thread_func_1()
{
	pthread_mutex_lock(&mutex_t);
	// 把锁释放掉,然后1秒种后重新去竞争锁
	pthread_cond_timedwait(&cond_t, &mutex_t, &waitcount);
	printf("a\n");
	pthread_mutex_unlock(&mutex_t); 
}
我想要得到的是10秒钟后再打印5个a
编译后,却是立即打印5个a

我对pthread_cond_timedwait()理解不对?

Re: 我也在学多线程,pthread_cond_timedwait()有点不懂

发表于 : 2008-11-29 21:40
xhy

代码: 全选

#include <pthread.h>

struct msg {
    struct msg *m_next;
    /* ... more stuff here ... */
};
struct msg *workq;
pthread_cond_t qready = PTHREAD_COND_INITIALIZER;
pthread_mutex_t qlock = PTHREAD_MUTEX_INITIALIZER;

void
process_msg(void)
{
    struct msg *mp;

    for (;;) {
        pthread_mutex_lock(&qlock);
        while (workq == NULL)
            pthread_cond_wait(&qready, &qlock);
        mp = workq;
        workq = mp->m_next;
        pthread_mutex_unlock(&qlock);
        /* now process the message mp */
    }
}

void
enqueue_msg(struct msg *mp)
{
    pthread_mutex_lock(&qlock);
    mp->m_next = workq;
    workq = mp;
    pthread_mutex_unlock(&qlock);
    pthread_cond_signal(&qready);
}

apue上的例子,可以参考下

Re: 我也在学多线程,pthread_cond_timedwait()有点不懂

发表于 : 2008-11-30 19:29
hellojinjie
今天去翻了下这本书,....................很是枯燥

Re: 我也在学多线程,pthread_cond_timedwait()有点不懂

发表于 : 2008-12-01 22:56
hellojinjie

代码: 全选

#include <stdio.h>
#include <pthread.h>
#include <sys/time.h>
#include <time.h>

void my_thread_func_1();
struct timespec waitcount;
pthread_cond_t cond_t;
pthread_mutex_t mutex_t;

int main(void)
{
	int i = 5;
	pthread_t pid;
	void *retval;
	
	
	pthread_cond_init(&cond_t, NULL);
	pthread_mutex_init(&mutex_t, NULL);

	while (i > 0)
	{
		pthread_create(&pid, NULL, (void *)&my_thread_func_1, NULL);
		i--;
	}
	pthread_join(pid, &retval);
	printf("\nexit\n");
}

void my_thread_func_1()
{
	pthread_mutex_lock(&mutex_t);
	clock_gettime(CLOCK_REALTIME, &waitcount);
	printf("%ld\n", (&waitcount)->tv_sec);
	(&waitcount)->tv_sec += 3;
	pthread_cond_timedwait(&cond_t, &mutex_t, &waitcount);
	printf("%ld\n", (&waitcount)->tv_sec);
	pthread_mutex_unlock(&mutex_t); 
}

代码: 全选

jj@hellojinjie:~/mywork/c/linux$ ./pthread_test_timedwait.o1228143386
1228143386
1228143386
1228143386
1228143386
1228143389
1228143389
1228143389
1228143389
1228143389

exit
有半点弄明白了,time用的是绝对时间而不是相对时间,,这点改了

可是输出还不是我想要的啊,,,
pthread_mutex_lock(&mutex_t);好像没有起到作用啊,
我想像的结果应该是这样子的

代码: 全选

1228143386
1228143386
1228143389
1228143386
1228143389
1228143386
1228143389
1228143386
1228143389

Re: 我也在学多线程,pthread_cond_timedwait()有点不懂

发表于 : 2008-12-02 1:18
xhy

代码: 全选

The  pthread_cond_timedwait() and pthread_cond_wait() functions shall block on a condition variable. They shall be called
       with mutex locked by the calling thread or undefined behavior results.

Re: 我也在学多线程,pthread_cond_timedwait()有点不懂

发表于 : 2008-12-02 7:32
shellex
就知道xhy会进来答

Re: 我也在学多线程,pthread_cond_timedwait()有点不懂

发表于 : 2008-12-02 8:51
hellojinjie
呵呵,想明白了,thanks xhy

我所谓的想象的结果是错误的,