天天看點

Linux線程通信--信号量(例子)

#include <stdio.h>

#include <stdlib.h>

#include <semaphore.h>

#include <pthread.h>

sem_t smt;

void DisplayData()

{

printf ("Display a frame/n");

}

void *ThreadFuncDisplay(void *arg)

{

while (1){

printf ("sem_wait............................../n");

sem_wait (&smt);

DisplayData();

}

}

void *ThreadFuncControl(void *arg)

{

while (1){

sleep (3);

sem_post (&smt);

printf ("sem_post/n");

}

}

int main(int argc, char *argv[])

{

gtk_init (&argc, &argv);

int rt;

pthread_t pt_dsp;

pthread_t pt_ctrl;

rt = sem_init (&smt, 0, 0);

if (rt){

perror ("semaphore initialization failed");

}

pthread_create (&pt_dsp, NULL, ThreadFuncDisplay, NULL);

pthread_create (&pt_ctrl, NULL, ThreadFuncControl, NULL);

gtk_main();

return 0;

}

繼續閱讀