1 /* Copyright (c) 2007. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "xbt/xbt_os_thread.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(sem_basic,"Messages specific for this sem example");
18 #define THREAD_THREADS_MAX ((unsigned int)10)
24 thread_routine(void* param);
26 /* an entry of the table of threads */
27 typedef struct s_thread_entry
29 xbt_os_thread_t thread;
30 unsigned int thread_index; /* the index of the thread */
31 }s_thread_entry_t,* thread_entry_t;
40 main(int argc, char* argv[])
42 s_thread_entry_t threads_table[THREAD_THREADS_MAX] = {0};
48 sem = xbt_os_sem_init(1);
52 while(i < THREAD_THREADS_MAX)
54 threads_table[i].thread_index = i;
56 if(NULL == (threads_table[i].thread = xbt_os_thread_create("thread",thread_routine,&(threads_table[i].thread_index))))
62 /* close the thread handles */
63 for(j = 0; j < THREAD_THREADS_MAX; j++)
64 xbt_os_thread_join(threads_table[j].thread,NULL);
66 xbt_os_sem_destroy(sem);
68 INFO1("sem_basic terminated with exit code %d (success)",EXIT_SUCCESS);
77 thread_routine(void* param)
79 int thread_index = *((int*)param);
82 xbt_os_sem_acquire(sem);
83 INFO1("Hello i'm the thread %d",thread_index);
85 INFO1("The new value of the global variable is %d, bye",value);
86 xbt_os_sem_release(sem);
88 xbt_os_thread_exit(&exit_code);