多线程问题

superbtl 2013-02-04 12:32:50
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
pthread_mutex_t mymutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mymutex2 = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t mycond = PTHREAD_COND_INITIALIZER;
void *mythread1(void *param)
{
printf("begin mythread1.\n");
pthread_mutex_lock(&mymutex1);
printf("wait in mythread1.\n");
pthread_cond_wait(&mycond,&mymutex1);
pthread_mutex_unlock(&mymutex1);
printf("end mythread1.\n");
return NULL;
}
void *mythread2(void *param)
{
printf("begin mythread2.\n");
pthread_mutex_lock(&mymutex2);
printf("wait in mythread2.\n");
pthread_cond_wait(&mycond,&mymutex2);
pthread_mutex_unlock(&mymutex2);
printf("end mythread2.\n");
return NULL;
}
int main(void)
{
printf("begin main thread.\n");
int i;
pthread_t tid1,tid2;
pthread_create(&tid1,NULL,mythread1,NULL);
pthread_create(&tid2,NULL,mythread2,NULL);
sleep(5);
printf("try to wake up mythread1 and mythread2 in main thread.\n");
if(pthread_cond_broadcast(&mycond)){
printf("error\n");
return 1;
}
void *res;
pthread_join(tid1, &res);
pthread_join(tid2, &res);
printf("end main thread.\n");
return 0;
}

我在机器上运行,只把tid1唤醒了,各位遇到过吗?如何解决
...全文
202 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
yong_f 2013-02-05
  • 打赏
  • 举报
回复
在我的机器上都唤醒了, begin main thread. begin ythread1. wait in mythread1. begin ythread2. wait in mythread2. try to wake up mythread1 and mythread2 in main thread. end mythread1. end mythread2. end main thread. 是不是你的机器上的多线程库有问题啊
qq120848369 2013-02-04
  • 打赏
  • 举报
回复
The effect of using more than one mutex for concurrent pthread_cond_timedwait() or pthread_cond_wait() operations on the same condition variable is undefined; that is, a condition variable becomes bound to a unique mutex when a thread waits on the condition variable, and this (dynamic) binding shall end when the wait returns. 从没见过一个条件变量关联两把锁的, 楼主意图何在?
qq120848369 2013-02-04
  • 打赏
  • 举报
回复
楼主看不懂英文?
superbtl 2013-02-04
  • 打赏
  • 举报
回复
引用 1 楼 qq120848369 的回复:
The effect of using more than one mutex for concurrent pthread_cond_timedwait() or pthread_cond_wait() operations on the same condition variable is undefined; that is, a co……
线程池,由于特定环境不能用信号量,如果有计数能力的其他锁就好了,现在是没想到办法
赵4老师 2013-02-04
  • 打赏
  • 举报
回复
特供调试多线程程序使用:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
    #include <windows.h>
    #include <io.h>
#else
    #include <unistd.h>
    #include <sys/time.h>
    #include <pthread.h>
    #define  CRITICAL_SECTION   pthread_mutex_t
    #define  _vsnprintf         vsnprintf
#endif
//Log{
#define MAXLOGSIZE 20000000
#define MAXLINSIZE 16000
#include <time.h>
#include <sys/timeb.h>
#include <stdarg.h>
char logfilename1[]="MyLog1.log";
char logfilename2[]="MyLog2.log";
static char logstr[MAXLINSIZE+1];
char datestr[16];
char timestr[16];
char mss[4];
CRITICAL_SECTION cs_log;
FILE *flog;
#ifdef WIN32
void Lock(CRITICAL_SECTION *l) {
    EnterCriticalSection(l);
}
void Unlock(CRITICAL_SECTION *l) {
    LeaveCriticalSection(l);
}
#else
void Lock(CRITICAL_SECTION *l) {
    pthread_mutex_lock(l);
}
void Unlock(CRITICAL_SECTION *l) {
    pthread_mutex_unlock(l);
}
#endif
void LogV(const char *pszFmt,va_list argp) {
    struct tm *now;
    struct timeb tb;

    if (NULL==pszFmt||0==pszFmt[0]) return;
    _vsnprintf(logstr,MAXLINSIZE,pszFmt,argp);
    ftime(&tb);
    now=localtime(&tb.time);
    sprintf(datestr,"%04d-%02d-%02d",now->tm_year+1900,now->tm_mon+1,now->tm_mday);
    sprintf(timestr,"%02d:%02d:%02d",now->tm_hour     ,now->tm_min  ,now->tm_sec );
    sprintf(mss,"%03d",tb.millitm);
    printf("%s %s.%s %s",datestr,timestr,mss,logstr);
    flog=fopen(logfilename1,"a");
    if (NULL!=flog) {
        fprintf(flog,"%s %s.%s %s",datestr,timestr,mss,logstr);
        if (ftell(flog)>MAXLOGSIZE) {
            fclose(flog);
            if (rename(logfilename1,logfilename2)) {
                remove(logfilename2);
                rename(logfilename1,logfilename2);
            }
        } else {
            fclose(flog);
        }
    }
}
void Log(const char *pszFmt,...) {
    va_list argp;

    Lock(&cs_log);
    va_start(argp,pszFmt);
    LogV(pszFmt,argp);
    va_end(argp);
    Unlock(&cs_log);
}
//Log}
int main(int argc,char * argv[]) {
    int i;
#ifdef WIN32
    InitializeCriticalSection(&cs_log);
#else
    pthread_mutex_init(&cs_log,NULL);
#endif
    for (i=0;i<10000;i++) {
        Log("This is a Log %04d from FILE:%s LINE:%d\n",i, __FILE__, __LINE__);
    }
#ifdef WIN32
    DeleteCriticalSection(&cs_log);
#else
    pthread_mutex_destroy(&cs_log);
#endif
    return 0;
}
morris88 2013-02-04
  • 打赏
  • 举报
回复
一般来说,cond基本上是通过mutex来实现的

23,217

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧