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,
15 "Messages specific for this sem example");
19 #define THREAD_THREADS_MAX ((unsigned int)10)
24 void *thread_routine(void *param);
26 /* an entry of the table of threads */
27 typedef struct s_thread_entry {
28 xbt_os_thread_t thread;
29 unsigned int thread_index; /* the index of the thread */
30 } s_thread_entry_t, *thread_entry_t;
33 static xbt_os_sem_t sem = NULL;
37 int main(int argc, char *argv[])
39 s_thread_entry_t threads_table[THREAD_THREADS_MAX] = { 0 };
43 xbt_init(&argc, argv);
45 sem = xbt_os_sem_init(1);
49 while (i < THREAD_THREADS_MAX) {
50 threads_table[i].thread_index = i;
53 (threads_table[i].thread =
54 xbt_os_thread_create("thread", thread_routine,
55 &(threads_table[i].thread_index))))
61 /* close the thread handles */
62 for (j = 0; j < THREAD_THREADS_MAX; j++)
63 xbt_os_thread_join(threads_table[j].thread, NULL);
65 xbt_os_sem_destroy(sem);
67 INFO1("sem_basic terminated with exit code %d (success)", EXIT_SUCCESS);
73 void *thread_routine(void *param)
75 int thread_index = *((int *) param);
78 xbt_os_sem_acquire(sem);
79 INFO1("Hello i'm the thread %d", thread_index);
81 INFO1("The new value of the global variable is %d, bye", value);
82 xbt_os_sem_release(sem);
84 xbt_os_thread_exit(&exit_code);
86 return (void *) (NULL);