> > Это в солярке pthread-ы такие - они kernel-но - user > > space-овые > > отсюда и косяки (race condition) - можно попытатся > вылечить > > это дело > > через pthread_cond_timewait() > > правильно я понимаю что можно заменить usleep на > > { > pthread_mutex_t m_Lock; > pthread_cond_t m_Cond; > > pthread_mutex_init(&m_Lock, NULL); > pthread_cond_init(&m_Cond, NULL); > > .... > pthread_cond_timedwait(&m_Cond, &m_Lock, &time); > > pthread_cond_destroy(&m_Cond); > pthread_mutex_destroy(&m_Lock); > } > > или имеется в виду что-то другое?
там чуть иначе - вот пример
#include <stdio.h>
#include <pthread.h>
#include <sys/time.h>
void pthread_sleep(long int time, long int ntime) {
pthread_mutex_t mtx=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
struct timeval now;
struct timezone tz;
struct timespec timeout;
int retcode;
pthread_mutex_lock(&mtx);
gettimeofday(&now,&tz);
timeout.tv_sec = now.tv_sec + time;
timeout.tv_nsec = now.tv_usec + ntime;
retcode = 0;
while ( retcode != 110) {
retcode = pthread_cond_timedwait(&cond, &mtx, &timeout);
}
pthread_mutex_unlock(&mtx);
pthread_cond_destroy(&cond);
pthread_mutex_destroy(&mtx);
};
void* _routine(void* ptr)
{
printf("wait 1 sec\n");
pthread_sleep(1,0);
};
int main(int argc, char** argv)
{
pthread_t tid;
int i;
for(i=1;i<100;i++)
{
pthread_create(&tid,NULL,_routine,NULL);
pthread_join(tid,NULL);
}
return 0;
};
---
|