pthread

Why are all of my threads sleeping using sleep()?

匿名 (未验证) 提交于 2019-12-03 02:38:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I saw the following piece of code regarding threading in Linux on the web. But when I run it, all the threads seem to sleep instead of just the main thread. Why? Also, without sleep(5), the statement-"Thread created successfully" runs 3 times instead of 2? Can someone please explain this behavior? Thanks Compiled using: gcc -pthread check.c and my o/p: First thread processingn Thread created successfullyn Second thread processingn Thread created successfullyn The first two lines are printed with a lag of 5sec and the next 2 after 5

reader-writer accessing multiple readers

匿名 (未验证) 提交于 2019-12-03 02:34:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a few problems I can't solve in implementing WRITER-READER problem in UNIX. First one is that I have no idea, how can I modify the code to work like threads are always calling to enter reading room. For example, when a writer is in the reading room, readers are waiting to access the reading room. When writer is escaping the reading room and readers are entering the reading room, he still is waiting for his chance. The second one is that I have no idea how to modify the code to allow a few readers to enter the reading room. In my code

Exits the loop after a certain amount of time if there's no input

匿名 (未验证) 提交于 2019-12-03 02:30:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm just wondering whether is it possible and how do I implement this feature, where we exit from the loop if there's no input from the user. For example, I want to exit the loop if the user does not input anything after 1 minute. Here's my C code : #include <stdio.h> #include <conio.h> #include <time.h> int main(void) { int x; time_t end = time(0) + 60; printf("Enter a number : "); while (time(0) < end) { if((scanf("%d", &x)) != EOF || (getchar() != '\n')) { time_t end2 = time(0) + 60; while(time(0) < end2); main(); } else { printf("%d", x)

How to fix gcc error: expected while before void

匿名 (未验证) 提交于 2019-12-03 02:16:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: So I am writing a peer-to-peer chat client that uses pthreads to manage all the IO and when I compile the file gcc gives me the error client.c: In function ‘accepted_daemon’: client.c:115:1: error: expected ‘while’ before ‘void’ void * ^ client.c: In function ‘listen_daemon’: client.c:176:1: error: expected ‘while’ before ‘int’ int main(int argc, char *argv[]) ^ The source code for my program is #include <stdlib.h> #include <string.h> #include <stdint.h> #include <errno.h> #include <assert.h> #include <unistd.h> #include <pthread.h> #include

PTHREAD_MUTEX_INITIALIZER inside C++ member function cannot compile?

匿名 (未验证) 提交于 2019-12-03 02:15:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: class A { public: A(); private: pthread_mutex_t mu; }; A::A() { mu = PTHREAD_MUTEX_INITIALIZER; //cannot compile } Can't I initialize pthread_mutex_t inside a class member function? 回答1: Instead of this: A::A() { mu = PTHREAD_MUTEX_INITIALIZER; //cannot compile } Try this: A::A() { pthread_mutex_init( &(mu), NULL); } The PTHREAD_MUTEX_INITIALIZER is a macro,a C struct initializer for something like {0,0,0,0,0{0}} and can only be used at the point of definition. 回答2: Use pthread_mutex_init in this case, as the constant is for compile-time

undefined reference to `pthread_key_create&#039; (linker error)

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have downloaded gtest 1.7.0 sources from here: https://code.google.com/p/googletest/downloads/list and build the gtest .a files (lib files) on ubuntu 13.10: Linux ubuntu 3.11.0-15-generic #23-Ubuntu SMP Mon Dec 9 18:17:04 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux and the resulting lib is called: libgtest.a . In my main.cpp file Have: #include <iostream> #include "gtest/gtest.h" int main(){ std::cout << "Test \n"; int argc = 2; char* cp01; char* cp02; char* argv[] = {cp01, cp02}; testing::InitGoogleTest(&argc, argv); return 0; } From a

pthread: one printf statement get printed twice in child thread

匿名 (未验证) 提交于 2019-12-03 02:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: this is my first pthread program, and I have no idea why the printf statement get printed twice in child thread: int x = 1; void *func(void *p) { x = x + 1; printf("tid %ld: x is %d\n", pthread_self(), x); return NULL; } int main(void) { pthread_t tid; pthread_create(&tid, NULL, func, NULL); printf("main thread: %ld\n", pthread_self()); func(NULL); } Observed output on my platform (Linux 3.2.0-32-generic #51-Ubuntu SMP x86_64 GNU/Linux): 1. main thread: 140144423188224 tid 140144423188224: x is 2 2. main thread: 140144423188224 tid

What is the difference between pthread_self() and gettid()? Which one should I use?

匿名 (未验证) 提交于 2019-12-03 01:56:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to set the CPU affinity of threads on Linux. I'd like to know which one of the following approaches is recommended: Get thread id using pthread_self() Set CPU affinity using pthread_setaffinity_np(....) by passing the thread id as an argument Get thread id using the gettid() call Set CPU affinity using sched_setaffinity(....) by passing the thread id in the place of the process id P.S: After setting the CPU affinity, I intend to increase the scheduling priority of the thread. 回答1: They are not the same . Here are some bits I

Is pthread_join a must when using pthread in linux?

匿名 (未验证) 提交于 2019-12-03 01:45:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I an learning pthread and I have a few questions. Here is my code: #include <pthread.h> #include <stdio.h> #include <stdlib.h> #include <iostream> #define NUM_THREADS 10 using namespace std ; void * PrintHello ( void * threadid ) { int * tid ; tid = ( int *) threadid ; for ( int i = 0 ; i < 5 ; i ++){ printf ( "Hello, World (thread %d)\n" , * tid ); } pthread_exit ( NULL ); } int main ( int argc , char * argv []) { pthread_t threads [ NUM_THREADS ]; int rc ; int t ; int * valPt [ NUM_THREADS ]; for ( t = 0 ; t < NUM_THREADS ; t ++)

Memory leaks when using pthead_exit() to exit thread

匿名 (未验证) 提交于 2019-12-03 01:36:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have a problem when use pthread_exit() to exit .My code is like this: { ... pthread_attr_t attr; iRetValue = pthread_attr_init(&attr); iRetValue = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); size_t nStackSize = 4 * 1024 * 1024; iRetValue = pthread_attr_setstacksize(&attr, nStackSize); while(condition) { ... int32 iSockId = cServSocket.accept(); if(iSockId < 0) { continue; } pthread_t tid; int32* pSockId = new int(iSockId); iRetValue = pthread_create(&tid, &attr, run_thread, (void*)pSockId); ... } ... pthread_attr_destroy(