有Event  Semaphore  Mutex
结合Wait函数的使用

解决方案 »

  1.   

    不好意思搞错了Unix下的线程同步不是很清楚  建立线程我到知道是fork
      

  2.   

    UNIX不是任何系统都支持线程的
      

  3.   

    我到那里能找到这样的例子,贴上来好吗!?!多谢!
    不用API MFC库 
    有什么好的办法吗!??? 
      

  4.   

    windows下的有mutex,semaphore,event,criticalsection,timer
    建议你看看richard steves的unix网络编程,我记得讲同步再第二卷上
      

  5.   

    UNIX下你可以使用互斥锁(mutex):
    #include<pthread.h>
    int pthread_mutex_lock(pthread_mutex_t *mptr);
    int pthread_mutex_unlock(pthread_mutex_t *mptr);定义一个全局变量:
    pthread_mutex_t nMutex=PTHREAD_MUTEX_INITIALIZER //不同的系统可以有不同的初值在线程函数中:
    pthread_mutex_lock(&nMutex);
    //处理数据
    pthread_mutex_unlock(&nMutex);
      

  6.   

    给你一个例子:
    #include <pthread.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <stdio.h>int myglobal;
    pthread_mutex_t mymutex=PTHREAD_MUTEX_INITIALIZER; void *thread_function(void *arg) {
      int i,j;
      for ( i=0; i<20; i++) {
        pthread_mutex_lock(&mymutex);
        j=myglobal;
        j=j+1;
        printf(".");
        fflush(stdout);
        sleep(1);
        myglobal=j;
        pthread_mutex_unlock(&mymutex);
      }
      return NULL;
    }int main(void) {  pthread_t mythread;
      int i;  if ( pthread_create( &mythread, NULL, thread_function, NULL) ) {
        printf("error creating thread.");
        abort();
      }  for ( i=0; i<20; i++) {
        pthread_mutex_lock(&mymutex);
        myglobal=myglobal+1;
        pthread_mutex_unlock(&mymutex);
        printf("o");
        fflush(stdout);
        sleep(1);
      }  if ( pthread_join ( mythread, NULL ) ) {
        printf("error joining thread.");
        abort();
      }  printf("\nmyglobal equals %d\n",myglobal);  exit(0);}