From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f72b0,b484d09643d9bbb6,start X-Google-Attributes: gidf72b0,public X-Google-Thread: f8c9b,b484d09643d9bbb6,start X-Google-Attributes: gidf8c9b,public X-Google-Thread: fc89c,b484d09643d9bbb6,start X-Google-Attributes: gidfc89c,public X-Google-Thread: fe1d2,b484d09643d9bbb6,start X-Google-Attributes: gidfe1d2,public X-Google-Thread: 115614,b484d09643d9bbb6,start X-Google-Attributes: gid115614,public X-Google-Thread: fee84,b484d09643d9bbb6,start X-Google-Attributes: gidfee84,public X-Google-Thread: 11035d,b484d09643d9bbb6,start X-Google-Attributes: gid11035d,public X-Google-Thread: fa664,b484d09643d9bbb6,start X-Google-Attributes: gidfa664,public From: blb@elt.com (Luke Braly) Subject: See if you can run this Date: 1997/05/12 Message-ID: <33776eb4.19046817@news.elt.com>#1/1 X-Deja-AN: 241162482 Organization: Electronic Label Technology, Inc. Newsgroups: comp.unix.sco.programmer,comp.unix.sco.misc,comp.unix.aix,comp.unix.misc,comp.unix.osf.osf1,comp.unix.programmer,comp.unix.questions,comp.unix.solaris If you are able to multi-thread on your Unix implementation, I'd be interested to see if you can run the program below. It uses msem_ type semaphores in conjunction with threads. A colleague of mine says it won't work on some Unix systems because the process (as opposed to a thread) will be locked, thus causing the program to hang if two threads in the same process lock the same semaphore. However, it works fine under AIX. The command to compile and link under AIX is: xlC_r -lpthreads msemthr.cpp. (xlC_r is the thread-compatible version of AIX's C++ compiler.) The output should be: thread - 0 thread - 1 thread - 2 thread - 3 (Note: you may have to replace the "pthread_create" call if your Unix system uses a different threading mechanism.) //THE PROGRAM: #include #include #include #include #include msemaphore *sem; void* threadfunc(void *p) { int n = *(int*)p; msem_lock(sem,0); printf("thread - %d\n",n); sleep(1); msem_unlock(sem,0); return (NULL); } main() { pthread_t ThreadID; int n,m; sem = (msemaphore*) mmap(NULL,sizeof(msemaphore), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS | MAP_VARIABLE, -1, 0 ); sem = msem_init(sem,MSEM_UNLOCKED); int X[4]; for (n=0; n<4; n++) { X[n] = n; pthread_create(&ThreadID, NULL, threadfunc, &(X[n])); } sleep(7); } //(END)