1/* This testcase is part of GDB, the GNU debugger. 2 3 Copyright 2018-2020 Free Software Foundation, Inc. 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 3 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 17 18#include <pthread.h> 19#include <stdio.h> 20#include <stdlib.h> 21#include <assert.h> 22#define NUM_THREADS 4 23 24/* Crude spin lock. Threads all spin until this is set to 0. */ 25int go = 1; 26 27/* Thread function, just spin until GO is set to 0. */ 28void * 29perform_work (void *argument) 30{ 31 /* Cast to volatile to ensure that ARGUMENT is loaded each time around 32 the loop. */ 33 while (*((volatile int*) argument)) 34 { 35 /* Nothing. */ 36 } 37 return NULL; 38} 39 40/* The spin loop for the main thread. */ 41void 42function (void) 43{ 44 (void) perform_work (&go); 45 printf ("Finished from function\n"); 46} 47 48/* Main program, create some threads which all spin waiting for GO to be 49 set to 0. */ 50int 51main (void) 52{ 53 pthread_t threads[NUM_THREADS]; 54 int result_code; 55 unsigned index; 56 57 /* Create some threads. */ 58 for (index = 0; index < NUM_THREADS; ++index) 59 { 60 printf ("In main: creating thread %d\n", index); 61 result_code = pthread_create (&threads[index], NULL, perform_work, &go); 62 assert (!result_code); 63 } 64 65 function (); 66 67 /* Wait for each thread to complete. */ 68 for (index = 0; index < NUM_THREADS; ++index) 69 { 70 /* Block until thread INDEX completes. */ 71 result_code = pthread_join (threads[index], NULL); 72 assert (!result_code); 73 printf ("In main: thread %d has completed\n", index); 74 } 75 printf ("In main: All threads completed successfully\n"); 76 return 0; 77} 78