pthread_mutex_t mutex;
- static void* thread1_fun(void* ignore)
-static void* thread_fun(void* ignore)
++static void* thread_fun(void* val)
{
pthread_mutex_lock(&mutex);
pthread_mutex_unlock(&mutex);
- fprintf(stderr, "The first thread is terminating.\n");
- return NULL;
- }
- static void* thread2_fun(void* ignore)
- {
- pthread_mutex_lock(&mutex);
- pthread_mutex_unlock(&mutex);
-
- fprintf(stderr, "The second thread is terminating.\n");
++ fprintf(stderr, "The thread %d is terminating.\n", *(int*)val);
return NULL;
}
int main(int argc, char* argv[])
{
- fprintf(stderr, "User main is starting\n");
-
pthread_mutex_init(&mutex, NULL);
- pthread_t thread1, thread2;
- pthread_create(&thread1, NULL, thread1_fun, NULL);
- pthread_create(&thread2, NULL, thread2_fun, NULL);
++ int id[2] = {0, 1};
+ pthread_t thread1;
+ pthread_t thread2;
- pthread_create(&thread1, NULL, thread_fun, NULL);
- fprintf(stderr, "here\n");
- pthread_create(&thread2, NULL, thread_fun, NULL);
- fprintf(stderr, "there\n");
++ pthread_create(&thread1, NULL, thread_fun, (void*)&id[0]);
++ pthread_create(&thread2, NULL, thread_fun, (void*)&id[1]);
+ fprintf(stderr, "All threads are started.\n");
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
- fprintf(stderr, "User main is done\n");
+ fprintf(stderr, "User's main is terminating.\n");
return 0;
}
XBT_LOG_NEW_DEFAULT_CATEGORY(sthread, "pthread intercepter");
namespace sg4 = simgrid::s4u;
- static sg4::Host* lilibeth = NULL;
+ static sg4::Host* lilibeth = nullptr;
int sthread_main(int argc, char** argv, char** envp, int (*raw_main)(int, char**, char**))
{
std::ostringstream id;
id << std::this_thread::get_id();
- XBT_INFO("sthread main() is starting in thread %s", id.str().c_str());
+ XBT_DEBUG("sthread main() is starting in thread %s", id.str().c_str());
sg4::Engine e(&argc, argv);
auto* zone = sg4::create_full_zone("world");
sthread_enable();
sg4::ActorPtr main_actor = sg4::Actor::create("tid 0", lilibeth, raw_main, argc, argv, envp);
- XBT_INFO("sthread main() is launching the simulation");
+ XBT_INFO("Starting the simulation.");
sg4::Engine::get_instance()->run();
+ XBT_INFO("All threads exited. Terminating the simulation.");
return 0;
}
sthread_disable();
}
- int sthread_create(unsigned long int* thread, const /*pthread_attr_t*/ void* attr, void* (*start_routine)(void*),
+ int sthread_create(unsigned long int* thread, const void* /*pthread_attr_t* attr*/, void* (*start_routine)(void*),
void* arg)
{
static int TID = 0;
TID++;
- XBT_INFO("Create thread %d", TID);
+ XBT_VERB("Create thread %d", TID);
int rank = 0;
#if HAVE_SMPI
if (SMPI_is_inited())
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
#endif
- char name[100];
- sprintf(name, "%d:%d", rank, TID);
- sg4::ActorPtr actor = sg4::Actor::init(name, lilibeth);
+ std::string name = simgrid::xbt::string_printf("%d:%d", rank, TID);
+ sg4::ActorPtr actor = sg4::Actor::init(name.c_str(), lilibeth);
actor->start(thread_create_wrapper, start_routine, arg);
intrusive_ptr_add_ref(actor.get());
*thread = reinterpret_cast<unsigned long>(actor.get());
return 0;
}
- int sthread_join(sthread_t thread, void** retval)
+ int sthread_join(sthread_t thread, void** /*retval*/)
{
sg4::ActorPtr actor(reinterpret_cast<sg4::Actor*>(thread));
actor->join();
return 0;
}
- int sthread_mutex_init(sthread_mutex_t* mutex, const /*pthread_mutexattr_t*/ void* attr)
+ int sthread_mutex_init(sthread_mutex_t* mutex, const void* /*pthread_mutexattr_t* attr*/)
{
auto m = sg4::Mutex::create();
intrusive_ptr_add_ref(m.get());