waf: configure option for enforcing autostart method
[jackdbus.git] / tests / testThread.cpp
blobde012f77b60b18e6344540f9b9cbc496466faf28
1 /*
2 Copyright (C) 2004-2005 Grame
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <stdio.h>
21 #include <sys/time.h>
22 #include <stdlib.h>
23 #include <pthread.h>
24 #include <sys/time.h>
25 #include <errno.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <exception>
32 pthread_t fThread;
33 char fName[256];
34 int fFifo;
36 void* ThreadHandler(void* arg)
38 char c;
39 printf("ThreadHandler\n");
40 try {
41 while (1) {
42 read(fFifo, &c, sizeof(c));
43 sleep(1);
44 //pthread_testcancel();
46 } catch (std::exception e) {}
50 int main(int argc, char * const argv[])
52 int res;
53 void* status;
54 struct stat statbuf;
56 printf("Thread test\n");
57 std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
59 sprintf(fName, "/tmp/fifo");
61 if (stat(fName, &statbuf)) {
62 if (errno == ENOENT) {
63 if (mkfifo(fName, 0666) < 0) {
64 printf("Cannot create inter-client FIFO [%s]\n", fName);
65 return 0;
67 } else {
68 printf("Cannot check on FIFO %s\n", fName);
69 return 0;
71 } else {
72 if (!S_ISFIFO(statbuf.st_mode)) {
73 printf("FIFO (%s) already exists, but is not a FIFO!\n", fName);
74 return 0;
78 if ((fFifo = open(fName, O_RDWR|O_CREAT, 0666)) < 0) {
79 printf("Cannot open fifo [%s]", fName);
80 return 0;
83 if (res = pthread_create(&fThread, 0, ThreadHandler, NULL)) {
84 printf("Cannot set create thread %d\n", res);
85 return 0;
88 sleep(3);
89 printf("Cancel Thread\n");
90 pthread_cancel(fThread);
91 pthread_join(fThread, &status);