神一样的使用方法,错误还是漏洞,pthread线程库锁的使用问题,跪求高手解答!
本帖最后由 lx111000lx0 于 2013-02-25 18:59:06 编辑 据说这段使用pthread线程库锁的代码有问题,求解析!
这段使用pthread线程库锁的方法并不是标准方法,但是可以正常运行,
求高手指出这样使用的问题,谢谢!!
/*
* wolegequ.cpp
*
* Created on: 2013-1-20
* Author: lj
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstdio>
#include <iostream>
#include <fstream>
#include <conio.h>
#include "pthread.h"
using namespace std;
#include <time.h>
clock_t start_time, finish_time;
double duration;
enum Statue
{
st_null,
st_start,
st_pause,
st_end
};
typedef struct str_thdata
{
pthread_mutex_t lock;
Statue bar;
int index;
} thdata;
void* PrintMessageFunction ( void* ptr )
{
thdata* data;
data = (thdata*) ptr;
int i = data->index;
for(; data->bar != st_end; i++)
{
printf("index: %d \n", i);
Sleep(1000);
//printf("statu: %d \n", data->bar);
if(data->bar == st_pause)
{
pthread_mutex_lock(&(data->lock));
}
}
data->bar = st_null;
pthread_exit(0);
return NULL;
}
void* ControlPrintFunction ( void* ptr )
{
thdata* data;
data = (thdata*) ptr;
char ch;
while(true)
{
ch = getch();
if(ch == 's' && data->bar == st_null)
{
data->bar = st_start;
printf("start\n");
pthread_t thread1;
pthread_create(&thread1, NULL, PrintMessageFunction, ptr);
pthread_detach(thread1);
ch = ' ';
}
else if(ch == 'c')
{
if(data->bar == st_start)
{
data->bar = st_pause;
printf("pause\n");
ch = ' ';
continue;
}
else if(data->bar == st_pause)
{
data->bar = st_start;
printf("recovery\n");
pthread_mutex_unlock(&(data->lock));
ch = ' ';
continue;
}
}
else if(ch == 'e' && data->bar != st_end && data->bar != st_null)
{
if(data->bar == st_pause)
{
pthread_mutex_unlock(&(data->lock));
}
data->bar = st_end;
printf("end\n");
ch = ' ';
continue;
}
else if(ch == 'x')
{
break;
}
}
return NULL;
}
int main()
{
printf("Press the 's' key to start, press 'c' switching key pause and recovery, \npress the 'e' key end,press the 'x' key exit.\n");
thdata data1;
data1.bar = st_null;
data1.index = 0;
pthread_mutex_init(&(data1.lock), NULL);
pthread_mutex_lock(&(data1.lock));
ControlPrintFunction((void*) &data1);
pthread_mutex_destroy(&(data1.lock));
return 0;
}
pthead thread lock 神奇
[解决办法]
帮顶,求解答
[解决办法]
我理解的是pthread_mutex_unlock只能在加锁的线程里自己调用,而不是由其他线程解锁。
高手说说看?
[解决办法]
利用pthread_mutex_lock的死锁来实现程序的暂停功能。
但是第二次按c暂停的时候不会导致print线程的自死锁吗?是不是应该52行添加一行代码
if(data->bar == st_pause)
{
pthread_mutex_lock(&(data->lock));
pthread_mutex_unlock(&(data->lock))
}
[解决办法]
不需要添加,但是一个线程获得锁,另一个线程释放,确实没有问题吗
[解决办法]
仅供参考
#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;
}
