void xd::lua::scheduler::start(luabind::object func) { // create a new thread lua_State *thread = lua_newthread(m_vm.lua_state()); // push the function in the new thread, because the function // the thread calls must be called in its local environment func.push(thread); // construct a callable object from it, the value is copied from stack luabind::object thread_func(luabind::from_stack(thread, -1)); // remove the original value from the stack lua_pop(thread, 1); // set the current thread m_thread_stack->threads.push(thread); m_current_thread = thread; // start the thread luabind::object result = luabind::resume_function<luabind::object>(thread_func); // if the thread was yielded from lua side, and the return value is a callable function int type = luabind::type(result); if (type == LUA_TFUNCTION || type == LUA_TTABLE || type == LUA_TUSERDATA) { yield(xd::create<detail::callback_task_lua>(result)); } // reset current thread m_thread_stack->threads.pop(); if (m_thread_stack->threads.empty()) m_current_thread = nullptr; else m_current_thread = m_thread_stack->threads.top(); }
void run_test() { const std::size_t thread_count = static_cast<std::size_t>( std::max<unsigned>(detail::thread::hardware_concurrency(), 16)); std::vector<random_generator_ptr> rngs; for (std::size_t i = 0; i != thread_count; ++i) { rngs.push_back(detail::make_shared<random_generator>()); } for (std::size_t n = 0; n != iteration_count; ++n) { detail::barrier work_barrier( static_cast<detail::barrier::counter_type>(thread_count)); ma::thread_group threads; for (std::size_t i = 0; i != thread_count - 1; ++i) { threads.create_thread( detail::bind(thread_func, detail::ref(work_barrier), rngs[i])); } thread_func(work_barrier, rngs.back()); threads.join_all(); } const foo_ptr f = foo::get_instance(); std::cout << "Init counter: " << to_string(f->init_count()) << std::endl; }
int main(int argc, char **argv) { int i, mcmodel; void *retval; int t1, t2, t3, t4; pthread_t thr[64]; if (argc != 4) { printf("Usage : reduct <NPROC> <NELEMENTS> <MCMODEL>\n"); exit(0); } pthread_mutex_init(&mut, NULL); P = atoi(argv[1]); N = atoi(argv[2]); pthread_barrier_init(&barr, NULL, P); t3 = time(NULL); for (i=1; i<P; i++) pthread_create(&thr[i], NULL, thread_func, &args[i]); t1 = time(NULL); thread_func(&args[0]); t2 = time(NULL); //printf("Thread id waiting \n"); for (i=1; i<P; i++) pthread_join(thr[i], &retval); t4 = time(NULL); printf("Time for completion : creation %d, reduction : %d, joining : %d\n",t1-t3, t2-t1, t4 - t2); return 0; }
void ABTD_thread_func_wrapper(int func_upper, int func_lower, int arg_upper, int arg_lower) { void (*thread_func)(void *); void *p_arg; size_t ptr_size, int_size; ptr_size = sizeof(void *); int_size = sizeof(int); if (ptr_size == int_size) { thread_func = (void (*)(void *))(uintptr_t)func_lower; p_arg = (void *)(uintptr_t)arg_lower; } else if (ptr_size == int_size * 2) { uintptr_t shift_bits = CHAR_BIT * int_size; uintptr_t mask = ((uintptr_t)1 << shift_bits) - 1; thread_func = (void (*)(void *))( ((uintptr_t)func_upper << shift_bits) | ((uintptr_t)func_lower & mask)); p_arg = (void *)( ((uintptr_t)arg_upper << shift_bits) | ((uintptr_t)arg_lower & mask)); } else { ABTI_ASSERT(0); } thread_func(p_arg); /* Now, the ULT has finished its job. Terminate the ULT. * We don't need to use the atomic operation here because the ULT will be * terminated regardless of other requests. */ ABTI_thread *p_thread = ABTI_local_get_thread(); p_thread->request |= ABTI_THREAD_REQ_TERMINATE; }
static DWORD WINAPI thread_run(LPVOID arg) { void (*thread_func)(void); /* VC really doesn't like casting between data and function pointers. */ memcpy(&thread_func, &arg, sizeof(thread_func)); thread_func(); return 0; }
void ABTD_thread_func_wrapper(void *p_arg) { ABTD_thread_context *p_fctx = (ABTD_thread_context *)p_arg; void (*thread_func)(void *) = p_fctx->f_thread; thread_func(p_fctx->p_arg); /* NOTE: ctx is located in the beginning of ABTI_thread */ ABTI_thread *p_thread = (ABTI_thread *)p_fctx; ABTD_thread_terminate(p_thread); }
int main(int argc, char **argv){ if(argc != 2) { printf("usage : %s nb_threads\n", argv[0]); exit(1); } int nb_threads = atoi(argv[1]); srand(time(NULL)*getpid()); /*** Linked list ***/ list_init(&list); /*** Tracing ... ***/ tracing_s tracing; t = &tracing; /* set a timeout to kill the process in case of deadlock */ alarm(5); tracing_init(t, nb_threads); /* Give a human readable name to events */ tracing_register_event(t, BR_EVENT_ID, "BEGIN READ"); tracing_register_event(t, ER_EVENT_ID, "END READ"); tracing_register_event(t, BW_EVENT_ID, "BEGIN WRITE"); tracing_register_event(t, EW_EVENT_ID, "END WRITE"); /*** Thread creation ***/ pthread_t *tids = malloc(sizeof(pthread_t) * nb_threads -1); long int i; for(i = 0; i < nb_threads-1; i++){ pthread_create(tids+i, NULL, thread_func, (void*)(i+1)); } thread_func((void*)0); for(i = 0; i < nb_threads-1; i++){ pthread_join(tids[i], NULL); } PrintList(); print_all_events_per_date(t); exit(EXIT_SUCCESS); }
void client::run_test() { unsigned optimal_threads_count = boost::thread::hardware_concurrency(); if (optimal_threads_count == 0) optimal_threads_count = 1; ostream_ << "Create " << optimal_threads_count << " threads for client" << std::endl; for (unsigned i = 0; i < optimal_threads_count; ++i) threads_.add_thread(new boost::thread([this, i] () { thread_func(i); })); threads_.join_all(); ostream_ << "All client's threads done" << std::endl; }
int main(int argc,char **argv) { // int i; struct timeval tv_start; struct timeval tv_end; pthread_t *tids=NULL; // void *status; // int result; // double sum; if(argc>1){ NUM_THREADS=atoi(argv[1]); } printf("Using %d threads...\n",NUM_THREADS); tids=(pthread_t *)malloc(sizeof(pthread_t) * NUM_THREADS); if(tids==NULL){ fprintf(stderr,"ERROR[%s:%d]\n",__FILE__,__LINE__); exit(EXIT_FAILURE); } initialize(); gettimeofday(&tv_start,NULL); /* for(i=1;i<NUM_THREADS;i++){ result=pthread_create(&tids[i],NULL,thread_func,(void *)i); if(result!=0){ printf("Error(%d) occurred in pthread_create.\n",result); exit(EXIT_FAILURE); } } */ thread_func(0); /* for(i=1;i<NUM_THREADS;i++){ pthread_join(tids[i],&status); } */ gettimeofday(&tv_end,NULL); printf("-- Sequential Version ==\n"); printf("Execution with %d threads: %lf sec.\n",NUM_THREADS,get_time(tv_start,tv_end)); if(validation){ validate(); } if(print_data){ print(); } return 0; }
void *thread_func(void *arg) { int count = ((thread_param *) arg)->count; int lo = ((thread_param *) arg)->lo; int hi = ((thread_param *) arg)->hi; if (lo < hi) { int m = (lo + hi) / 2; if (count < MAX_THREADS) { pthread_t thread; thread_param thr_arg[2]; thr_arg[0].lo = lo; thr_arg[0].hi = m; thr_arg[0].count = 2 * count; if (0 != pthread_create(&thread, NULL, thread_func, &(thr_arg[0]))) { exit(thr_arg[0].count); } thr_arg[1].lo = m + 1; thr_arg[1].hi = hi; thr_arg[1].count = 2 * count; thread_func(&(thr_arg[1])); pthread_join(thread, NULL); /* Warten bis die Threads terminiert sind */ } else { mergesort(lo, m); mergesort(m + 1, hi); } merge(lo, m, hi); } return 0; }
int main(int argc, char **argv) { long sd; if (3 > argc) { fprintf(stderr, "Usage: %s host port\n", argv[0]); return -1; } sd = echo_socket_listen(argv[1], argv[2]); if (0 > sd) return -1; /* --- */ int num_cores = (4 == argc) ? atoi(argv[3]) : sysconf(_SC_NPROCESSORS_ONLN); fprintf(stderr, "found %d cores\n", num_cores); int rc, i; for (i = 0; i < num_cores - 1; ++i) { pthread_t thread_id; rc = pthread_create(&thread_id, NULL, thread_func, (void *) sd); if (0 != rc) { fprintf(stderr, "thread create error %d\n", rc); return -1; } } thread_func((void *) sd); return 0; }
void Thread::RunInThread() { thread_func(thread_arg); }
int main(int argc, char *argv[]) { int sock, client_sock; struct sockaddr_in server; #ifdef USE_PTHREAD pthread_t thread; #else pid_t fork_ret; #endif //Create socket sock = socket(AF_INET, SOCK_STREAM, 0); if (sock == -1) { printf("Could not create socket\n"); return 1; } printf("Socket created\n"); //Prepare the sockaddr_in structure server.sin_family = AF_INET; server.sin_addr.s_addr = INADDR_ANY; server.sin_port = htons(1234); //Bind if (bind(sock, (struct sockaddr *)&server, sizeof(server)) < 0) { //print the error message perror("bind failed. Error\n"); return 1; } //Listen listen(sock, 3); //Accept and incoming connection puts("Waiting for incoming connection...\n"); //Receive filename from client while (1) { //accept connection from an incoming client client_sock = accept(sock, 0, 0); if (client_sock < 0) { printf("accept failed\n"); return 1; } #ifdef USE_PTHREAD printf("client connected, using pthread to handle...\n"); pthread_create(&thread, NULL, thread_func, (void*)&client_sock); #else printf("client connected, using fork to handle...\n"); fork_ret = fork(); if (fork_ret < 0) { printf("fork failed\n"); return 1; } if (fork_ret == 0) { // child process close(sock); thread_func((void*)&client_sock); break; } else // parent process close(client_sock); #endif } close(sock); return 0; }
int main(int argc, char *argv[]) { int port = 1234; // default port number to use int sock, listener, rc; struct sockaddr_in addr; char filename[1024]; int bytes_read; // check command line arguments, handling an optional port number if ((argc == 2) || (argc == 3)) { port = atoi(argv[1]); if (port <= 0) { printf("Invalid port: %s\n", argv[1]); exit(1); } } else if (argc != 1) { printf("Usage: %s [port] [p|t]\n", argv[0]); exit(1); } // reate Internet domain socket listener = socket(AF_INET, SOCK_STREAM, 0); if (listener < 0) { perror("Socket"); exit(1); } addr.sin_family = AF_INET; addr.sin_port = htons(port); addr.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(listener, (struct sockaddr *)&addr, sizeof(addr)) < 0) { perror("Bind"); exit(2); } listen(listener, 1); int pid; while (1) { struct sockFile *sf; pthread_t thread_id; // wait for a client to connect sock = accept(listener, NULL, NULL); sf = (struct sockFile*) malloc (sizeof(struct sockFile)); if(sock < 0) { perror("Accept"); exit(3); } bytes_read = recv(sock, filename, 1024, 0); sf->sock = sock; strcpy(sf->filename, filename); if (argv[2] == "t") { rc = pthread_create(&thread_id, NULL, thread_func, (void*)sf); if (rc) printf("Can't create thread!"); } else { pid = fork(); if (pid == 0) thread_func((void*)sf); } } return 0; }
void main (int argc, char *argv[]) { int error, on; int listen_sd; int accept_sd; char buffer[80]; struct sockaddr_in addr; pid_t child_pid; // create listening socket listen_sd = socket(AF_INET, SOCK_STREAM, 0); if (listen_sd < 0) { perror("socket() failed"); exit(-1); } error = setsockopt(listen_sd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)); // setoption error if (error < 0) { perror("setsockopt() failed"); close(listen_sd); exit(-1); } memset(&addr, 0, sizeof(addr)); addr.sin_family = AF_INET; addr.sin_addr.s_addr = htonl(INADDR_ANY); addr.sin_port = htons(SERVER_PORT); error = bind(listen_sd, (struct sockaddr *)&addr, sizeof(addr)); if (error < 0) { perror("bind() failed"); close(listen_sd); exit(-1); } error = listen(listen_sd, QUEUE_SIZE); if (error < 0) { perror("listen() failed"); close(listen_sd); exit(-1); } printf("The server is ready\n"); while (1) { accept_sd = accept(listen_sd, NULL, NULL); if (accept_sd < 0) { perror("accept() failed"); close(listen_sd); exit(-1); } // here start new thread pthread_t pthread; if (thread_def) { pthread_create(&pthread, NULL, &thread_func, (void*)&accept_sd); } else { child_pid = fork(); if(child_pid == 0) { thread_func((void*)&accept_sd); } } } close(listen_sd); }
void operator()(bool throws) const { if (throws) throw std::exception("an exception happened..."); thread_func(); }
void operator()() const { thread_func(); }
static int thread_block(void *arg) { while (!kthread_should_stop()) thread_func(); return 0; }
static void *thread_run(void *arg) { void (*thread_func)(void) = arg; thread_func(); return NULL; }