int main(void) { if (mtx_init(&mtx, mtx_plain) != thrd_success) { fprintf(stderr, "Error initializing the mutex.\n"); return -1; } // As in Example 14-2: // start threads, wait for them to finish, print output: clock_t cl = clock(); thrd_t th1, th2; if( thrd_create(&th1, (thrd_start_t)incFunc, NULL) != thrd_success || thrd_create(&th2, (thrd_start_t)decFunc, NULL) != thrd_success) { fprintf(stderr,"Error creating thread\n"); return -1; } thrd_join(th1, NULL); thrd_join(th2, NULL); printf("Counter: %ld \t", counter); printf("CPU time: %ld ms\n", (clock()-cl)*1000L/CLOCKS_PER_SEC); mtx_destroy(&mtx); return 0; }
int main(void) { SetConsoleTitle(TITLE); mtx_init(&gmutex, mtx_plain); cnd_init(&gcond); init_timer(); init_path(); init_strings(); init_server(); initpackets(); //test_map(1); thrd_create(&t1, initsocket, (void*)0); thrd_create(&t2, commands, (void*)0); server_loop(); destroy_server(); mtx_destroy(&gmutex); cnd_destroy(&gcond); #if _DEBUG VLDReportLeaks(); #endif exit(TRUE);// Exit program }
static void test_condition_variables (void) { thrd_t t1, t[40]; int i; /* Set the global counter to the number of threads to run. */ gCount = 40; /* Start the waiting thread (it will wait for gCount to reach zero). */ thrd_create(&t1, thread_condition_waiter, NULL); /* Start a bunch of child threads (these will decrease gCount by 1 when they finish) */ for (i = 0; i < 40; ++ i) { thrd_create(&t[i], thread_condition_notifier, NULL); } /* Wait for the waiting thread to finish */ thrd_join(t1, NULL); /* Wait for the other threads to finish */ for (i = 0; i < 40; ++ i) { thrd_join(t[i], NULL); } }
static void b1(void *obj) { thrd_t t3, t4; int i = 7; int r = atomic_load_explicit(&x, memory_order_acquire); printf("r = %d\n", r); store_32(&var, 2); thrd_create(&t3, (thrd_start_t)&a, &i); thrd_create(&t4, (thrd_start_t)&b2, NULL); thrd_join(t3); thrd_join(t4); }
int user_main(int argc, char **argv) { thrd_t t1, t2; atomic_init(&mylock.lock, RW_LOCK_BIAS); thrd_create(&t1, (thrd_start_t)&a, NULL); thrd_create(&t2, (thrd_start_t)&a, NULL); thrd_join(t1); thrd_join(t2); return 0; }
int main(int argc, char**argv) { int errno = 0; srand(time(NULL)); if (errno = PacMan()) { return errno; } /* ToDo: add Timer-Callbacks for Goodies */ //SDL_TimerID timer = SDL_AddTimer(200, NewGoodie, NULL); if(CheckEvents()){ return 0; } SDL_TimerID timer = SDL_AddTimer(randomGTime(2000, 7000), NewGoodie, NULL); if(timer){ printf("Timer erfolgreich erstellt!\n"); } thrd_create(NULL, Enemy, NULL); /* Implementation of an enemy *********************************************/ /* An enemy lives for 25 Steps and walks a bit slower than the player ... */ /**************************************************************************/ /* ToDo: move enemy into Thread */ /* Already done */ /* EventLoop */ return EventLoop(); }
static int do_test (void) { /* Setting an invalid key should return an error. */ if (tss_set (key, TSS_VALUE) == thrd_success) FAIL_EXIT1 ("tss_set succeed where it should have failed"); if (tss_create (&key, NULL) != thrd_success) FAIL_EXIT1 ("tss_create failed"); thrd_t id; if (thrd_create (&id, tss_thrd, NULL) != thrd_success) FAIL_EXIT1 ("thrd_create failed"); if (thrd_join (id, NULL) != thrd_success) FAIL_EXIT1 ("thrd failed"); /* The value set in tss_thrd should not be visible here. */ void *value = tss_get (key); if (value != 0) FAIL_EXIT1 ("tss_get succeed where it should have failed"); tss_delete (key); return 0; }
static void test_tss (void) { thrd_t threads[TEST_TSS_N_THREADS]; int* value = (int*)malloc(sizeof(int)); int i; *value = rand(); tss_create(&(test_tss_data.key), test_tss_free); mtx_init(&(test_tss_data.mutex), mtx_plain); test_tss_data.values_freed = 0; assert(tss_get(test_tss_data.key) == NULL); tss_set(test_tss_data.key, value); assert(tss_get(test_tss_data.key) == value); for (i = 0; i < TEST_TSS_N_THREADS; i++) { thrd_create(&(threads[i]), test_tss_thread_func, NULL); } for (i = 0; i < TEST_TSS_N_THREADS; i++) { thrd_join(threads[i], NULL); } assert(test_tss_data.values_freed == TEST_TSS_N_THREADS); assert(tss_get(test_tss_data.key) == value); tss_delete(test_tss_data.key); assert(tss_get(test_tss_data.key) == NULL); assert(test_tss_data.values_freed == TEST_TSS_N_THREADS); free(value); }
static void test_once (void) { const once_flag once_flag_init = ONCE_FLAG_INIT; thrd_t threads[TEST_ONCE_N_THREADS]; int i; /* Initialize 10000 once_flags */ for (i = 0; i < 10000 ; i++) { onceFlags[i] = once_flag_init; } /* Clear the global counter. */ mtx_lock(&gMutex); gCount = 0; mtx_unlock(&gMutex); /* Create threads */ for (i = 0; i < TEST_ONCE_N_THREADS; i++) { thrd_create(&(threads[i]), thread_once, NULL); } /* Wait for all threads to finish executing. */ for (i = 0; i < TEST_ONCE_N_THREADS; i++) { thrd_join(threads[i], NULL); } /* Check the global count */ assert(gCount == 10000); }
int main(void) { int n; usCount start; pthread_permit1_t permit1; printf("Press key to continue ...\n"); getchar(); printf("Wait ...\n"); start=GetUsCount(); while(GetUsCount()-start<3000000000000ULL); printf("Calculating timing overhead ...\n"); for(n=0; n<5000000; n++) { start=GetUsCount(); timingoverhead+=GetUsCount()-start; } timingoverhead/=5000000; printf("Timing overhead on this machine is %u us. Go!\n", (size_t) timingoverhead); pthread_permit1_init(&permit1, 1); permitaddr=&permit1; for(n=0; n<THREADS; n++) { thrd_create(&threads[n], (thrd_start_t) threadfunc<pthread_permit1_t, pthread_permit1_grant, pthread_permit1_revoke, pthread_permit1_wait>, (void *)(size_t)n); } printf("Press key to kill all\n"); getchar(); done=1; mssleep(2000); printf("Press key to exit\n"); getchar(); return 0; }
static void test_mutex_timed(void) { struct TestMutexTimedData data; thrd_t thread; struct timespec interval = { 0, }; struct timespec start; struct timespec end; interval.tv_sec = 0; interval.tv_nsec = (NSECS_PER_SECOND / 10) * 2; mtx_init(&(data.mutex), mtx_timed); mtx_lock(&(data.mutex)); timespec_get(&(data.start), TIME_UTC); data.timeout = data.start; timespec_add_nsec(&(data.timeout), NSECS_PER_SECOND / 10); data.end = data.timeout; timespec_add_nsec(&(data.end), NSECS_PER_SECOND / 10); data.upper = data.end; timespec_add_nsec(&(data.upper), NSECS_PER_SECOND / 10); thrd_create(&thread, test_mutex_timed_thread_func, &data); timespec_get(&start, TIME_UTC); assert (thrd_sleep(&interval, &interval) == 0); timespec_get(&end, TIME_UTC); mtx_unlock(&(data.mutex)); thrd_join(thread, NULL); }
int main(int argc, char **argv) { thrd_t t[TRDS]; size_t i; struct add a[TRDS]; size_t gt = 0; int ep = 0; p = &t[0]; if( argc != 2 ) { puts("An integer argument must be passed"); exit(1); } size_t in = (size_t)atoi(argv[1]); for(i=0; i<TRDS; i++) { a[i].start = i*(in/TRDS) + 1; if(i == (TRDS-1)) { ep = in - TRDS*(in/TRDS); //In case of odd number, the last thread will do an extra addition } a[i].end = a[i].start + in/TRDS - 1 + ep; thrd_create(&t[i], func, &a[i]); } for(i=0; i<TRDS; i++) { thrd_join(t[i], 0); gt += a[i].tot; } printf("n*(n+1)/2 = %zu Grand Total: %zu \n", in*(in+1)/2, gt); return 0; }
int user_main(int argc, char **argv) { int i; int *param; unsigned int in_sum = 0, out_sum = 0; atomic_init(&x[1], 0); atomic_init(&x[2], 0); stack = (stack_t*) calloc(1, sizeof(*stack)); num_threads = procs; threads = (thrd_t*) malloc(num_threads * sizeof(thrd_t)); param = (int*) malloc(num_threads * sizeof(*param)); init_stack(stack, num_threads); for (i = 0; i < num_threads; i++) { param[i] = i; thrd_create(&threads[i], main_task, ¶m[i]); } for (i = 0; i < num_threads; i++) thrd_join(threads[i]); bool correct = false; //correct |= (a == 17 || a == 37 || a == 0); //MODEL_ASSERT(correct); free(param); free(threads); free(stack); return 0; }
bool initUartOutput(flowOutputState state, char* port, unsigned int baud, size_t bufferSize) { // Initialize UART communication int uartErrno = uart_open(port, baud); if (uartErrno) { caerLog(CAER_LOG_ALERT, SUBSYSTEM_UART, "Failed to identify serial communication, errno=%i.",uartErrno); return (false); } // Test message char* testMessage = "DVS128UART"; if (uart_tx((int) strlen(testMessage), (unsigned char*) testMessage)) { caerLog(CAER_LOG_ALERT, SUBSYSTEM_UART, "Test transmission unsuccessful - connection closed."); uart_close(); return(false); } // Start output handler thread state->thread = 0; if (thrd_create(&state->thread, &outputHandlerThread, state) != thrd_success) { caerLog(CAER_LOG_ALERT, SUBSYSTEM_UART,"Failed to start output handler thread"); return (false); } state->buffer = ringBufferInit(bufferSize); if (state->buffer == NULL) { caerLog(CAER_LOG_ERROR, SUBSYSTEM_UART, "Failed to allocate transfer ring-buffer."); return (false); } atomic_store(&state->running, true); caerLog(CAER_LOG_NOTICE, SUBSYSTEM_UART, "Streaming flow events to port %s.",port); return (true); }
int Music_decode(text_t* path, MusicData* data) { QWORD len; thrd_t id; if (thrd_busy == mtx_trylock(&s_mutex)) { Dialog_showError(TEXT("Decoding of stream already in-progress. Re-open after it's been completed.")); return 1; } // now this will break if we already have a thread running. free(data->fftData); data->fftData = 0; int flags = BASS_STREAM_DECODE | BASS_SAMPLE_MONO | BASS_POS_SCAN; #ifdef _WIN32 flags |= BASS_UNICODE; #endif HSTREAM chan = BASS_StreamCreateFile(0, path, 0, 0, flags); if (!chan) { Dialog_showError(TEXT("Unable to open stream for decode. No music data will be available.")); mtx_unlock(&s_mutex); return 0; } len = BASS_ChannelGetLength(chan, BASS_POS_BYTE); if (len == -1) { Dialog_showError(TEXT("Stream has no length. No music data will be available.")); BASS_StreamFree(chan); mtx_unlock(&s_mutex); return 0; } ThreadFuncData* threadData = malloc(sizeof(ThreadFuncData)); threadData->stream = chan; threadData->data = data; data->percentDone = 1; data->fftData = 0; // So there is a race-condition here as the mutex is unclocked while the thread is crated but in practice // this won't be a problem mtx_unlock(&s_mutex); if (thrd_create(&id, decodeFunc, threadData) != thrd_success) { printf("Unable to create decode thread. Stalling main thread and running...\n"); decodeFunc(threadData); } return 1; }
/* ==================== TFile_StartServer ==================== */ void TFile_StartServer( void ) { server_message_t message; if ( !server_initialized ) { T_FatalError( "TFile_StartServer: Server is not initialized" ); } if ( server_running ) { T_FatalError( "TFile_StartServer: Server is already running" ); } cnd_init( &server_condition ); mtx_init( &server_mutex, mtx_plain ); mtx_lock( &server_mutex ); message.ip_socket = server_socket; message.ip6_socket = server_socket6; if ( thrd_create( &server_thread, ServerThread, &message ) != thrd_success ) { T_FatalError( "TFile_StartServer: Unable to create thread" ); } cnd_wait( &server_condition, &server_mutex ); mtx_destroy( &server_mutex ); cnd_destroy( &server_condition ); server_running = t_true; }
bool thread_action_test(thrd_cb_t cb, void* arg = nullptr) { BEGIN_HELPER; static_assert(kNumThreads >= kSuccessCount, "Need more threads or less successes"); thrd_t threads[kNumThreads]; for (size_t i = 0; i < kNumThreads; i++) { ASSERT_EQ(thrd_create(&threads[i], cb, arg), thrd_success); } size_t success_count = 0; for (size_t i = 0; i < kNumThreads; i++) { int rc; ASSERT_EQ(thrd_join(threads[i], &rc), thrd_success); if (rc == kSuccess) { success_count++; ASSERT_LE(success_count, kSuccessCount, "Too many succeeding threads"); } else { ASSERT_EQ(rc, kFailure, "Unexpected return code from worker thread"); } } ASSERT_EQ(success_count, kSuccessCount, "Not enough succeeding threads"); END_HELPER; }
int main(void) { int i, result; Thread threads[] = { { NULL, "Red", 1.f, 0.f, 0.f, 0 }, { NULL, "Green", 0.f, 1.f, 0.f, 0 }, { NULL, "Blue", 0.f, 0.f, 1.f, 0 } }; const int count = sizeof(threads) / sizeof(Thread); if (!glfwInit()) { fprintf(stderr, "Failed to initialize GLFW: %s\n", glfwErrorString(glfwGetError())); exit(EXIT_FAILURE); } for (i = 0; i < count; i++) { glfwWindowHint(GLFW_POSITION_X, 200 + 250 * i); glfwWindowHint(GLFW_POSITION_Y, 200); threads[i].window = glfwCreateWindow(200, 200, GLFW_WINDOWED, threads[i].title, NULL); if (!threads[i].window) { fprintf(stderr, "Failed to open GLFW window: %s\n", glfwErrorString(glfwGetError())); exit(EXIT_FAILURE); } if (thrd_create(&threads[i].id, thread_main, threads + i) != thrd_success) { fprintf(stderr, "Failed to create secondary thread\n"); exit(EXIT_FAILURE); } } while (running) { assert(glfwGetCurrentContext() == NULL); glfwWaitEvents(); for (i = 0; i < count; i++) { if (glfwGetWindowParam(threads[i].window, GLFW_CLOSE_REQUESTED)) running = GL_FALSE; } } for (i = 0; i < count; i++) thrd_join(threads[i].id, &result); exit(EXIT_SUCCESS); }
int user_main(int argc, char **argv) { thrd_t t1, t2, t5; int i = 4; atomic_init(&x, 0); thrd_create(&t1, (thrd_start_t)&a, &i); thrd_create(&t2, (thrd_start_t)&b1, NULL); thrd_create(&t5, (thrd_start_t)&c, NULL); thrd_join(t1); thrd_join(t2); thrd_join(t5); return 0; }
static void test_thrd_exit(void) { thrd_t thread; int res; thrd_create(&thread, test_thrd_exit_func, NULL); assert(thrd_join(thread, &res)); assert(res == 2); }
int user_main(int argc, char **argv) { thrd_t t1, t2; atomic_init(&x, -1); atomic_init(&y, 0); printf("Main thread: creating 2 threads\n"); thrd_create(&t1, (thrd_start_t)&a, NULL); thrd_create(&t2, (thrd_start_t)&b, NULL); thrd_join(t1); thrd_join(t2); printf("Main thread is finished\n"); return 0; }
int main(void){ int threadId; int retval; thrd_create(&threadId, f, NULL); thrd_join(threadId, &retval); // thread f is now dead *sneaky; return 0; }
int main(void) { int i, result; Thread threads[] = { { NULL, "Red", 1.f, 0.f, 0.f, 0 }, { NULL, "Green", 0.f, 1.f, 0.f, 0 }, { NULL, "Blue", 0.f, 0.f, 1.f, 0 } }; const int count = sizeof(threads) / sizeof(Thread); glfwSetErrorCallback(error_callback); if (!glfwInit()) exit(EXIT_FAILURE); glfwWindowHint(GLFW_VISIBLE, GL_FALSE); for (i = 0; i < count; i++) { threads[i].window = glfwCreateWindow(200, 200, threads[i].title, NULL, NULL); if (!threads[i].window) { glfwTerminate(); exit(EXIT_FAILURE); } glfwSetWindowPos(threads[i].window, 200 + 250 * i, 200); glfwShowWindow(threads[i].window); if (thrd_create(&threads[i].id, thread_main, threads + i) != thrd_success) { fprintf(stderr, "Failed to create secondary thread\n"); glfwTerminate(); exit(EXIT_FAILURE); } } while (running) { glfwWaitEvents(); for (i = 0; i < count; i++) { if (glfwWindowShouldClose(threads[i].window)) running = GL_FALSE; } } for (i = 0; i < count; i++) thrd_join(threads[i].id, &result); exit(EXIT_SUCCESS); }
void db_worker_start(char *path) { if (!db_enabled) { return; } ring_alloc(&ring, 1024); mtx_init(&mtx, mtx_plain); cnd_init(&cnd); thrd_create(&thrd, db_worker_run, path); }
/* ToDo: Thread Function for Enemy */ int Enemy(void* dummy){ unsigned int count; unsigned int x,y; unsigned int i = 10,j = 10; count=0; printf("Thread:"); do { x = rand(); y = rand(); x %= (MAX_X); y %= (MAX_Y); } while(!CHECK_EMPTY(x, y)); playground[y][x] = ENEMY; //Steuerung des Enemy SDL_Delay(randomGTime(3000, 5000)); thrd_create(NULL, Enemy, NULL); for (count = 0; count < 25U; count++) { int nx, ny; nx = x; ny = y; //playground[y][x] = ENEMY; /* retrieve pacman position, so that enemy can follow ... */ GetPacmanPosition(&i, &j); if (i>x && CHECK_EMPTY_OR_PLAYER(x + 1, y)) nx++; else if (i<x && CHECK_EMPTY_OR_PLAYER(x - 1, y)) nx--; else if (j<y && CHECK_EMPTY_OR_PLAYER(x, y - 1)) ny--; else if (j>y && CHECK_EMPTY_OR_PLAYER(x, y + 1)) ny++; playground[y][x]=EMPTY; playground[ny][nx]=ENEMY; x=nx; y=ny; SDL_Delay(300); } playground[y][x]=EMPTY; return 0; }
int user_main(int argc, char **argv) { thrd_t t1, t2; if (argc > 1) N = atoi(argv[1]); atomic_init(&x, 0); thrd_create(&t1, (thrd_start_t)&a, NULL); thrd_create(&t2, (thrd_start_t)&a, NULL); thrd_join(t1); thrd_join(t2); MODEL_ASSERT(atomic_load(&x) == N * 2); return 0; }
/** * @brief Initialize a stream. * @protected * * @warning This function must only be used to implement a subclass of * @ref SquashStream. Streams returned by other functions will * already be initialized, and you *must* *not* call this function on * them; doing so will likely trigger a memory leak. * * @param stream The stream to initialize. * @param codec The codec to use. * @param stream_type The stream type. * @param options The options. * @param destroy_notify Function to call to destroy the instance. * * @see squash_object_init */ void squash_stream_init (void* stream, SquashCodec* codec, SquashStreamType stream_type, SquashOptions* options, SquashDestroyNotify destroy_notify) { SquashStream* s; assert (stream != NULL); s = (SquashStream*) stream; squash_object_init (stream, false, destroy_notify); s->next_in = NULL; s->avail_in = 0; s->total_in = 0; s->next_out = NULL; s->avail_out = 0; s->total_out = 0; s->codec = codec; s->options = (options != NULL) ? squash_object_ref (options) : NULL; s->stream_type = stream_type; s->state = SQUASH_STREAM_STATE_IDLE; s->user_data = NULL; s->destroy_user_data = NULL; if (codec->impl.create_stream == NULL && codec->impl.splice != NULL) { s->priv = squash_malloc (sizeof (SquashStreamPrivate)); mtx_init (&(s->priv->io_mtx), mtx_plain); mtx_lock (&(s->priv->io_mtx)); s->priv->request = SQUASH_OPERATION_INVALID; cnd_init (&(s->priv->request_cnd)); s->priv->result = SQUASH_STATUS_INVALID; cnd_init (&(s->priv->result_cnd)); s->priv->finished = false; #if !defined(NDEBUG) int res = #endif thrd_create (&(s->priv->thread), (thrd_start_t) squash_stream_thread_func, s); assert (res == thrd_success); while (s->priv->result == SQUASH_STATUS_INVALID) cnd_wait (&(s->priv->result_cnd), &(s->priv->io_mtx)); s->priv->result = SQUASH_STATUS_INVALID; } else { s->priv = NULL; } }
static void testTwoThreads() { start_test("Lock free FIFO - producer/consumer threads"); //TODO: actually check stuff const int es = sizeof(int); const int c = 100; drLockFreeFIFO f; drLockFreeFIFO_init(&f, c, es); thrd_t t1, t2; thrd_create(&t1, entryPointConsumer, &f); thrd_create(&t2, entryPointProducer, &f); int joinRes1, joinRes2; thrd_join(t1, &joinRes1); thrd_join(t2, &joinRes2); }
void client_start() { if (!client_enabled) { return; } mtx_init(&mutex, mtx_plain); if (thrd_create(&recv_thread, recv_worker, NULL) != thrd_success) { perror("thrd_create"); exit(1); } }
int main(int argc, char *argv[]) { thrd_t thread1; thrd_create( &thread1, func_thrd, NULL); thrd_join( thread1, NULL); return EXIT_SUCCESS; }