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

软件和网站开发以及相关技术探讨
回复
头像
hellojinjie
帖子: 1150
注册时间: 2007-09-14 21:03
来自: 浙江

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

#1

帖子 hellojinjie » 2008-11-29 20:02

代码: 全选

/**
 * 好像没有想像的那样简单啊
 */
#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()理解不对?
Say hello to everyday!
头像
xhy
帖子: 3916
注册时间: 2005-12-28 1:16
系统: Ubuntu 12.10 X64
来自: 火星

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

#2

帖子 xhy » 2008-11-29 21:40

代码: 全选

#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上的例子,可以参考下
目前负债150多万
头像
hellojinjie
帖子: 1150
注册时间: 2007-09-14 21:03
来自: 浙江

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

#3

帖子 hellojinjie » 2008-11-30 19:29

今天去翻了下这本书,....................很是枯燥
Say hello to everyday!
头像
hellojinjie
帖子: 1150
注册时间: 2007-09-14 21:03
来自: 浙江

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

#4

帖子 hellojinjie » 2008-12-01 22:56

代码: 全选

#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
Say hello to everyday!
头像
xhy
帖子: 3916
注册时间: 2005-12-28 1:16
系统: Ubuntu 12.10 X64
来自: 火星

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

#5

帖子 xhy » 2008-12-02 1:18

代码: 全选

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.
目前负债150多万
头像
shellex
帖子: 2180
注册时间: 2007-02-18 19:33
系统: OSX
来自: lyric.im
联系:

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

#6

帖子 shellex » 2008-12-02 7:32

就知道xhy会进来答
既然你诚心诚意地问了
我就大慈大悲地告诉你
为了防止世界被破坏
为了维护世界的和平
贯彻爱与真实的罪恶
可爱而又迷人的反派角色
武藏,小次郎
我们是穿越银河的火箭队,白洞白色的明天在等着我们。就是这样!!喵~~
头像
hellojinjie
帖子: 1150
注册时间: 2007-09-14 21:03
来自: 浙江

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

#7

帖子 hellojinjie » 2008-12-02 8:51

呵呵,想明白了,thanks xhy

我所谓的想象的结果是错误的,
Say hello to everyday!
回复