how to implement the WaitForMultipleObjects in linux
2012-11-18 20:19
811 查看
原文地址: http://www.linuxquestions.org/questions/programming-9/how-to-implement-waitformultipleobjects-in-linux-908553/
#include <stdlib.h> #include <stdio.h> #include <semaphore.h> #include <pthread.h> #include <unistd.h> #include <errno.h> #include <string.h> #define NUM_THREADS (2) sem_t semaphore; void *worker_thread(void *arg) { int i; for (i=0; i<10; ++i) { fprintf(stderr,"."); sleep(1); } sem_post(&semaphore); return NULL; } int main(int argc, char* argv[]) { pthread_t thread[NUM_THREADS]; int i; sem_init(&semaphore, 0, -1 * NUM_THREADS); for (i =0; i< NUM_THREADS; ++i) { pthread_create(&thread[i],NULL, &worker_thread, NULL); } if (sem_wait(&semaphore) < 0) { printf("sem_wait failed: %s\n", strerror(errno)); return EXIT_FAILURE; } for (i =0; i< NUM_THREADS; ++i) { pthread_join(thread[i], NULL); } printf("\nAll threads are done now\n"); return EXIT_SUCCESS; }
相关文章推荐
- how to implement WaitForMultipleObjects in linux
- Database Performance: How to reduce "library cache: mutex X" waits for scalability in the Oracle dat
- How to change the resolution in text mode for Linux5+
- How to load the specified mscordacwks.dll for managed debugging when multiple .NET runtime are loaded in one process
- How to git clone linux kernel for developer(THE URL)
- How to disable the Media Sensing feature for TCP/IP in Windows
- How to enable the Xdebug debugger in Zend Studio for Eclips
- How to use Wise SetupCapture in the Wise Packager for Deployment Server to create an MSI
- 解决linux下WaitForMultipleObjects的一种方法
- How to use udev for Oracle ASM in Oracle Linux 6
- How To contain multiple fileds in the querystrig, DataNavigateUrlFormatString=xxx.asp?ID={0}&Name={1}
- Linux: How to enable root user in Gnome for VirtualBox Fedora?
- How to install Nola, the free accounting package for Linux @ JDJ
- 解决linux下WaitForMultipleObjects的一种方法
- How to fix the issue that GEM_HOME and/or GEM_PATH not set issue for rvm in mac version 10.12
- How to improve the performance networking (TCP & UDP) on Linux 2.4+ for high-bandwidth applications
- How to programmatically clear the filesystem memory cache in C++ on a Linux system?
- How to Change the Icon for a File Type in Windows 7 and Vista(转)
- How to check the external public IP in linux
- How to count the number of threads in a process on Linux