Ejemplo n.º 1
0
extern int main(void)
{
  int i;
  int retval;
  cl_thread_settings_t thread_list[THREAD_COUNT];

/*  timeout_thread = (cl_thread_settings_t*) malloc(sizeof(cl_thread_settings_t)); */

  for (i=0;i<THREAD_COUNT;i++) {
     char name[20];
     sprintf(name,"thread_%d_",i);
     if ( (retval=cl_thread_setup(&thread_list[i],NULL,name, i, timeout_thread_main, NULL, NULL, CL_TT_USER1)) != CL_RETVAL_OK) {
        printf("error: cl_thread_setup() - %d\n", retval);
     }
  }

  
  retval = cl_thread_shutdown(&thread_list[0]);  
  if (retval != CL_RETVAL_OK) {
     printf("error: cl_thread_shutdown() - %d\n", retval);
  }

  for (i=THREAD_COUNT/2;i<THREAD_COUNT;i++) {
     retval = cl_thread_trigger_event(&thread_list[i]);
     if (retval != CL_RETVAL_OK) {
        printf("error: cl_thread_trigger_event() - %d\n", retval);
     }

     retval = cl_thread_join(&thread_list[i]); 
     if (retval != CL_RETVAL_OK) {
        printf("error: cl_thread_join() - %d\n", retval);
     }
  }

  for (i=0;i<THREAD_COUNT;i++) {
     printf("main(): thread %s: state= %s\n",(&thread_list[i])->thread_name,cl_thread_get_state(&thread_list[i]));
  }
 
  for (i=0;i<(THREAD_COUNT/2);i++) {
     retval = cl_thread_join(&thread_list[i]);
     if (retval != CL_RETVAL_OK) {
        printf("error: cl_thread_join() - %d\n", retval);
     }
  }
 
  for (i=0;i<THREAD_COUNT;i++) {
     retval =  cl_thread_cleanup(&thread_list[i]);
     if (retval != CL_RETVAL_OK) {
        printf("error: cl_thread_cleanup() - %d, thread %d\n", retval,i);
     }
  }

/*  free(timeout_thread); */

  printf("main done\n");
  return 0;
}
Ejemplo n.º 2
0
extern int main(int argc, char** argv)
{
  cl_raw_list_t* log_list = NULL;
  cl_thread_settings_t* thread_p = NULL;
  cl_thread_settings_t* log_thread = NULL;
  cl_thread_settings_t* dummy_thread_p = NULL;
  int count = 2;



  if (argc != 2) {
     printf("please enter 0 for standard log function, 1 for special\n");
     exit(1);
  }


  /* setup log list */
  if (atoi(argv[1]) == 0) {
     cl_log_list_setup(&log_list,"application",0,CL_LOG_IMMEDIATE, NULL);
  } else {
     cl_log_list_setup(&log_list,"application",0,CL_LOG_IMMEDIATE, my_log_flush_list);
  }

  /* setup log thread */
  log_thread = (cl_thread_settings_t*) malloc(sizeof(cl_thread_settings_t));
  cl_thread_setup(log_thread, log_list, "log thread", 1,my_log_thread, NULL, NULL, CL_TT_USER1);
  cl_log_list_set_log_level(log_list,CL_LOG_DEBUG );

  /* setup thread list */
  cl_thread_list_setup(&thread_list,"thread list");

  /* setup first thread */
  cl_thread_list_create_thread(thread_list, &dummy_thread_p, log_list, "1st thread", 1, my_test_thread, NULL, NULL, CL_TT_USER1);

  /* setup second thread */
  cl_thread_list_create_thread(thread_list, &dummy_thread_p, log_list, "2nd thread", 2, my_test_thread, NULL, NULL, CL_TT_USER1);

  
  thread_p = cl_thread_list_get_thread_by_id(thread_list, 1);
  CL_LOG_STR( CL_LOG_INFO,  "My thread name is ", thread_p->thread_name  );

  thread_p = cl_thread_list_get_thread_by_name(thread_list, "2nd thread");
  CL_LOG_STR( CL_LOG_INFO,  "My thread name is ", thread_p->thread_name  );

  while ( count  < 10  ) {
     int id;
     char new_thread_name[255];

     count++;

     CL_LOG_INT(CL_LOG_INFO,  "number of threads: ", (int)cl_raw_list_get_elem_count(thread_list) );

     thread_p = cl_thread_list_get_first_thread(thread_list);
     id = thread_p->thread_id;

     CL_LOG_INT( CL_LOG_INFO,  "delete thread: ", id );
     cl_thread_list_delete_thread_by_id(thread_list, id);
     CL_LOG_INT(CL_LOG_INFO,  "thread deleted, id: ", id );


     sprintf(new_thread_name,"thread nr %d", count);
     CL_LOG( CL_LOG_INFO,  "adding thread ...");
     cl_thread_list_create_thread(thread_list,&dummy_thread_p, log_list,new_thread_name, id, my_test_thread, NULL, NULL, CL_TT_USER1);
     CL_LOG( CL_LOG_INFO, "adding thread done");

  }

  
  /* remove all threads from thread list */
  while ( (thread_p=cl_thread_list_get_first_thread(thread_list)) != NULL ) {
     int id = thread_p->thread_id;
     CL_LOG_INT( CL_LOG_INFO,  "delete thread: ", id );
     CL_LOG_INT( CL_LOG_INFO,  "event calls: ", (int)thread_p->thread_event_count );

     cl_thread_list_delete_thread_by_id(thread_list, id);
     CL_LOG_INT( CL_LOG_INFO,  "thread deleted, id: ", id );
  }


  CL_LOG_INT( CL_LOG_INFO,  "log event calls: ", (int)log_thread->thread_event_count );

  CL_LOG( CL_LOG_INFO,  "cleaning up thread list");
  cl_thread_list_cleanup(&thread_list);

  /* shutdown of log thread */
  cl_thread_shutdown(log_thread);
  cl_thread_join(log_thread);
  cl_thread_cleanup(log_thread);
  free(log_thread);

  /* cleanup log list */
  CL_LOG( CL_LOG_INFO,  "cleaning up log list");
  printf( "cl_log_list_cleanup() returned: %s\n", cl_get_error_text(cl_log_list_cleanup(&log_list)));
  printf("main done\n");










  return 0;
}